Skip to content

Instantly share code, notes, and snippets.

@AlphaT7
Last active February 9, 2025 11:45
Show Gist options
  • Save AlphaT7/dd03266102a7116c5eb5c47b22a471da to your computer and use it in GitHub Desktop.
Save AlphaT7/dd03266102a7116c5eb5c47b22a471da to your computer and use it in GitHub Desktop.
One Way Sync Script from Google Sheets App To Google Firebase RTDB
{
"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"
}
}
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