-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncMasterSheet.gs
442 lines (350 loc) · 15.6 KB
/
SyncMasterSheet.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
var SyncMasterSheet=new function(){
/**
* update all the lastUpdate cells from firebase
* @param {array} fbData firebase data of the sheet
* @param {array} sheetValues sheet's data
* @param {Sheet} the chosen sheet
* @param {bool} true= clear data, false= set data
*/
this.lastDatefetcher=function(fbData, sheetValues, chosenSheet, isReset){
var labelRowForLastDateIndex,fbDataLURow,cell, sheetDate,range;
labelRowForLastDateIndex=(LastDateUpdater.getLURow(chosenSheet)-1);
chosenSheet = chosenSheet || SpreadSheetCache.getActiveSheet();
fbDataLURow=fbData[labelRowForLastDateIndex];
for (var _i = 0, sheetValuesLURow_length=fbDataLURow.length; _i<sheetValuesLURow_length; _i++) {
cell=fbDataLURow[_i];
if(cell===sheetValues[labelRowForLastDateIndex][_i]){
continue;
}
sheetDate=moment(sheetValues[labelRowForLastDateIndex][_i]).format(Config.lastUpdatedDateDBFormat);
//if fb cell different to sheetcell
if(cell!==sheetDate){
range=chosenSheet.getRange(labelRowForLastDateIndex+1, _i+1);
range.setValue(cell);
range.setNumberFormat(Config.lastUpdatedDateSheetFormat);
}else if(isReset){
range=chosenSheet.getRange(labelRowForLastDateIndex+1, _i+1);
range.setValue('');
}
}
};
/**
* show a confirm message to the user to ask confirmation to discard sheet data
* @return {bool} true if user said yes, false otherwise
*/
this.fetchConfirmMsg = function() {
var userChoise = "yes";
userChoise = Browser.msgBox( 'DISCARD CHANGES', 'Discard your edits and overwrite the sheet with the data from the AMIS database?', Browser.Buttons.YES_NO );
return ( userChoise === "yes" );
};
/**
* Get the data from firebase
* @param {string} userToken auth token
* @param {bool} forceload (default false) if true doesn't ask the user for loading data
*/
this.startFetch = function( userToken, forceload ) {
var spreadsheet, country;
spreadsheet = SpreadSheetCache.getActiveSpreadsheet();
country = FirebaseConnector.getCountryNameFromSheet( userToken );
if ( ( forceload !== true ) && ( !SyncMasterSheet.fetchConfirmMsg() ) ) {
return;
}
Utility.forEachSheet( null, /^[A-Za-z]+$/, function( sheet, sheetName ) {
//fetch lastupdate date
SyncMasterSheet.getLastUpdate(sheet);
//in the call back we load data for all the commodities
SyncMasterSheet.startFetchLoadAllData( userToken, spreadsheet, sheet, sheetName, false, country );
} );
Utility.toastInfo( 'Data successfully loaded from the AMIS database', 'DATA SAVED' );
};
/**
* Fetch or delete all data for the master template, for a chosen country (loop all the sheet of a spreadsheet)
* @param {string} userToken auth token
* @param {bool} forceload (default false) if true doesn't ask the user for loading data
* @param {string} the country selected
* @param {bool} true=clear data , false = set data
*/
this.startFetchMaster = function( userToken, forceload, countrySelected, isReset ) {
var spreadsheet;
spreadsheet = SpreadSheetCache.getActiveSpreadsheet();
if ( ( forceload !== true ) && ( !SyncMasterSheet.fetchConfirmMsg() ) ) {
return;
}
Utility.forEachSheet( null, /^[A-Za-z]+$/, function( sheet, sheetName ) {
//in the call back we load data for all the commodities
SyncMasterSheet.startFetchLoadAllData( userToken, spreadsheet, sheet, sheetName, isReset, countrySelected );
} );
Utility.toastInfo( 'Data successfully loaded from the AMIS database', 'DATA SAVED' );
};
/**
* Get the ALL data from firebase for each commodities by country
* @param {string} userToken auth token
* @param {object} spreadsheet [optional] the spreadsheet
* @param {object} sheet [optional] the sheet
* @param {string} sheetName sheet's name*
* @param {bool} isReset true=clear data , false = set data
* @param {string} countrySelected the country selected
* @return {void}
* @throws {InvalidFirebaseData}
*/
this.startFetchLoadAllData=function(userToken, spreadsheet, sheet, sheetName, isReset, countrySelected) {
userToken= userToken || FirebaseConnector.getToken();
countrySelected = countrySelected || FirebaseConnector.getCountryNameFromSheet(userToken);
spreadsheet = spreadsheet || SpreadSheetCache.getActiveSpreadsheet();
//var sheetName= sheet.getName().toLowerCase();
sheetName=sheetName.toLowerCase();
try {
//we take only NOT TEMPLATE_ sheets
if(sheet.getSheetName().indexOf(Config.templatePrefix)){
//Get the currently active sheet
var sheetValues=sheet.getSheetValues(1, 1, sheet.getLastRow(),sheet.getLastColumn());
var rangeFromConfig= SyncMasterSheet.getRangeToBeStored(sheetName);
var fbData, fireBaseValues, baseOfSaveNode= JSON.parse(SyncMasterSheet.getAbsoluteDataSheetPath(userToken))+ '/'+ countrySelected+'Data'+ '/' + sheet.getSheetName().toLowerCase();
fbData=FirebaseConnector.getFireBaseDataParsed(baseOfSaveNode, userToken);
//exception if firebase data has not the same size of the sheet
if (!isReset && ((!fbData) || (fbData.length!==sheetValues.length) || (fbData[0].length!==sheetValues[0].length))) {
Browser.msgBox("Firebase data has not the same size of the sheet "+sheetName);
throw "InvalidFirebaseData";
}
//get lastDateUpdaterRow
SyncMasterSheet.lastDatefetcher(fbData, sheetValues, sheet, isReset);
//get all range to be stored
if (fbData) {
for (var i=0; i<rangeFromConfig.length;i++){
//get Firebase node name to be fetch
fireBaseValues=Utility.getRangeValuesFromArray(fbData, rangeFromConfig[i]);
//if data note IS NOT EMPTY
if(fireBaseValues){
//if isreset...empty all data
if(isReset){
sheet.getRange(rangeFromConfig[i]).setValue('');
}
else{
//set value into cells
sheet.getRange(rangeFromConfig[i]).setValues(fireBaseValues);
}
}
}
}
ProtectionMaker.validateSheet(sheetValues, spreadsheet, sheet);
ForecastUtility.hideAllPreviousForecasts(sheet);
}
} catch (e) {
var ex=e;
if(e!=="Network401Error"){
Utility.sendErrorEmails(
"Firebase data wrong",
Config.errorEmail
);
Browser.msgBox(
"Internal error reading the data from the AMIS database.\\n"+
"The AMIS administrator has been notified.\\n"+
"You can contact them directly on [email protected]");
}
throw e;
}
}
/**
* get single range values and prepare it to be stored in firebase
* @param {array} sheetValues all the data in the sheet. from first column to the last
* @param {string} range range in A1Notation
* @param {array} fmRanges ForecastingMethodologies ranges from firebase
* @return {array} a two-dimensional array of values, indexed by row, then by column
*/
this.getRangeValuesToBeStored=function(sheetValues, range, fmRanges) {
var currA1, rangeIndexes, sheetDataJson,dataToBeStored;
dataToBeStored = Utility.getRangeValuesFromArray(sheetValues, range);
rangeIndexes=ConvertA1.rangeA1ToIndex(range);
sheetDataJson=JSON.stringify(dataToBeStored);
for (var i = 0, len = dataToBeStored.length; i < len; i++) {
for (var j = 0, len2 = dataToBeStored[i].length; j < len2; j++){
currA1=Utility.numToChar(j+1+rangeIndexes.left)+(i+1+rangeIndexes.top);
dataToBeStored[i][j]=ForecastingMethodologies.onEditCell(currA1,fmRanges,dataToBeStored[i][j],true);
sheetValues[i+1+rangeIndexes.top][j+1+rangeIndexes.left]=dataToBeStored[i][j];
}
}
//if the range doesn't contains date but has changed
if (JSON.stringify(dataToBeStored)!==sheetDataJson) {
SpreadSheetCache.getActiveSheet().getRange(range).setValues(dataToBeStored);
}
return sheetValues;
};
/**
* get the sheetValues array and format all the last date dates
* @param {array} sheetValues all the data in the sheet. from first column to the last
* @param {object} sheet [optional] the sheet
* @return {array} the sheetValues with the dates formatted
* @throws {InvalidArgument}
*/
this.formatAllLastDate = function( sheetValues, sheet ) {
if ( sheet === null ) {
throw "InvalidArgument";
}
sheet = ( sheet || SpreadSheetCache.getActiveSheet() );
var labelRowForLastDateIndex, sheetValuesLURow, cell;
labelRowForLastDateIndex = ( LastDateUpdater.getLURow( sheet ) - 1 );
sheetValuesLURow = sheetValues[ labelRowForLastDateIndex ];
for ( var _i = 0, sheetValuesLURow_length = sheetValuesLURow.length; _i < sheetValuesLURow_length; _i++ ) {
cell = sheetValuesLURow[ _i ];
if ( moment.isDate( cell ) ) {
sheetValues[ labelRowForLastDateIndex ][ _i ] = moment( cell ).format( Config.lastUpdatedDateDBFormat );
}
}
return sheetValues;
};
/**
* validate and reads the data of a sheet for saving in firebase
* @param {object} spreadsheet the spreadsheet
* @param {object} sheet the sheet
* @param {string} sheetName sheet's name
* @return {array} array of data of the sheet rightly formatted
* @throws {InvalidSheetData}
*/
this.startSyncSheet = function( spreadsheet, sheet, sheetName ) {
var sheetValues;
Logger.log( sheetName );
sheetValues = sheet.getDataRange().getValues();
ForecastUtility.hideAllPreviousForecasts( sheet );
try {
ProtectionMaker.validateSheet( sheetValues, spreadsheet, sheet );
} catch ( e ) {
var ex = e;
throw "InvalidSheetData";
}
//SyncMasterSheet.setLastUpdate( sheet );
sheetValues = SyncMasterSheet.formatAllLastDate( sheetValues );
//SyncMasterSheet.syncMasterSheet(sheetValues,userToken,baseOfSaveNode, sheet);
return sheetValues;
};
/**
* Saving Sheet Data function
* @param {string} userToken auth token
* @throws {InvalidSheetData} in case of non valid data in the sheet
*/
this.startSync = function( userToken ) {
var baseOfSaveNode, spreadsheet, spreadsheetData = {};
spreadsheet = SpreadSheetCache.getActiveSpreadsheet();
baseOfSaveNode = JSON.parse( SyncMasterSheet.getAbsoluteDataSheetPath( userToken ) ) + '/' + JSON.parse( SyncMasterSheet.getNodeToWriteData( userToken ) ).dataSheetNode;
Utility.forEachSheet( null, /^(?!Template).*$/, function( sheet, sheetName ) {
spreadsheetData[ sheetName.toLowerCase() ] = SyncMasterSheet.startSyncSheet( spreadsheet, sheet, sheetName );
} );
FirebaseConnector.writeOnFirebase( spreadsheetData, baseOfSaveNode, userToken );
Utility.toastInfo( 'Data successfully saved to the AMIS database', 'DATA SAVED' );
};
/**
* retrive the absolute firebase node where write data
* @param {string} auth token
* @return {string} the correct node where save datas
*/
this.getAbsoluteDataSheetPath = function(userToken){
var dataBaseNodeToRead='config/absoluteDataSheetPath';
var node=APPCache.get("absoluteDataSheetPath");
if (!node) {
node=FirebaseConnector.getFireBaseData(dataBaseNodeToRead,userToken);
APPCache.put("absoluteDataSheetPath", node);
}
return node;
};
/**
* retrive from config how to name the new node (depends on the country )
* @param {string} auth token
* @return {string} the correct node where save datas
*/
this.getNodeToWriteData= function (userToken){
var sheetId= this.getSheetId();
var dataBaseNodeToRead='config/countries/'+sheetId;
return FirebaseConnector.getFireBaseData(dataBaseNodeToRead,userToken);
};
/**
* retrive from config how to name the new node (depends on the country )
* @param {string} auth token
* @return {string} the correct node where save datas
*/
this.getNodeToWriteDataSecretariat= function (userToken,chosenCountry){
chosenCountry = getSecretariatCountry();
var addingString = 'Data'
return chosenCountry+addingString;
};
this.getSheetId= function(){
return Utility.getGoogleSheetID();
};
/**
* set the current date to the lastUpdateCell in the sheet
* @param {object} sheet [optional] the sheet
* @return {void}
* @throws {InvalidArgument}
*/
this.setLastUpdate = function( sheet ) {
if ( sheet === null ) {
throw "InvalidArgument";
}
sheet = ( sheet || SpreadSheetCache.getActiveSheet() );
var date = new Date();
var lastUpdateCell = AmisNamedRanges.getCommodityNamedRanges().lastUpdateCell.cell;
sheet.getRange( lastUpdateCell ).setValue( date );
};
/**
* get from Database the current lastUpdateCell in the current sheet
* @param {object} sheet [optional] the sheet
* @return {void}
* @throws {InvalidArgument}
*/
this.getLastUpdate = function( sheet ) {
if ( sheet === null ) {
throw "InvalidArgument";
}
sheet = ( sheet || SpreadSheetCache.getActiveSheet() );
var userToken= userToken || FirebaseConnector.getToken();
var countrySelected = countrySelected || FirebaseConnector.getCountryNameFromSheet(userToken);
var fbData, fireBaseDate, baseOfSaveNode= JSON.parse(SyncMasterSheet.getAbsoluteDataSheetPath(userToken))+ '/'+ countrySelected+'Data'+ '/' + sheet.getSheetName().toLowerCase();
fbData=FirebaseConnector.getFireBaseDataParsed(baseOfSaveNode, userToken);
var lastUpdateCell = AmisNamedRanges.getCommodityNamedRanges().lastUpdateCell.cell;
//get Firebase node name to be fetch
fireBaseDate=Utility.getRangeValuesFromArray(fbData, lastUpdateCell+':'+lastUpdateCell);
var fireBaseDateFormatted = moment(new Date(fireBaseDate).toISOString()).utc().format(Config.lastUpdatedDateDBFormat);
if (fbData) {
sheet.getRange(lastUpdateCell).setValue(fireBaseDateFormatted);
sheet.getRange(lastUpdateCell).setNumberFormat(Config.lastUpdatedDateSheetFormat);
}
};
/**
* retrive all the ranges to be stored
* @param {string} auth token
* @return {array} ranges to be stored
*/
this.getRangeToBeStored = function(commodityName){
return AmisNamedRanges.getCommodityNamedRanges(commodityName).rangeToBeStored;
};
//---------------------------------------------------------
//---------------------------------------------------------
// END -- Retrives all the ranges to be stored
//---------------------------------------------------------
//---------------------------------------------------------
/**
* retrive all the ranges to be stored FOR SECRETARIAT
* @param {string} auth token
* @return {array} ranges to be stored
*/
//---------------------------------------------------------
this.getRangeToBeStoredSecretariat = function() {
return this.getRangeToBeStored();
};
//---------------------------------------------------------
//---------------------------------------------------------
// END -- Retrives all the ranges to be stored FOR SECRETARIAT
//---------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------
// delete saved data
//------------------------------------------------------------------------------------------------------------------
this.deleteSavedData = function(){
var ar = [];
FirebaseConnector.writeOnFirebase(
ar,
'dataAmisSheet/countries/argentinaData',
FirebaseConnector.getToken()
);
};
//------------------------------------------------------------------------------------------------------------------
// END -- delete saved data
//------------------------------------------------------------------------------------------------------------------
};