-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.gs
153 lines (129 loc) · 4.16 KB
/
Utility.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
Utility=new AmisLib.UtilityClass(Config.devMode, Config.errorEmail);
/**
* open amis Sidebar
*/
Utility.openSidebar = function() {
var sheet=SpreadSheetCache.getActiveSheet();
var spreadsheet=SpreadSheetCache.getActiveSpreadsheet();
dbName = Config.dbName;
apiKey = Config.apiKey;
countryCell = sheet.getRange( Config.Sheet.countryCell ).getValue();
datasourceCell = sheet.getRange( Config.Sheet.datasourceCell ).getValue();
devMode = Config.devMode;
secretariatMode = Utility.isSecretariat();
spreadSheetId = Utility.getGoogleSheetID();
isMaster=Utility.isMaster(spreadsheet);
var html = HtmlService.createTemplateFromFile( 'amisMenu' )
.evaluate()
.setTitle( 'Amis' )
.setWidth( 500 )
.setSandboxMode( HtmlService.SandboxMode.IFRAME );
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showSidebar( html );
};
/**
* create Amisi menu
*/
Utility.createAmisMenu = function() {
//create the menu voice
SpreadsheetApp.getUi()
.createMenu( 'AMIS' )
.addItem( 'Open', 'AmisMarketApp.openSidebar' )
.addToUi()
};
/**
* check if the current spreadsheet is Master
* @param {object} spreadsheet [optional] the spreadsheet
* @return {bool} true if master, false otherwise
*/
Utility.isMaster = function( spreadsheet ) {
spreadsheet = spreadsheet || SpreadSheetCache.getActiveSpreadsheet();
return spreadsheet.getName().indexOf( Config.masterKeyword ) > -1;
};
/**
* check if the current spreadsheet is Master
* @return {bool} true if master, false otherwise
*/
Utility.isSecretariat = function() {
return SpreadsheetApp.getActiveSpreadsheet().getName().indexOf( Config.secretariatKeyword ) > -1;
};
/**
* check if the current spreadsheet is Master
* @param {object} sheet [optional] the sheet
* @return {bool} true if template, false otherwise
*/
Utility.isTemplate = function(sheet) {
sheet=sheet||SpreadSheetCache.getActiveSpreadsheet().getActiveSheet();
return sheet.getName().indexOf( Config.templatePrefix ) === 0;
};
/**
* show/hide all templates in the spreadsheet
* @param {bool} show true to show all templates, false to hide
* @param {string} SpreadSheet ID
*/
Utility.toggleTemplates=function(show,spreadSheetId){
var sheets=spreadSheetId? SpreadsheetApp.openById(spreadSheetId).getSheets() : SpreadSheetCache.getActiveSpreadsheet().getSheets(),s;
for (var _i = 0, sheets_length=sheets.length; _i<sheets_length; _i++) {
s=sheets[_i];
if(Utility.isTemplate(s)){
if (show) {
s.showSheet();
} else {
s.hideSheet();
}
}
}
};
/**
* get Template sheet by commodities
* @param {string} commodity name of commodity (eg. 'maize')
* @return {SHEET} template sheet
*/
Utility.getTemplateByCommodity = function(commodity) {
return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Config.templatePrefix+commodity);
};
/**
* get Template sheet by sheetName
* @param {string} spreadsheetName (optional) the spreadsheet name
* @return {SHEET} template sheet
*/
Utility.getTemplateBySheetName=function(spreadsheetName){
spreadsheetName=(spreadsheetName || SpreadSheetCache.getActiveSheetName());
return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Config.templatePrefix+spreadsheetName);
};
/**
* @deprecated use Utility.getTemplateBySheetName
*/
Utility.getTemplateBySpreadSheetName=Utility.getTemplateBySheetName;
/**
* get the template object for a shee:
* @param {SHEET} sheet the sheet
* @return {SHEET} the template
* @throws {InvalidArgument}
*/
Utility.getTemplateBySheetObj=function(sheet){
var sheetName;
if (!sheet) {
throw "InvalidArgument";
}
sheetName=sheet.getName();
return Utility.getTemplateBySheetName(sheetName);
};
/**
* get Template sheet by commodities
* @param
* @return
*/
Utility.unhideAllColumns = function(sheet) {
var range = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
sheet.unhideColumn(range);
};
/**
* includes html files into an html
* @param {string} filename
* @return {string} the content
* @todo this function cannot access to the template from AmisLib.Utility
*/
Utility.include = function( filename ) {
return HtmlService.createTemplateFromFile( filename ).evaluate().getContent();
};