-
Notifications
You must be signed in to change notification settings - Fork 7
/
writeGoogleSheetsToFirebase.googleScript
52 lines (44 loc) · 1.67 KB
/
writeGoogleSheetsToFirebase.googleScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function writeDataToFirebase() {
var ss = SpreadsheetApp.openById("1Qrgw91Vod2bPmH1OlDMBqGJG2DF5XGZnIGz_Lc_9PvQ");
var sheet = ss.getSheets()[0];
var data = sheet.getDataRange().getValues();
var dataToImport = {};
for(var i = 2; i < data.length; i++) {
var programName = data[i][0];
var programKey = data[i][1];
// Stop processing at the end marker.
if (programName == "end") {
break;
}
// Ignore row if no key is present.
if (programKey == "") {
break;
}
dataToImport[programName] = {};
for(var y = 0; y < 100; y++) {
var columnName = data[1][y]
if(columnName) {
columnName = toTitleCase(columnName).replace(/\s/g, '');
columnName = decapitalizeFirstLetter(columnName)
// Don't send columns that begin with the word 'private'.
if (columnName.substr(0,7) != 'private') {
dataToImport[programName][columnName] = data[i][y];
}
}
}
}
// TODO: Use more secure auth method as per https://sites.google.com/site/scriptsexamples/new-connectors-to-google-services/firebase/tutorials/authenticate-with-a-service-account
// Until this is complete, do not commit this token to any shared repo.
var firebaseUrl = "https://southafricacodeschool.firebaseio.com/";
var token = "[FIREBASE-DB-SECRET-GOES-HERE]";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl, token);
base.setData("", dataToImport);
}
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
function decapitalizeFirstLetter(string) {
return string.charAt(0).toLowerCase() + string.slice(1);
}