-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsheets2wbtemplate.js
412 lines (349 loc) · 13.5 KB
/
gsheets2wbtemplate.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
//forked from https://gist.github.com/pamelafox/1878143 and modified to create WB JSON template from Google sheets
var FORMAT_ONELINE = "One-line";
var FORMAT_MULTILINE = "Multi-line";
var FORMAT_PRETTY = "Pretty";
var LANGUAGE_JS = "JavaScript";
var LANGUAGE_PYTHON = "Python";
var STRUCTURE_LIST = "List";
var STRUCTURE_HASH = 'Hash (keyed by "id" column)';
/* Defaults for this particular spreadsheet, change as desired */
var DEFAULT_FORMAT = FORMAT_PRETTY;
var DEFAULT_LANGUAGE = LANGUAGE_JS;
var DEFAULT_STRUCTURE = STRUCTURE_LIST;
function onOpen() {
//creating new menu
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [
// {name: "Export JSON for this sheet", functionName: "exportSheet"},
{ name: "Create WB template", functionName: "createWbTemplate" },
];
ss.addMenu("WB tools", menuEntries);
}
//Result output window
function makeTextBox(app, name) {
var textArea = app
.createTextArea()
.setWidth("100%")
.setHeight("100%")
.setId(name)
.setName(name);
return textArea;
}
//----------------------------new variant of template generation
function createWbTemplate(e) {
console.log("exportTest");
var convertOptions = getExportOptions(e);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("main");
convertOptions.structure = STRUCTURE_LIST;
var templateObj = getRowsData_(ss.getSheetByName("main"), convertOptions)[0];
// console.log(templateObj);
// console.log(typeof(templateObj));
var sheets = ss.getSheets(); //Sheet[] — An array of all the sheets in the spreadsheet.
var sheetsData = {};
for (var i = 1; i < sheets.length; i++) {
var sheet = sheets[i];
var sheetName = sheet.getName();
convertOptions.structure = STRUCTURE_LIST;
var rowsData = getRowsData_(sheet, convertOptions);
//если нет заполненных строк кроме заголовка, то секцию не добаляем в шаблон
if (Object.keys(rowsData).length == 0) continue;
if ((sheetName != "main") && (sheetName != "device") && (sheetName != "en") && (sheetName != "ru")) {
sheetsData[sheetName] = rowsData;
}
}
templateObj.device = sheetsData;
//console.log('templateObj = ' + templateObj);
//Adding translations
convertOptions.structure = STRUCTURE_LIST;
templateObj.translations = {};
sheet = ss.getSheetByName("en");
templateObj.translations.en = getColumnsData_(sheet, sheet.getRange(sheet.getFrozenRows() + 1, 2, sheet.getMaxRows(), sheet.getMaxColumns()), 1)[0];
sheet = ss.getSheetByName("ru");
templateObj.translations.ru = getColumnsData_(sheet, sheet.getRange(sheet.getFrozenRows() + 1, 2, sheet.getMaxRows(), sheet.getMaxColumns()), 1)[0];
//Creating JSON wb template from JS object
var templateJSON = makeJSON_(templateObj, getExportOptions(e));
var outputWindowHeader = "Result";
//Trying to convert from JSON to JS object to check correctness of templete
try {
var templateJSONParsed = JSON.parse(templateJSON);
templateJSON = JSON.stringify(templateJSONParsed, null, 4);
outputWindowHeader = "JSON created successfully";
} catch (err) {
console.log(err.name); // ReferenceError
console.log(err.message); // lalala is not defined
console.log(err.stack); // ReferenceError: lalala is not defined at (...стек вызовов)
// Можем также просто вывести ошибку целиком
// Ошибка приводится к строке вида "name: message"
console.log(err);
outputWindowHeader = "Exported with ERRORS!!! Check JSON data!!!";
}
displayText_(templateJSON, outputWindowHeader);
}
function correctParId(sheetParameters) {
var parIdRange = sheetParameters.getRange(
sheetParameters.getFrozenRows() + 1,
1,
sheetParameters.getMaxRows(),
sheetParameters.getMaxColumns()
);
// console.log(sheetParameters.getFrozenRows() + 1);
// console.log(sheetParameters.getMaxRows());
// console.log(parIdRange[0]);
//console.log(getColumnsData_(sheetParameters, parIdRange, 1));
}
function getExportOptions(e) {
var options = {};
options.language = (e && e.parameter.language) || DEFAULT_LANGUAGE;
options.format = (e && e.parameter.format) || DEFAULT_FORMAT;
options.structure = (e && e.parameter.structure) || DEFAULT_STRUCTURE;
var cache = CacheService.getPublicCache();
cache.put("language", options.language);
cache.put("format", options.format);
cache.put("structure", options.structure);
Logger.log(options);
return options;
}
function makeJSON_(object, options) {
if (options.format == FORMAT_PRETTY) {
var jsonString = JSON.stringify(object, null, 4);
} else if (options.format == FORMAT_MULTILINE) {
var jsonString = Utilities.jsonStringify(object);
jsonString = jsonString.replace(/},/gi, "},\n");
jsonString = jsonString.replace(/":\[{"/gi, '":\n[{"');
jsonString = jsonString.replace(/}\],/gi, "}],\n");
} else {
var jsonString = Utilities.jsonStringify(object);
}
if (options.language == LANGUAGE_PYTHON) {
// add unicode markers
jsonString = jsonString.replace(/"([a-zA-Z]*)":\s+"/gi, '"$1": u"');
}
//console.log("JSON string", jsonString);
return jsonString;
}
//View result in JSON
function displayText_(text, windowHeader) {
var output = HtmlService.createHtmlOutput(
"<textarea style='width:100%;' rows='50'>" + text + "</textarea>"
);
output.setWidth(1000);
output.setHeight(850);
SpreadsheetApp.getUi().showModalDialog(output, windowHeader);
}
// getRowsData iterates row by row in the input range and returns an array of objects.
// Each object contains all the data for a given row, indexed by its normalized column name.
// Arguments:
// - sheet: the sheet object that contains the data to be processed
// - range: the exact range of cells where the data is stored
// - columnHeadersRowIndex: specifies the row number where the column names are stored.
// This argument is optional and it defaults to the row immediately above range;
// Returns an Array of objects.
function getRowsData_(sheet, options) {
var headersRange = sheet.getRange(1, 1, sheet.getFrozenRows(), sheet.getMaxColumns());
var headers = headersRange.getValues()[0];
var dataRange = sheet.getRange(
sheet.getFrozenRows() + 1,
1,
sheet.getMaxRows(),
sheet.getMaxColumns()
);
var objects = getObjects_(dataRange.getValues(), normalizeHeaders_(headers));
if (options.structure == STRUCTURE_HASH) {
var objectsById = {};
objects.forEach(function (object) {
objectsById[object.id] = object;
delete objectsById[object.id].id;
});
return objectsById;
} else {
return objects;
}
}
// getColumnsData iterates column by column in the input range and returns an array of objects.
// Each object contains all the data for a given column, indexed by its normalized row name.
// Arguments:
// - sheet: the sheet object that contains the data to be processed
// - range: the exact range of cells where the data is stored
// - rowHeadersColumnIndex: specifies the column number where the row names are stored.
// This argument is optional and it defaults to the column immediately left of the range;
// Returns an Array of objects.
function getColumnsData_(sheet, range, rowHeadersColumnIndex) {
rowHeadersColumnIndex = rowHeadersColumnIndex || range.getColumnIndex() - 1;
var headersTmp = sheet
.getRange(range.getRow(), rowHeadersColumnIndex, range.getNumRows(), 1)
.getValues();
var headers = headersTmp; //normalizeHeaders_(arrayTranspose_(headersTmp)[0]);
return getObjects_(arrayTranspose_(range.getValues()), headers);
}
// For every row of data in data, generates an object that contains the data. Names of
// object fields are defined in keys.
// Arguments:
// - data: JavaScript 2d array
// - keys: Array of Strings that define the property names for the objects to create
function getObjects_(data, keys) {
var objects = [];
for (var i = 0; i < data.length; ++i) {
var object = {};
var hasData = false;
for (var j = 0; j < data[i].length; ++j) {
var cellData = data[i][j];
cellData = normalizeDataCell_(cellData, keys[j]);
if (isCellEmpty_(cellData)) {
continue;
}
object[keys[j]] = cellData;
hasData = true;
}
if (hasData) {
objects.push(object);
}
}
return objects;
}
// Returns an Array of normalized Strings.
// Arguments:
// - headers: Array of Strings to normalize
function normalizeHeaders_(headers) {
var keys = [];
for (var i = 0; i < headers.length; ++i) {
var key = normalizeHeaderCell_(headers[i]);
if (key.length > 0) {
keys.push(key);
}
}
return keys;
}
// Normalizes a string, by removing all alphanumeric characters and using mixed case
// to separate words. The output will always start with a lower case letter.
// This function is designed to produce JavaScript object property names.
// Arguments:
// - header: string to normalize
// Examples:
// "First Name" -> "firstName"
// "Market Cap (millions) -> "marketCapMillions
// "1 number at the beginning is ignored" -> "numberAtTheBeginningIsIgnored"
function normalizeHeaderCell_(header) {
var key = ""; //key - key field of JSON
var upperCase = false;
for (var i = 0; i < header.length; ++i) {
var letter = header[i];
if (letter == " " && key.length > 0) {
upperCase = true;
continue;
}
//Allowed symbols are letters, numbers, _
if (!isAlnum_(letter) && letter != "_") {
continue;
}
if (key.length == 0 && isDigit_(letter)) {
continue; // first character must be a letter
}
if (upperCase) {
upperCase = false;
key += letter.toUpperCase();
} else {
key += letter.toLowerCase();
}
}
return key;
}
function normalizeDataCell_(cellRawData, key) {
// var cellData = ""; //cellData - data field of JSON
//убираем лишние пробелы из содержимого ячейки (перед данными, дублирующие, в конце строки)
if (typeof cellRawData != "number") {
cellData = cellRawData.toString().trim();
} else {
cellData = cellRawData;
}
// var upperCase = false;
// for (var i = 0; i < cellRawData.length; ++i) {
// var letter = cellRawData[i];
// var previousLetter = cellRawData[i - 1];
// if ((( (previousLetter == " ") && (cellData.length > 0)) || (i == 0)) && (key == "name")) {
// upperCase = true;
// // continue;
// }
// // //Allowed symbols are letters, numbers, _
// // if ( (!isAlnum_(letter)) && (letter != "_") ) {
// // continue;
// // }
// // if (key.length == 0 && isDigit_(letter)) {
// // continue; // first character must be a letter
// // }
// if (upperCase) {
// upperCase = false;
// cellData += letter.toUpperCase();
// } else {
// cellData += letter.toLowerCase();
// }
// }
return cellData;
}
// Returns true if the cell where cellData was read from is empty.
// Arguments:
// - cellData: string
function isCellEmpty_(cellData) {
return typeof cellData == "string" && cellData == "";
}
// Returns true if the character char is alphabetical or number, false otherwise.
function isAlnum_(char) {
return (char >= "A" && char <= "Z") || (char >= "a" && char <= "z") || isDigit_(char);
}
// Returns true if the character char is a digit, false otherwise.
function isDigit_(char) {
return char >= "0" && char <= "9";
}
// Given a JavaScript 2d Array, this function returns the transposed table.
// Arguments:
// - data: JavaScript 2d Array
// Returns a JavaScript 2d Array
// Example: arrayTranspose([[1,2,3],[4,5,6]]) returns [[1,4],[2,5],[3,6]].
function arrayTranspose_(data) {
if (data.length == 0 || data[0].length == 0) {
return null;
}
var ret = [];
for (var i = 0; i < data[0].length; ++i) {
ret.push([]);
}
for (var i = 0; i < data.length; ++i) {
for (var j = 0; j < data[i].length; ++j) {
ret[j][i] = data[i][j];
}
}
return ret;
}
//Unused functions
// function makeLabel(app, text, id) {
// var lb = app.createLabel(text);
// if (id) lb.setId(id);
// return lb;
// }
// function makeListBox(app, name, items) {
// var listBox = app.createListBox().setId(name).setName(name);
// listBox.setVisibleItemCount(1);
// var cache = CacheService.getPublicCache();
// var selectedValue = cache.get(name);
// Logger.log(selectedValue);
// for (var i = 0; i < items.length; i++) {
// listBox.addItem(items[i]);
// if (items[1] == selectedValue) {
// listBox.setSelectedIndex(i);
// }
// }
// return listBox;
// }
// function makeButton(app, parent, name, callback) {
// var button = app.createButton(name);
// app.add(button);
// var handler = app.createServerClickHandler(callback).addCallbackElement(parent);;
// button.addClickHandler(handler);
// return button;
// }
// function replacer(key, value) {
// if (typeof value == "\[") {
// return value.toString()
// }
// return value
// }