Use Google sheet as remote config
Sheet:
Code:
suspend fun main() {
val fig = Fig(sheetUrl = "https://docs.google.com/spreadsheets/d/../edit?usp=sharing") // your Google sheet URL
fig.load() // call this before calling any getXXX function
println("Fruit is '${fig.getString("fruit", null)}'")
}Output:
Fruit is 'apple'
- 📄 Create a Google Sheet with two columns
keyandvalue
- 🔗 Choose "Anyone with link" and copy the link
- 🤝 Add dependency
repositories {
maven { url = uri("https://jitpack.io") }
}
dependencies {
// [latest version - i promise!]
implementation("com.github.theapache64:fig:0.3.0")
}Groovy:
repositories {
maven { url 'https://jitpack.io' } // Add jitpack
}
dependencies {
// [latest version - i promise!]
implementation 'com.github.theapache64:fig:0.3.0'
}- ⌨️ Use
Fig.loadandFig.getXXX
suspend fun main() {
val fig = Fig(sheetUrl = "YOUR-GOOGLE-SHEET-URL-GOES-HERE")
fig.load()
println("Fruit is '${fig.getString("fruit", null)}'")
}- 💻 Output
Fruit is 'apple'
Fig supports automatic cache refresh through a global TTL (Time To Live) parameter. When specified, Fig will automatically refresh the entire cache from the Google Sheet in the background when the specified duration has elapsed.
You can specify a cache TTL when creating the Fig instance:
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
suspend fun main() {
// Cache will auto-refresh every 5 minutes
val fig = Fig(
sheetUrl = "YOUR-GOOGLE-SHEET-URL-GOES-HERE",
cacheTTL = 5.minutes
)
fig.load()
// All subsequent calls use the cached data
val apiKey = fig.getString("api_key", null)
val isFeatureEnabled = fig.getBoolean("feature_flag", false)
println("API Key: $apiKey")
println("Feature Enabled: $isFeatureEnabled")
}- Global cache: The TTL applies to the entire cache, not individual keys
- Automatic background refresh: When the TTL expires, Fig automatically fetches fresh data from the Google Sheet in the background
- Non-blocking: Cache refreshes happen asynchronously without blocking your data access
- Default behavior: If no TTL is specified, the cache never expires (
Duration.INFINITE)
Fig exposes two public properties to help you monitor cache status:
val fig = Fig(sheetUrl = "YOUR-URL", cacheTTL = 5.minutes)
fig.load()
// Check when the cache was last updated (timestamp in milliseconds)
val lastUpdate: Long? = fig.inMemCacheUpdatedAt
// Check if cache is currently being refreshed
val isRefreshing: Boolean = fig.isRefreshingThese properties are useful for:
- Displaying cache freshness in your UI
- Debugging cache behavior
- Implementing custom cache management logic
If you want to see this library in practice, you can check out this video tutorial on YouTube: https://youtu.be/E8X94pCJ2zs
- Your value field can't have two data types. To solve this always wrap your number inputs with
TO_TEXTfunction. Eg:=TO_TEXT("2.4") - This library uses an unofficial Google Sheets API to fetch data, which may stop working at any time. It's best to use this library only for small projects where you need quick, dynamic values without setting up something like Firebase (and honestly, most of my projects use this library as a config source and database, so if it crashes, I'll be crying right alongside you).
👤 theapache64
- Twitter: @theapache64
- Email: [email protected]
Feel free to ping me 😉
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Open an issue first to discuss what you would like to change.
- Fork the Project
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a pull request
Please make sure to update tests as appropriate.
Give a ⭐️ if this project helped you!
Copyright © 2024 - theapache64
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This README was generated by readgen ❤
