-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
275 lines (231 loc) · 7.2 KB
/
Code.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
/* Variables */
var userTable = SpreadsheetApp.openById(getUserTableId());
var safetyTable = SpreadsheetApp.openById(getSafetyTableId());
var cacheModules = true;
if(getIsCaching){
cacheModules = getIsCaching();
}
var MeritBadges = [
'Laser Cutter',
'3D Printing',
'HandiBot',
'Woodshop Level 1 Tools',
'Woodshop Level 2 Tools',
'Woodshop Level 3 Tools',
'Print Shop',
'Bookbinding',
'Sewing Machine',
'Embroidery Machine',
'Vinyl Cutter',
'FormLabs',
'Soldering',
'Arduino',
'Button Maker',
'Raspberry Pi',
'Programming'
];
function getMeritBadges(){
return MeritBadges;
}
/*
================================ DOGET =====================================
This functions gets called when the pages loads every time.
============================================================================
*/
function doGet(e) {
// var parameter = e.parameter;
Logger.log("Opening page...");
var html = HtmlService
.createTemplateFromFile('index')
.evaluate()
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
.addMetaTag('viewport', 'width=device-width, initial-scale=1');
return html;
}
/*
=============================== INCLUDE ====================================
this function let you include other files' content in your index.html
============================================================================
*/
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
/*
=============================== INCLUDEMODULES ====================================
this function let you include ALL the modules for a particular section
============================================================================
*/
function includeModules(section){
var cache = CacheService.getScriptCache();
if(cacheModules){
var cached = cache.get("includeModules"+section);
if (cached != null) {
return cached;
}
}
console.time("includeModules"+section);
var sectionContent = "";
var modules = getModules();
for (var i = 0 ; i < modules.length; i++){
var module = modules[i];
var modeleSectionFilename = "module."+module+"."+section;
try{
var moduleSectionContent = HtmlService.createHtmlOutputFromFile(modeleSectionFilename)
.getContent();
sectionContent += moduleSectionContent;
}catch(e){
Logger.log("ERROR IN includeModule");
Logger.log(e);
}
}
console.timeEnd("includeModules"+section);
cache.put("includeModules"+section, sectionContent, 1500); // cache for 25 minutes
return sectionContent;
}
/*
=========================== DATAINTOHASHROWS ===============================
Any time you get some rows from a google sheet, run it through this function
so that it uses the column names as keys, instead of just numbers.
data : the data from the sheet
keysRow : the row that holds the column names (starts a 0, NOT 1)
startsRow : the row that holds the FIRST row of data (starts a 0, NOT 1)
filterFunction: a function that gets a row of data (with the column names as
keys), and returns true or false, based on whatever criteria you want.
============================================================================
*/
function dataIntoHashRows(data, keysRow, startRow, filterFunction){
var idKey= {};
var keyId= {};
var newData = [];
Logger.log("data");
for (var k = 0; k < data[keysRow].length; k++) {
var key = data[keysRow][k];
key = key.replace("?","");
key = key.replace("'","");
key = key.replace(":","");
if(key.trim() == ""){
continue;
}
idKey[k] = key;
keyId[key] = k;
}
for (var i = startRow; i < data.length; i++) {
var newRow = {};
for (var j = 0; j < data[i].length; j++) {
if(!idKey[j] || idKey[j].trim() == ""){
continue;
}
newRow[idKey[j]] = data[i][j];
}
if(!filterFunction || filterFunction(newRow) == true){
newData.push(newRow);
}
}
return {data:newData, keyId: keyId, idKey: idKey};
}
/*
============================ INSERTHASHROW =================================
Insert a new row into a sheet. Use column names as keys. You don't have to
have blank columns in the row
table: the google sheets object
data: the row, with column names as keys
keysrow: which row of the table holds the column names (starts a 0, NOT 1)
============================================================================
*/
function insertHashRow(table, data, keysrow){
var insertArray = [];
var idKey= {};
var keyId= {};
var range = "A"+(keysrow+1).toString() +":"+(keysrow+1).toString();
tableMetaData = table
.getActiveSheet()
.getRange(range)
.getValues();
for (var k = 0; k < tableMetaData[0].length; k++) {
var key = tableMetaData[0][k];
// key is text, k is number
if(key.trim() == ""){
continue;
}
insertArray.push("");
idKey[k] = key;
keyId[key] = k;
}
datakeys = Object.keys(data);
for(var i = 0; i < datakeys.length; i++){
var key = datakeys[i];
var k = keyId[key];
insertArray[k] = data[key];
}
table.getActiveSheet().appendRow(insertArray);
}
/*
============================ UPDATEHASHROW =================================
update a row in a sheet. Use column names as keys.
table: the google sheets object
data: the row, with column names as keys. Missing columns will be updated to
blank, NOT left alone.
keysrow: which row of the table holds the column names (starts a 0, NOT 1)
updateKey: object {key: column Name of identifying key of row to update
(eg 'NetId'), value : value for that cell in that row (eg 'dhu3')
============================================================================
*/
function updateHashRow(table, data, keysrow, updateKey){
Logger.log("updating2");
var insertArray = [];
var idKey= {};
var keyId= {};
var range = "A"+(keysrow+1).toString() +":"+(keysrow+1).toString();
tableMetaData = table
.getActiveSheet()
.getRange(range)
.getValues();
for (var k = 0; k < tableMetaData[0].length; k++) {
var key = tableMetaData[0][k];
// key is text, k is number
if(key.trim() == ""){
continue;
}
insertArray.push("");
idKey[k] = key;
keyId[key] = k;
}
datakeys = Object.keys(data);
for(var i = 0; i < datakeys.length; i++){
var key = datakeys[i];
var k = keyId[key];
insertArray[k] = data[key];
}
var index = findRowNumForQuery(table, keysrow, keysrow + 1, function(row){
if(row[updateKey.key] == updateKey.value){
return true;
}else{
return false;
}
});
var toDelete = index + 1;
if(index){
table.getActiveSheet().deleteRow(toDelete);
}
table.getActiveSheet().appendRow(insertArray);
return index;
}
function findRowNumForQuery(table, keysRow, startRow, queryFunction){
var tableData = table.getActiveSheet().getDataRange().getValues();
var data = dataIntoHashRows(tableData, keysRow, startRow).data;
for (var i = 0; i < data.length; i++) {
var res = queryFunction(data[i]);
if(res == true){
return i + startRow;
}
}
return false;
}
function getImageUrl(imagename){
var results = PicasaApp.find(imagename);
if(results.length > 0){
return results[0].getUrl();
}
return false;
}