-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMasterUtility.gs
127 lines (90 loc) · 3.92 KB
/
MasterUtility.gs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var MasterUtility=new function(){
//------------------------------------------------------------------------------------------------------------------
/**
* write into MASTER TEMPLATE the notes/data taken from Firebase
* @param {string} selected country
* @param {string} user token
*/
//------------------------------------------------------------------------------------------------------------------
this.writeNoteAndDataForCountriesMaster= function(countrySelected,isReset){
//read config from firebase
var templateCompilerNode = 'config/templateCompiler/'+countrySelected;
var templateCompiler = JSON.parse(FirebaseConnector.getFireBaseData(templateCompilerNode,FirebaseConnector.getToken()));
//read config from firebase
var valuesNode = '';
var valuesToBeWritten = '';
for (var values in templateCompiler) {
valuesNode = 'config/templateCompiler/'+countrySelected+'/'+values;
valuesToBeWritten = JSON.parse(FirebaseConnector.getFireBaseData(valuesNode,FirebaseConnector.getToken()));
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(values);
//get TEMPLATE sheet
var templateSheet = Utility.getTemplateByCommodity(values);
for (var subNode in valuesToBeWritten) {
//contain the range
//valuesToBeWritten[subNode][0]
//contain the value
//valuesToBeWritten[subNode][1]
if(isReset){
//delete all the data/notes. The master will be restored
sheet.getRange(valuesToBeWritten[subNode][0]).setValue('');
templateSheet.getRange(valuesToBeWritten[subNode][0]).setValue('');
}else{
//set the values from Firebase
sheet.getRange(valuesToBeWritten[subNode][0]).setValue(valuesToBeWritten[subNode][1])
templateSheet.getRange(valuesToBeWritten[subNode][0]).setValue(valuesToBeWritten[subNode][1]);
}
}
}
};
/**
* execute a function for each spreadsheet registered in firebase
* @param {Function} callback function to execute taking 5 arguments: nation (string), the spreadsheet id, spreadsheet object, index, total length
* @return {void}
* @throws "InvalidArgument"
* @throws "InvalidSpreadsheet" if the spreadsheet is not accessible
*/
this.forEachSpreadsheet=function(callback){
var countryRegister,countryRegisterNode, userToken, _currSpreadsheet, _id, _index=0, _length;
if ( !callback) {
throw "InvalidArgument";
}
//datanode from firebase
countryRegisterNode = 'config/countryRegister';
userToken=FirebaseConnector.getToken();
//retrive the country google sheet id stored
countryRegister = JSON.parse(FirebaseConnector.getFireBaseData(countryRegisterNode,userToken));
_length=Object.keys(countryRegister).length;
for (var _nation in countryRegister) {
if (countryRegister.hasOwnProperty(_nation)) {
_id=countryRegister[_nation];
if(!_id || _id==="false"){
continue;
}
_currSpreadsheet=SpreadsheetApp.openById(_id);
if ( !_currSpreadsheet ) {
throw "InvalidSpreadsheet";
}
callback(_nation, _id, _currSpreadsheet, _index, _length);
_index++;
}
}
};
/**
* update all named range from the active spreadsheet to all spreadsheet registeredi in firebase
* @return {void}
*/
this.updateNamedRangesOfAllSpreadSheet=function(){
var source, report="";
source=SpreadSheetCache.getActiveSpreadsheet();
this.forEachSpreadsheet(function(destName, destId, dest,index,length){
Utility.copyAllNamedRanges(dest, source);
report+=destName+" updated on "+moment().format("YYYY-MM-DD HH:mm:ss")+" id:"+destId+"\\n";
SpreadSheetCache.getActiveSpreadsheet().toast("Updated "+destName, (index+1)+" of "+length, 30);
});
Browser.msgBox(
"All Spreadsheet are updated succesfully!\\n\\n"+
"Update report:\\n"+
report
);
};
};