-
Notifications
You must be signed in to change notification settings - Fork 1
/
sheetToMongodb.gs
564 lines (503 loc) · 19.8 KB
/
sheetToMongodb.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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//
// exportToMongoDB
//
// By Heinz Baumann - May 2024
//
// This function will export either a specified language or all the translation to the
// translation collection.
//
// initial implementation: all translations, specific language
// updates existing entries based on the langId or creates a new entry if no langId is not found
//
// Things that need to be donw:
// - clean up code and remove all the unncessary Debug stuff
// - add production access point and triggers
var debug = true; // Debug logs enabled
var jsonString = ""; // global jsonString
function exportToMongoDB(strLanguage = null) {
var langStrArray = [];
var tempLang = null;
// if we have no given language we read all the languages from the sheet and
// stuff it into the langStrArray.
if (strLanguage !== null){
langStrArray[0] = strLanguage;
} else {
langStrArray = getLanguages();
}
// process and write language after language
tempLang = null;
for (var k = 0; k < langStrArray.length; k++) {
// write the language and the date stamp into the jsonString
if (tempLang === null) {
tempLang = langStrArray[k];
// write the language section header
jsonString = '{ "langId": "' + tempLang + '",';
// add the lastUpdated field
const d = new Date();
jsonString += '"lastUpdated": "' + d.toISOString().substring(0,19) + "Z" + '",';
}
// process the purposes
processPurposes(tempLang);
// process the stacks
processStacks(tempLang);
// finally process the taxonomy
processTaxonomies(tempLang);
// terminate the language section and reset the tempLang string
jsonString += "}";
/*
// Debug output to check for specific JSON string errors
Logger.log(jsonString.substring(5000, 6000));
Logger.log(jsonString.substring(5865, 5885) + " chatAt: " + jsonString.charAt(5878) + " charCodeAt: " + jsonString.charCodeAt(5879) + " " + jsonString.charCodeAt(5879));
*/
// Transmit to MongoDB
// For the transaction we need to stringify the data and stuff in into a name: value
// pair, where name is 'jstring' and the value is the JSON string
// We will add another name value pair to handle updates where the name is id and the
// value is the actual mongo id returned from the original transaction.
/*
// debug data
var jStr = {
langId: "en",
purposes: {
1: {
id: 1,
name: "Store and/or access information on a device",
description: "Cookies, device or similiar...",
illustration: [
"Most purposes explained..."
]
}
}
}
*/
var jStr = JSON.parse(jsonString);
var params = {
'method': 'post',
'muteHttpExceptions': true,
'payload': { recordId: tempLang, jstring: JSON.stringify(jStr) }
// 'payload': JSON.stringify(formData)
};
// write to mongoDB
var result = UrlFetchApp.fetch('https://eu-central-1.aws.data.mongodb-api.com/app/gsheettomongo-jqpnesh/endpoint/SheetToMongo', params);
Logger.log("DEBUG: LangId=" +tempLang + " result: " + result);
// Write the result value back into column H in the first row of the respective langauge section in the purpose sheet
// Debug:
// Logger.log("Posted to MongoDB: " + result.getContentText("UTF-8").replaceAll(/"/g,"").replaceAll(/\\/g, ""));
var sheetName = "masterPurposesV2.2";
var sheetRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getDataRange();
var firstRaw = findFirstLanguageRow(sheetRange.getValues(), tempLang)
sheetRange.getCell(firstRaw, 8).setValue(result.getContentText("UTF-8").replaceAll(/"/g,"").replaceAll(/\\/g, ""));
jsonString = "";
tempLang = null;
}
}
//
// processPurposes(strLanguage)
//
function processPurposes(strLanguage) {
var sheetName = "masterPurposesV2.2";
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var sheetRange = sheet1.getDataRange();
var sheetValues = sheetRange.getValues();
var workingRange = null;
var workingValues = null;
var bPurposeSection = false;
var bSpecialPurposeSection = false;
var bFeatureSection = false;
var bSpecialFeatureSection = false;
if (strLanguage !== null){
var rangeArray = findRangeByValue(sheetValues, strLanguage);
workingRange = sheet1.getRange(rangeArray[0], rangeArray[1], rangeArray[2], rangeArray[3]);
} else {
Logger.log("Error: processPurposes - No language specified");
return;
}
workingValues = workingRange.getValues();
if (false /* debug */) { // debug
if (workingRange != null) {
Logger.log("rows found: " + workingRange.getNumRows());
Logger.log("columns found: " + workingRange.getNumColumns());
Logger.log("Range found: " + workingRange.getA1Notation());
if (workingValues != null) {
for (var i = 0; i < workingValues.length; i++)
workingValues[i].forEach((element) => Logger.log(element));
}
} else {
Logger.log("Value not found.");
}
} // debug
if (workingValues != null) {
for (var i = 0; i < workingValues.length; i++) {
// if we have a purpose
if (workingValues[i][1] === "purpose") {
if (!bPurposeSection) {
jsonString += '"purposes": {'; // purpose section
bPurposeSection = true;
}
jsonString += '"' + workingValues[i][2] + '": {'; // get the purpose id
jsonString += '"id": ' + workingValues[i][2] + ',';
jsonString += '"name": "' + workingValues[i][3].replace(/\n/g,"") + '",';
jsonString += '"description": "' + workingValues[i][4].replace(/\n/g,"") + '",';
jsonString += '"illustrations": ["' + workingValues[i][5].replace(/\n/g,"").replace(/"/g, "\\\"") + '"';
if (workingValues[i][6] !== "")
jsonString += ', "' + workingValues[i][6].replace(/\n/g,"").replace(/"/g, "\\\"") + '"]';
else
jsonString += ']';
jsonString += '}'
if (workingValues[i+1][1] === "purpose") { // there are more purposes
jsonString += ',';
} else { // we reached the end of the purpose section
jsonString += '}';
bPurposeSection = false;
}
} else if (workingValues[i][1] === "specialPurpose") { // special purposes
if (!bSpecialPurposeSection) {
jsonString += ', "specialPurposes": {'; // special purpose section
bSpecialPurposeSection = true;
}
jsonString += '"' + workingValues[i][2] + '": {'; // get the specialPurpose id
jsonString += '"id": ' + workingValues[i][2] + ',';
jsonString += '"name": "' + workingValues[i][3].replace(/\n/g,"") + '",';
jsonString += '"description": "' + workingValues[i][4].replace(/\n/g,"") + '",';
jsonString += '"illustrations": ["' + workingValues[i][5].replace(/\n/g,"").replace(/"/g, "\\\"") + '"';
if (workingValues[i][6] !== "")
jsonString += ', "' + workingValues[i][6].replace(/\n/g,"").replace(/"/g, "\\\"") + '"]';
else
jsonString += ']';
jsonString += '}'
if (workingValues[i+1][1] === "specialPurpose") { // there are more special purposes
jsonString += ',';
} else { // we reached the end of the special purpose section
jsonString += '}';
bSpecialPurposeSection = false;
}
} else if (workingValues[i][1] === "feature") { // features
if (!bFeatureSection) {
jsonString += ', "features": {' // feature section
bFeatureSection = true;
}
jsonString += '"' + workingValues[i][2] + '": {'; // get the feature id
jsonString += '"id": ' + workingValues[i][2] + ',';
jsonString += '"name": "' + workingValues[i][3].replace(/\n/g,"") + '",';
jsonString += '"description": "' + workingValues[i][4].replace(/\n/g,"") + '",';
jsonString += workingValues[i][5] !== "" ? '"illustrations": ["' + workingValues[i][5].replace(/\n/g,"").replace(/"/g, "\\\"") + '"' : '"illustrations": [ ';
if (workingValues[i][6] !== "")
jsonString += ', "' + workingValues[i][6].replace(/\n/g,"").replace(/"/g, "\\\"") + '"]';
else
jsonString += ']';
jsonString += '}'
if (workingValues[i+1][1] === "feature") { // there are more features
jsonString += ',';
} else { // we reached the end of the feature section
jsonString += '}';
bFeatureSection = false;
}
} else if (workingValues[i][1] === "specialFeature") { // special features
if (!bSpecialFeatureSection) {
jsonString += ', "specialFeatures": {' // special feature section
bSpecialFeatureSection = true;
}
jsonString += '"' + workingValues[i][2] + '": {'; // get the feature id
jsonString += '"id": ' + workingValues[i][2] + ',';
jsonString += '"name": "' + workingValues[i][3].replace(/\n/g,"") + '",';
jsonString += '"description": "' + workingValues[i][4].replace(/\n/g,"") + '",';
jsonString += workingValues[i][5] !== "" ? '"illustrations": ["' + workingValues[i][5].replace(/\n/g,"").replace(/"/g, "\\\"") + '"' : '"illustrations": [ ';
if (workingValues[i][6] !== "")
jsonString += ', "' + workingValues[i][6].replace(/\n/g,"").replace(/"/g, "\\\"") + '"]';
else
jsonString += ']';
jsonString += '}'
if (i+1 < workingValues.length && workingValues[i+1][1] === "specialFeature") { // there are more special features
jsonString += ',';
} else { // we reached the end of the feature section
jsonString += '}';
bSpecialFeatureSection = false;
}
}
}
}
}
//
// processStacks
// Switch the Stacks sheet
// Create a range for the language in hand
// Write the stack information into our json file
//
function processStacks(strLanguage) {
var sheetName = "masterStacksV2.2";
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var sheetRange = sheet1.getDataRange();
var sheetValues = sheetRange.getValues();
var workingRange = null;
var workingValues = null;
if (strLanguage !== null){
var rangeArray = findRangeByValue(sheetValues, strLanguage);
workingRange = sheet1.getRange(rangeArray[0], rangeArray[1], rangeArray[2], rangeArray[3]);
} else {
Logger.log("Error: processPurposes - No language specified");
return;
}
workingValues = workingRange.getValues();
if (workingValues != null) {
// write the section header
jsonString += ',"stacks": {';
var bfirstValueWritten = false;
for (var i = 0; i < workingValues.length; i++) {
jsonString += '"' + workingValues[i][1] + '": {'; // get the stack id
jsonString += '"id": ' + workingValues[i][1] + ',';
jsonString += '"purposes": [';
bfirstValueWritten = false;
for (var l = 4; l < 15; l++) {
if (workingValues[i][l] === "X") {
if (bfirstValueWritten) {
jsonString += ', ';
}
jsonString += l - 3;
bfirstValueWritten = true;
}
}
jsonString += '],';
jsonString += '"specialFeatures": [';
bfirstValueWritten = false;
for (var l = 15; l < 17; l++) {
if (workingValues[i][l] === "X") {
if (bfirstValueWritten) {
jsonString += ', ';
}
jsonString += l - 14;
bfirstValueWritten = true;
}
}
jsonString += '],';
jsonString += '"name": "' + workingValues[i][2].replace(/\n/g,"").replace(/\r/g,"") + '",';
jsonString += '"description": "' + workingValues[i][3].replace(/\n/g,"").replace(/\r/g,"") + '"';
jsonString += '}'
if (i+1 !== workingValues.length)
jsonString += ',';
}
// close the Stack section
jsonString += '}';
}
}
//
// processTaxonomies
// - create range for language in hand
// - witch the sheet and process the taxonomy
// - write the taxonomies into the jsonString
//
function processTaxonomies(strLanguage) {
var sheetName = "masterDataTaxonomyV2.2";
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var sheetRange = sheet1.getDataRange();
var sheetValues = sheetRange.getValues();
var workingRange = null;
var workingValues = null;
if (strLanguage === null){
Logger.log("Error: processPurposes - No language specified");
return;
}
var rangeArray = findRangeByValue(sheetValues, strLanguage);
if (rangeArray[0] === undefined) {
console.log("langId: " + strLanguage + " not found in Taxonomy sheet. Aborting.")
return;
}
workingRange = sheet1.getRange(rangeArray[0], rangeArray[1], rangeArray[2], rangeArray[3]);
workingValues = workingRange.getValues();
if (workingValues != null) {
// write the section header
jsonString += ',"dataCategories": {';
for (var i = 0; i < workingValues.length; i++) {
jsonString += '"' + workingValues[i][1] + '": {'; // get the dataCategories id
jsonString += '"id": ' + workingValues[i][1] + ',';
jsonString += '"name": "' + workingValues[i][2].replace(/\n/g,"") + '",';
jsonString += '"description": "' + workingValues[i][3].replace(/\n/g,"").replace(/\"/g,"\\\"") + '",';
jsonString += '"vendorGuidance": "' + workingValues[i][4].replace(/\n/g,"").replace(/\"/g,"\\\"") + '"';
jsonString += '}';
if (i+1 !== workingValues.length)
jsonString += ',';
}
// close the Stack section
jsonString += '}';
}
}
//
// Update translation in MongoDB by a specified language or all lanagauge in sheet
//
function updateRecordInMongoDB(strLanguage = null) {
var langStrArray = [];
var tempLang = null;
// if we have no given language we read all the languages from the sheet and
// stuff it into the langStrArray.
if (strLanguage !== null){
langStrArray[0] = strLanguage;
} else {
langStrArray = getLanguages();
}
// process and write language after language
tempLang = null;
for (var k = 0; k < langStrArray.length; k++) {
// write the language and the date stamp into the jsonString
if (tempLang === null) {
tempLang = langStrArray[k];
// write the language section header
jsonString = '{ "langId": "' + tempLang + '",';
// add the lastUpdated field
const d = new Date();
jsonString += '"lastUpdated": "' + d.toISOString().substring(0,19) + "Z" + '",';
}
// process the purposes
processPurposes(tempLang);
// process the stacks
processStacks(tempLang);
// finally process the taxonomy
processTaxonomies(tempLang);
// terminate the language section and reset the tempLang string
jsonString += "}";
// Transmit to MongoDB
var formData = JSON.parse(jsonString);
// find the mongo DB record id in the sheet
var sheetName = "masterPurposesV2.2";
var sheetRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getDataRange();
/*
We don';'t use the record id because that is not working through the MongoDB Script instead we look up the by language string
var cell1 = sheetRange.getCell(findFirstLanguageRow(sheetRange.getValues(), tempLang), 8);
if (cell1.isBlank()) {
Logger.log("Error no recordId found in sheet, proceed with a new export.");
break;
} else {
*/
if (findFirstLanguageRow(sheetRange.getValues(), tempLang) === -1) {
Logger.log("Error " + tempLang + " not found in sheet. Abort.");
break;
} else {
/*
var recordId = cell1.getValue();
Logger.log(recordId.toString());
*/
// we really should use the returned id but so far the _id is not working to query against in MongoDB
// so for now we use the language id
recordId = tempLang;
}
// setup the props to update
var params = {
'method': 'post',
'payload': { recordId: recordId.toString(), jstring: JSON.stringify(formData) }
};
// call the DB and update the record
var resultId = UrlFetchApp.fetch('https://eu-central-1.aws.data.mongodb-api.com/app/gsheettomongo-jqpnesh/endpoint/SheetToMongo', params);
// Write the result value back into column H in the first row of the respective langauge section in the purpose sheet
// Debug:
Logger.log("Posted to MongoDB: " + resultId.getContentText("UTF-8").replaceAll(/"/g,"").replaceAll(/\\/g, ""));
var sheetName = "masterPurposesV2.2";
var sheetRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getDataRange();
var firstRaw = findFirstLanguageRow(sheetRange.getValues(), strLanguage)
sheetRange.getCell(firstRaw, 8).setValue(resultId.getContentText("UTF-8").replaceAll(/"/g,"").replaceAll(/\\/g, ""));
jsonString = "";
tempLang = null;
}
}
//
// import new translation file by a specified language
//
function importNewTranslationToSheet(strLanguage) {
}
//
// Import a single or multiple new entries into the table
// Example: translation for a new Special Prupose 3
//
function importSingleEntryToSheet(strLanguage = null, sectionString, entryNumber) {
}
//
// GUI function
//
// Add a custom menu to execute the load to MongoDB
//
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('IAB DB Connectivity')
.addItem('Select a language to update...', 'menuItem1')
.addItem('Update all', 'menuItem2')
.addItem('Import Data from MongoDB', 'importFromMongoDb')
.addToUi();
}
// export the selected language
function menuItem1() {
const html = HtmlService.createHtmlOutputFromFile('page')
.setWidth(350)
.setHeight(150);
SpreadsheetApp.getUi().showModalDialog(html, 'Select a language to export')
}
function onSelectedItem(value) {
updateRecordInMongoDB(value);
SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp or FormApp. // need to style the error msg
.alert("Language translation for " + value + " exported.");
}
// export all
function menuItem2() {
updateRecordInMongoDB();
SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp or FormApp. // need to style the error msg
.alert('Exporting all languages!');
}
//
// Support and test functions
//
function myTest() {
exportToMongoDB("ar"); // "en"
}
function myTestUpdate() {
updateRecordInMongoDB("en");
}
function getLanguages() {
var sheetName = "masterPurposesV2.2";
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)
var values = sheet1.getRange(2,1, sheet1.getLastRow()).getValues();
var langStrArray = [];
var tempLang = null;
var j = 0;
for (var i = 0; i < values.length; i++) {
if (tempLang === null && values[i][0] !== "") {
langStrArray[j++] = tempLang = values[i][0];
} else if (i+1 < values.length && values[i+1][0] !== tempLang) {
tempLang = null;
}
}
return langStrArray;
}
function findRangeByValue(values, strLanguage) {
var rowArray = [];
var i = j = 0;
// var j = 0;
// First find the given language rows
for (i = 0; i < values.length; i++) {
if (values[i][0] === strLanguage) { // language is the first column
rowArray[j++] = i+1;
}
}
// Set the number of columns
var numCol = values[0].length+1;
// Find min and max row
var minRow = rowArray[0];
var maxRow = 0;
for (i = 0; i < rowArray.length; i++) {
if (rowArray[i] <= minRow)
minRow = rowArray[i];
}
for (i = 0; i < rowArray.length; i++) {
if (rowArray[i] >= maxRow)
maxRow = rowArray[i];
}
// return range array minRow, minCol, maxRow, minCol
return [minRow,
1,
maxRow + 1 - minRow,
numCol];
}
function findFirstLanguageRow(values, strLanguage) {
for (var i = 0; i < values.length; i++) {
if (values[i][0] === strLanguage) { // language is the first column
return i+1;
}
}
return -1;
}
// EOF