Last active
February 9, 2025 11:45
-
-
Save AlphaT7/dd03266102a7116c5eb5c47b22a471da to your computer and use it in GitHub Desktop.
One Way Sync Script from Google Sheets App To Google Firebase RTDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"timeZone": "America/New_York", | |
"exceptionLogging": "STACKDRIVER", | |
"runtimeVersion": "V8", | |
"dependencies": {}, | |
"oauthScopes": [ | |
"https://www.googleapis.com/auth/firebase.database", | |
"https://www.googleapis.com/auth/userinfo.email", | |
"https://www.googleapis.com/auth/spreadsheets", | |
"https://www.googleapis.com/auth/script.scriptapp", | |
"https://www.googleapis.com/auth/script.external_request" | |
], | |
"executionApi": { | |
"access": "DOMAIN" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function myOnEdit() { | |
const RTDB_URL = "path/to/your/realtimedb/node.json"; | |
const sheet = SpreadsheetApp.getActiveSheet(); | |
let data = sheet.getDataRange().getValues(); | |
let payload = {}; | |
for (let i = 0; i < data.length; i++) { | |
payload[data[i][0]] = { | |
meal: data[i][1], | |
mealType: data[i][2], | |
meatType: data[i][3] | |
} | |
} | |
const token = ScriptApp.getOAuthToken(); | |
const url = RTDB_URL + "?access_token=" + encodeURIComponent(token); | |
const response = UrlFetchApp.fetch(url, { | |
method: 'put', | |
payload: JSON.stringify(payload) | |
}) | |
//Logger.log(response.getResponseCode()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment