-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExcelExport.gs
107 lines (78 loc) · 3.28 KB
/
ExcelExport.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
/**
* class to manage all operation for the excel exportation
*/
var ExcelExport = new function() {
/**
* create an empty spreadsheet for the excel exportation (copy a spreadsheet from the export master file)
* @param {string} countryLabel label of the country
* @param {string} userToken auth token
* @return {string} id of the file
* @throws {InvalidArgument}
*/
this.createExportSheet = function(countryLabel,token) {
var master, filename, newfile, spreadSheetConfig;
if (!countryLabel) {
throw "InvalidArgument";
}
spreadSheetConfig=FirebaseConnector.getSheetConfig(SpreadSheetCache.getActiveSpreadsheet().getId(), token);
//get current file master file to be cloned
master = DriveApp.getFileById( spreadSheetConfig.excelExportSheetId );
filename = Utility.interpolate( Config.excelExportSpreadSheetFileName, {
country: countryLabel
} );
newfile= master.makeCopy( filename );
//set permissions
newfile.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
return newfile.getId();
};
/**
* write the spreadsheet id of the excel exportation spreadsheet to firebase
* @param {string} parentSheetId the spreadsheet id of the nation
* @param {string} exportSheetId spreadsheet id of the excel exportation spreadsheet
* @param {string} userToken auth token
* @return {void}
* @throws {InvalidArgument}
*/
this.storeExportSheetId=function(parentSheetId, exportSheetId, userToken){
var sheetConfigNode;
if (!parentSheetId || !exportSheetId || !userToken) {
throw "InvalidArgument";
}
sheetConfigNode=FirebaseConnector.getSheetConfigNode(parentSheetId)+"/excelExportSheetId";
FirebaseConnector.writeOnFirebase(exportSheetId,sheetConfigNode,userToken);
};
/**
* reads the spreadsheet id of the spreadsheet used for the excel exportation from firebase
* @param {String} parentSpreadSheetId (optional) the spreadSheetId of the parent spreadsheet (the one with used from the user). Current spreadsheet will be used if undefined
* @param {string} userToken auth token
* @return {String} the spreadSheetId
* @throws {InvalidArgument}
* @throws {InvalidDbData} if data not found in firebase
*/
this.getExcelExportSheetId=function(parentSpreadSheetId, userToken){
parentSpreadSheetId=(parentSpreadSheetId || SpreadSheetCache.getActiveSpreadsheet().getId());
var sheetConfig;
if (!userToken) {
throw "InvalidArgument";
}
sheetConfig=FirebaseConnector.getSheetConfig(parentSpreadSheetId, userToken);
if (!sheetConfig || !sheetConfig.excelExportSheetId) {
throw "InvalidDbData";
}
return sheetConfig.excelExportSheetId;
};
/**
* start the excel exportation: copy all values to the excel export sheet and return the id for the download
* @param {string} userToken auth token
* @return {String} the spreadSheetId
*/
this.startExport=function(userToken){
var from, to, fromSpreadSheet;
fromSpreadSheet=SpreadSheetCache.getActiveSpreadsheet();
from=fromSpreadSheet.getId();
to=ExcelExport.getExcelExportSheetId(from, userToken);
ProtectionMaker.validateAllSheet(fromSpreadSheet);
Utility.copyAllSpreadSheetValues(from, to, Config.commoditySheetsRegex);
return to;
};
};