-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGoogleAppsScriptLibraries.gs
More file actions
312 lines (229 loc) · 8.17 KB
/
GoogleAppsScriptLibraries.gs
File metadata and controls
312 lines (229 loc) · 8.17 KB
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
// JSHint - 20200116
/* jshint asi: true */
(function() {"use strict"})()
// GoogleAppsScriptLibraries.gs
// ============================
//
// Dev: AndrewRoberts.net
//
// External interface to this script - all of the event handlers.
//
// This files contains all of the event handlers, plus miscellaneous functions
// not worthy of their own files yet
//
// The filename is prepended with _API as the Github chrome extension won't
// push a file with the same name as the project.
var Log_
// Public event handlers
// ---------------------
//
// All external event handlers need to be top-level function calls; they can't
// be part of an object, and to ensure they are all processed similarily
// for things like logging and error handling, they all go through
// errorHandler_(). These can be called from custom menus, web apps,
// triggers, etc
//
// The main functionality of a call is in a function with the same name but
// post-fixed with an underscore (to indicate it is private to the script)
//
// For debug, rather than production builds, lower level functions are exposed
// in the menu
var EVENT_HANDLERS_ = {
// Name onError Message Main Functionality
// ---- --------------- ------------------
onRefresh: ['onRefresh()', 'Failed to refresh', onRefresh_],
}
function onRefresh(args) {return eventHandler_(EVENT_HANDLERS_.onRefresh, args)}
// Private Functions
// =================
// General
// -------
/**
* All external function calls should call this to ensure standard
* processing - logging, errors, etc - is always done.
*
* @param {Array} config:
* [0] {Function} prefunction
* [1] {String} eventName
* [2] {String} onErrorMessage
* [3] {Function} mainFunction
*
* @param {Object} args The argument passed to the top-level event handler
*/
function eventHandler_(config, args) {
try {
var userEmail = Session.getActiveUser().getEmail()
Log_ = BBLog.getLog({
level: DEBUG_LOG_LEVEL_,
displayFunctionNames: DEBUG_LOG_DISPLAY_FUNCTION_NAMES_,
sheetId: TEST_SHEET_ID_
})
Log_.info('Handling ' + config[0] + ' from ' + (userEmail || 'unknown email') + ' (' + SCRIPT_NAME + ' ' + SCRIPT_VERSION + ')')
// Call the main function
return config[2](args)
} catch (error) {
var assertConfig = {
error: error,
userMessage: config[1],
log: Log_,
handleError: HANDLE_ERROR_,
sendErrorEmail: SEND_ERROR_EMAIL_,
emailAddress: ADMIN_EMAIL_ADDRESS_,
scriptName: SCRIPT_NAME,
scriptVersion: SCRIPT_VERSION,
}
Assert.handleError(assertConfig)
}
} // eventHandler_()
// Private event handlers
// ----------------------
function onRefresh_() {
var spreadsheet = Utils_.getSpreadsheet()
var masterSheet = spreadsheet.getSheetByName(MASTER_SHEET_NAME_)
var output = getMasterLibraryList()
writeLibraryList(output)
return
// Private Functions
// -----------------
function getMasterLibraryList() {
/*
[Library 1 Name] : {
[Key 1]: [Value 1]
[Key 1]: [Value 1]
.
.
.
},
[Library 2 Name] : {
},
.
.
.
*/
var output = {}
SHEETS_.forEach(function(config) {
var sheetName = config.name
var sheet = spreadsheet.getSheetByName(sheetName)
var input = sheet.getDataRange().getValues()
var nameOffset = getNameOffset()
input.shift() // Remove the header
input.forEach(function(row) {
if (row[0] === '') {
return
}
var libraryName = row[nameOffset]
if (!output.hasOwnProperty(libraryName)) {
output[libraryName] = {}
}
config.columns.forEach(function(column) {
var inputOffset = column.offset
var nextOutputColumnName = column.map
if (nextOutputColumnName === '') {
return
}
var nextInputValue = row[inputOffset]
if (nextOutputColumnName !== 'Name') {
output[libraryName][nextOutputColumnName] = nextInputValue
}
})
})
// Private Functions
// -----------------
function getNameOffset() {
var nameOffset = null
config.columns.some(function(column) {
if (column.name === 'Name' || column.name === 'name' || column.name === 'Library Name') {
nameOffset = column.offset
return true
}
})
if (nameOffset === null) {
throw new Error('Can not find the library names in "' + sheetName + '"')
}
return nameOffset
} // onRefresh_.getMasterLibraryList.getNameOffset()
Log_.finest('output: %s', output)
}) // for each sheet
return addJSONLists(output)
// Private Functions
// -----------------
/**
* Map the next library object key stored in the JSON to the
* appropriate "master" list key
*/
function addJSONLists(output) {
JSON_.forEach(function(config) {
var url = config.url
var jSONkeys = config.keys
var json = JSON.parse(UrlFetchApp.fetch(url).getContentText())
json.forEach(function(jSONlibrary) {
var libraryName = jSONlibrary[jSONkeys[0].name]
if (output[libraryName] === undefined) {
output[libraryName] = {}
}
for (var key in jSONkeys) {
if (!jSONkeys.hasOwnProperty(key)) {
continue
}
var nextJSONKey = jSONkeys[key]
var nextValue = jSONlibrary[nextJSONKey.name]
if (nextValue instanceof Array) {
nextValue = nextValue.join()
}
output[libraryName][nextJSONKey.map] = nextValue
}
})
})
return output
} // // onRefresh_.getMasterLibraryList().addJSONLists()
} // onRefresh_.getMasterLibraryList()
function writeLibraryList(output) {
var data = []
var maxOffset = 0
for (var library in output) {
if (!output.hasOwnProperty(library)) {
continue
}
var nextRow = [library] // Initialise the row with the name
if (!output.hasOwnProperty(library)) {
continue
}
var nextLibrary = output[library]
for (var columnName in nextLibrary) {
if (!nextLibrary.hasOwnProperty(columnName)) {
continue
}
var offsetIntoMasterList = OFFSETS_[columnName]
maxOffset = (offsetIntoMasterList > maxOffset) ? offsetIntoMasterList : maxOffset
nextRow[offsetIntoMasterList] = nextLibrary[columnName]
}
data.push(nextRow)
} // for each library
Log_.fine('maxOffset: ' + maxOffset)
// Replace any null values - use "for" as some indexes may be empty
data.forEach(function(row, rowIndex) {
for (var columnIndex = 0; columnIndex <= maxOffset; columnIndex++) {
var cell = data[rowIndex][columnIndex]
if (cell === null || cell === undefined) {
data[rowIndex][columnIndex] = ''
}
}
})
data = data.sort(sortFunction)
Log_.finest('data: %s', data)
masterSheet.getRange(2, 1, masterSheet.getLastRow(), OFFSETS_.LAST_OFFSET + 1).clearContent()
masterSheet.getRange(2, 1, data.length, maxOffset + 1).setValues(data)
return
// Private Functions
// -----------------
function sortFunction(a, b) {
var la = a[0].toLowerCase()
var lb = b[0].toLowerCase()
if (la === lb) {
return 0;
} else {
return (la < lb) ? -1 : 1;
}
} // onRefresh_.writeLibraryList.sortFunction()
} // onRefresh_.writeLibraryList()
} // onRefresh_()