forked from google/bulkdozer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SheetDAO.js
433 lines (355 loc) · 10.2 KB
/
SheetDAO.js
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
/***************************************************************************
*
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Note that these code samples being shared are not official Google
* products and are not formally supported.
*
***************************************************************************/
/**
* Handles all interactions with the sheet, such as reading data, writing data,
* clearing ranges, etc.
*/
var SheetDAO = function() {
// PRIVATE FIELDS
// Allows private methods to access this object
var that = this;
// Defines the maximun number of columns to consider in a sheet
const MAX_SHEET_WIDTH = 52;
// Column names to facilitate index to a1 notation syntax translation
var columns = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD',
'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR',
'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'];
this.columns = columns;
// Object that holds sheet cache in memory
var sheetCache = {};
// PRIVATE METHODS
/**
* Turns a feed item into a sheet row based on the fiels in the header
*
* params:
* header: array of strings representing the headers of the sheet
* item: the feed item to write, a dictionary with fields that match the
* header fields
*
* returns: array of values to be written to the sheet
*/
function dictToRow(header, item) {
var result = [];
for(var i = 0; i < header.length; i++) {
var value = item[header[i]];
if(value !== undefined && value !== null) {
result.push(item[header[i]].toString());
} else {
result.push(null);
}
}
return result;
}
/**
* Gets the header row of a sheet
*
* params:
* sheetName: name of the sheet to get the header
*
* returns: Array representing each column of the header row of the identified
* sheet
*/
function getHeader(sheetName) {
var result = cacheGet(sheetName, 'header');
if(!result) {
var sheet = getSheet(sheetName);
var header = sheet.getSheetValues(1, 1, 1, MAX_SHEET_WIDTH)[0];
var result = [];
for(var i = 0; i < header.length && header.slice(i).join(''); i++) {
result.push(header[i]);
}
if(!sheetCache[sheetName]) {
sheetCache[sheetName] = {};
}
cachePut(sheetName, 'header', result);
}
return result;
}
/**
* Gets data from a sheet
*
* params:
* sheetName: name of the sheet from which to get the data
*/
function getData(sheetName) {
var result = cacheGet(sheetName, 'data');
if(!result) {
var header = getHeader(sheetName);
var data;
data = that.getValues(sheetName, 'A2:' + columns[header.length + 1]);
result = [];
for(var i = 0; i < data.length; i++) {
var row = data[i];
if(row.join('')) {
result.push(row);
} else {
break;
}
}
cachePut(sheetName, 'data', result);
}
return result;
}
/**
* Returns a list with all sheets, uses cache for performance reasons
*
* returns: Array of sheets
*/
function getSheets() {
var result = cacheGet('all', 'sheets');
if(!result) {
result = SpreadsheetApp.getActive().getSheets();
cachePut('all', 'sheets', result);
}
return result;
}
/**
* Gets the sheet object that represents a given sheet
*
* params:
* sheetName: Name of the sheet to return
*
* returns: The sheet object
*/
function getSheet(sheetName) {
var result = cacheGet(sheetName, 'sheet');
if(!result) {
var sheets = getSheets();
for(var i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
if(sheet.getName() === sheetName) {
result = sheet;
break;
}
}
cachePut(sheetName, 'sheet', result);
}
return result;
}
/**
* Fetches data from the cache
*
* params:
* sheetName: The cache is sheet based, this is the name of the sheet used to
* identify the cache
* key: key within the sheet cache to fetch
*
* returns: The value from the cache if there is a value that matches the
* sheetName and key, null otherwise
*/
function cacheGet(sheetName, key) {
if(sheetCache[sheetName] && sheetCache[sheetName][key]) {
return sheetCache[sheetName][key];
}
return null;
}
/**
* Puts data into the sheet cache
*
* params:
* sheetName: The cache is sheet based, this is the name of the sheet used to
* identify the cache
* key: key within the sheet cache to put
* value: value to put in the cache
*/
function cachePut(sheetName, key, value) {
if(!sheetCache[sheetName]) {
sheetCache[sheetName] = {};
}
sheetCache[sheetName][key] = value;
}
/**
* Returns a range object representing the specified range. This method will
* cache the range for performance reasons avoiding multiple calls to the API
*
* params:
* sheetName: name of the tab
* range: A1 notation of the range
*
* returns: Range object representing the identified range
*/
function getRange(sheetName, range) {
var result = cacheGet(sheetName, range);
if(!result) {
var sheet = getSheet(sheetName);
if(sheet) {
result = sheet.getRange(range);
cachePut(sheetName, range, result);
}
}
return result;
}
/**
* Turns a row into a dictionary based on the field specification of a header
* row
*
* params:
* header: array representing the header of the sheet
* row: row with data
*
* returns: dictionary with each field in the header and data from row
*/
function rowToDict(header, row) {
var result = {};
for(var i = 0; i < header.length; i++) {
result[header[i]] = row[i];
}
return result;
}
// PUBLIC METHODS
this.isQA = function() {
return getSheet('QA') != null;
}
/**
* Clears a range in the sheet
*
* params:
* sheetName: Name of the sheet to clear
* range: range within the sheet to clear
*/
this.clear = function(sheetName, range) {
var range = getRange(sheetName, range);
if(range) {
range.clear();
}
}
/**
* Opens the specified tab
*
* params:
* sheetName: The name of the tab to open
*/
this.goToTab = function(sheetName) {
console.log('going to tab ' + sheetName);
SpreadsheetApp.setActiveSheet(SpreadsheetApp.getActive().getSheetByName(sheetName));
}
/**
* Turns a sheet into a dictionary with each field name being the column header
* which is assumed to be row 1 of the sheet
*
* params:
* sheetName: name of the sheet to transform
* noCache: boolean, if true the cache isn't used, otherwise cache is used
*/
this.sheetToDict = function(sheetName, noCache) {
if(!this.tabExists(sheetName)) {
return [];
}
var result = noCache ? null : cacheGet(sheetName, 'dict');
if(!result) {
var header = getHeader(sheetName);
var data = getData(sheetName);
var result = [];
for(var i = 0; i < data.length && data[i].join('') != ''; i++) {
result.push(rowToDict(header, data[i]));
}
cachePut(sheetName, 'dict');
}
return result;
}
/**
* Checks if a tab exists
*
* params:
* tabName: name of the tab to check
*
* returns: true if exists, false otherwise
*/
this.tabExists = function(tabName) {
return getSheet(tabName) ? true : false;
}
/**
* Gets data from a particular range in a spreadsheet
*
* params:
* sheetName: Name of the sheet (tab) from which to read the data
* range: Range in the sheet from which to get the data
*
* returns: array of arrays with the values identified
*/
this.getValues = function(sheetName, range) {
var result = cacheGet(sheetName, 'values' + range);
if(!result) {
var range = getRange(sheetName, range);
var result = _retry(function() {
return range.getValues();
}, 3, 2 * 1000);
cachePut(sheetName, 'values' + range, result);
}
return result;
}
/**
* Returns a single value from a cell in the sheet
*
* params:
* sheetName: Name of the sheet to read from
* range: a1 notation of a specific cell
*/
this.getValue = function(sheetName, range) {
var values = this.getValues(sheetName, range);
if(values.length > 0 && values[0].length > 0) {
return values[0][0];
}
return null;
}
/**
* Write values to a particular sheet and range
*
* params:
* sheetName: Name of the sheet where to write
* range: range in which to write
* values: array of arrays containing values to write
*/
this.setValues = function(sheetName, range, values) {
var sheetRange = getRange(sheetName, range);
sheetRange.clear();
var response = _retry(function() {
sheetRange.setValues(values);
}, 3, 2 * 1000);
}
/**
* Writes a feed to a sheet
*
* params:
* sheetName: name of the sheet in which to write the feed
* items: list of items in the feed format to write to the sheet
*/
this.dictToSheet = function(sheetName, items) {
var header = getHeader(sheetName);
this.clear(sheetName, '!A2:AZ');
var rows = [];
for(var i = 0; i < items.length; i++) {
rows.push(dictToRow(header, items[i]));
}
if(rows.length > 0) {
this.setValues(sheetName, '!A2:' + columns[rows[0].length - 1] + (rows.length + 1), rows);
}
}
}
// Singleton implementation for the sheet dao
var sheetDAO;
function getSheetDAO() {
if(!sheetDAO) {
sheetDAO = new SheetDAO();
}
return sheetDAO;
}