forked from HeinzBaumann/GoogleAppScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongodbToSheet.gs
269 lines (248 loc) · 10.8 KB
/
MongodbToSheet.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
//
// Read from MongoDB and write out into the 3 different sheets
//
function importFromMongoDb() {
var getData = UrlFetchApp.fetch('https://eu-central-1.aws.data.mongodb-api.com/app/gsheettomongo-jqpnesh/endpoint/MongoToSheet').getContentText();
var jsonObj = JSON.parse(getData);
Logger.log(jsonObj.length + " records found and starting to import...");
// scan the json and write into the 3 different sheets: purposes, stacks and data taxonomies
// scan through the array, process lanuage by langauge
var langIdArray = [];
var proceed = true;
var rowNum = 1;
var cellCnt = 1;
var shIndex = SpreadsheetApp.getActiveSpreadsheet().getSheets().length;
var k = 1;
var m = 0;
var recordsImported = 0
for (i = 0; i < jsonObj.length; i++) {
proceed = true;
// check for the language and whether it is a duplicate
if (langIdArray.length > 0) {
for (j = 0; j < langIdArray.length; j++) {
// found a duplicate (we print a warning and ignore that entry)
if (langIdArray[j] === jsonObj[i].langId) {
Logger.log("Warning: Duplicate langauge " + jsonObj[i].langId + " found. Record will be ignored.");
proceed = false;
}
}
}
if (proceed) {
recordsImported++;
langIdArray[i] = jsonObj[i].langId;
// Purpose, special purpose, features & special features
//
// language
// type
// Id
// title
// description
// illustration[0]
// illustration[1]
var purposeSheetName = "masterPurposesV2.2_NEW";
var sh1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(purposeSheetName);
if (sh1 === null) { // create sheet
sh1 = SpreadsheetApp.getActive().insertSheet(purposeSheetName, shIndex++);
}
// write the header into the sheet
rowNum = 1;
if (sh1.getRange(rowNum,1).isBlank()) {
sh1.getRange(rowNum,1, 1, 8).setFontWeight("bold");
sh1.getRange(rowNum,1).setValue("language");
sh1.getRange(rowNum,2).setValue("type");
sh1.getRange(rowNum,3).setValue("id");
sh1.getRange(rowNum,4).setValue("title");
sh1.getRange(rowNum,5).setValue("description");
sh1.getRange(rowNum,6).setValue("illustration[0]");
sh1.getRange(rowNum,7).setValue("illustration[1]");
}
// get last rowNum and set our start row to populate the content
rowNum = sh1.getLastRow();
rowNum++;
// purposes
for (k = 1; k < 12; k++) {
sh1.getRange(rowNum, 1).setValue(jsonObj[i].langId);
sh1.getRange(rowNum, 2).setValue("purpose");
sh1.getRange(rowNum, 3).setValue(jsonObj[i].purposes[k].id);
sh1.getRange(rowNum, 4).setValue(jsonObj[i].purposes[k].name);
sh1.getRange(rowNum, 5).setValue(jsonObj[i].purposes[k].description);
sh1.getRange(rowNum, 6).setValue(jsonObj[i].purposes[k].illustrations[0]);
if (jsonObj[i].purposes[k].illustrations.length > 1)
sh1.getRange(rowNum, 7).setValue(jsonObj[i].purposes[k].illustrations[1]);
rowNum++;
}
// special purposes
for (k = 1; k < 3; k++) {
sh1.getRange(rowNum, 1).setValue(jsonObj[i].langId);
sh1.getRange(rowNum, 2).setValue("specialPurpose");
sh1.getRange(rowNum, 3).setValue(jsonObj[i].specialPurposes[k].id);
sh1.getRange(rowNum, 4).setValue(jsonObj[i].specialPurposes[k].name);
sh1.getRange(rowNum, 5).setValue(jsonObj[i].specialPurposes[k].description);
sh1.getRange(rowNum, 6).setValue(jsonObj[i].specialPurposes[k].illustrations[0]);
if (jsonObj[i].specialPurposes[k].illustrations.length > 1)
sh1.getRange(rowNum, 7).setValue(jsonObj[i].specialPurposes[k].illustrations[1]);
rowNum++;
}
// features
for (k = 1; k < 4; k++) {
sh1.getRange(rowNum, 1).setValue(jsonObj[i].langId);
sh1.getRange(rowNum, 2).setValue("feature");
sh1.getRange(rowNum, 3).setValue(jsonObj[i].features[k].id);
sh1.getRange(rowNum, 4).setValue(jsonObj[i].features[k].name);
sh1.getRange(rowNum, 5).setValue(jsonObj[i].features[k].description);
sh1.getRange(rowNum, 6).setValue(jsonObj[i].features[k].illustrations[0]);
if (jsonObj[i].features[k].illustrations.length > 1)
sh1.getRange(rowNum, 7).setValue(jsonObj[i].features[k].illustrations[1]);
rowNum++;
}
// special features
for (k = 1; k < 3; k++) {
sh1.getRange(rowNum, 1).setValue(jsonObj[i].langId);
sh1.getRange(rowNum, 2).setValue("specialFeature");
sh1.getRange(rowNum, 3).setValue(jsonObj[i].specialFeatures[k].id);
sh1.getRange(rowNum, 4).setValue(jsonObj[i].specialFeatures[k].name);
sh1.getRange(rowNum, 5).setValue(jsonObj[i].specialFeatures[k].description);
sh1.getRange(rowNum, 6).setValue(jsonObj[i].specialFeatures[k].illustrations[0]);
if (jsonObj[i].specialFeatures[k].illustrations.length > 1)
sh1.getRange(rowNum, 7).setValue(jsonObj[i].specialFeatures[k].illustrations[1]);
rowNum++;
}
// stacks
// language id
// name
// description
// v2.consent.1 v2.consent.2 v2.consent.3 v2.consent.4 v2.consent.5 v2.consent.6 v2.consent.7
// v2.consent.8 v2.consent.9 v2.consent.10 v2.consent.11
// v2.specialFeature.1 v2.specialFeature.2
var stackSheetName = "masterStacksV2.2_NEW";
var sh2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(stackSheetName);
if (sh2 === null) { // create sheet
sh2 = SpreadsheetApp.getActive().insertSheet(stackSheetName, shIndex++);
}
// write the header into the sheet
rowNum = 1;
if (sh2.getRange(rowNum,1).isBlank()) {
sh2.getRange(rowNum,1, 1, 17).setFontWeight("bold");
sh2.getRange(rowNum,1).setValue("language");
sh2.getRange(rowNum,2).setValue("id");
sh2.getRange(rowNum,3).setValue("name");
sh2.getRange(rowNum,4).setValue("description");
sh2.getRange(rowNum,5).setValue("v2.consent.1");
sh2.getRange(rowNum,6).setValue("v2.consent.2");
sh2.getRange(rowNum,7).setValue("v2.consent.3");
sh2.getRange(rowNum,8).setValue("v2.consent.4");
sh2.getRange(rowNum,9).setValue("v2.consent.5");
sh2.getRange(rowNum,10).setValue("v2.consent.6");
sh2.getRange(rowNum,11).setValue("v2.consent.7");
sh2.getRange(rowNum,12).setValue("v2.consent.8");
sh2.getRange(rowNum,13).setValue("v2.consent.9");
sh2.getRange(rowNum,14).setValue("v2.consent.10");
sh2.getRange(rowNum,15).setValue("v2.consent.11");
sh2.getRange(rowNum,16).setValue("v2.specialFeature.1");
sh2.getRange(rowNum,17).setValue("v2.specialFeature.2");
}
// get last rowNum and set our start row to populate the content
rowNum = sh2.getLastRow();
rowNum++;
for (k = 1; k < 46; k++) {
sh2.getRange(rowNum, 1).setValue(jsonObj[i].langId);
sh2.getRange(rowNum, 2).setValue(jsonObj[i].stacks[k].id);
sh2.getRange(rowNum, 3).setValue(jsonObj[i].stacks[k].name);
sh2.getRange(rowNum, 4).setValue(jsonObj[i].stacks[k].description);
// convert the purpose and special feature object and write into the individual cells
if (jsonObj[i].stacks[k].purposes.length != 0) {
for (m = 0; m < jsonObj[i].stacks[k].purposes.length; m++) {
switch (jsonObj[i].stacks[k].purposes[m]) {
case 1:
sh2.getRange(rowNum, 5).setValue("X");
break;
case 2:
sh2.getRange(rowNum, 6).setValue("X");
break;
case 3:
sh2.getRange(rowNum, 7).setValue("X");
break;
case 4:
sh2.getRange(rowNum, 8).setValue("X");
break;
case 5:
sh2.getRange(rowNum, 9).setValue("X");
break;
case 6:
sh2.getRange(rowNum, 10).setValue("X");
break;
case 7:
sh2.getRange(rowNum, 11).setValue("X");
break;
case 8:
sh2.getRange(rowNum, 12).setValue("X");
break;
case 9:
sh2.getRange(rowNum, 13).setValue("X");
break;
case 10:
sh2.getRange(rowNum, 14).setValue("X");
break;
case 11:
sh2.getRange(rowNum, 15).setValue("X");
break;
default:
Logger.log("Error: value out of range");
break;
}
}
}
if (jsonObj[i].stacks[k].specialFeatures.length != 0) {
for (m = 0; m < jsonObj[i].stacks[k].specialFeatures.length; m++) {
switch (jsonObj[i].stacks[k].specialFeatures[m]) {
case 1:
sh2.getRange(rowNum, 16).setValue("X");
break;
case 2:
sh2.getRange(rowNum, 17).setValue("X");
break;
default:
Logger.log("Error: value out of range");
break;
}
}
}
rowNum++;
}
// end of stack
// DataTaxonomies
// column 1 - langId
// column 2 - id
// column 3 - categories
// column 4 - description
// column 5 - vendorGuidance
var taxonomySheetName = "masterDataTaxonomyV2.2_NEW";
var sh3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(taxonomySheetName);
if (sh3 === null) { // create sheet
sh3 = SpreadsheetApp.getActive().insertSheet(taxonomySheetName, shIndex);
}
// write header into the sheet
rowNum = 1;
if (sh3.getRange(rowNum,1).isBlank()) {
sh3.getRange(rowNum,1, 1, 5).setFontWeight("bold");
sh3.getRange(rowNum,1).setValue("LangId");
sh3.getRange(rowNum,2).setValue("Id");
sh3.getRange(rowNum,3).setValue("Categories");
sh3.getRange(rowNum,4).setValue("Description");
sh3.getRange(rowNum,5).setValue("VendorGuidance");
}
// get last rowNum and set our start row to populate the content
rowNum = sh3.getLastRow();
rowNum++;
for (k = 1; k < 12; k++) {
sh3.getRange(rowNum, 1).setValue(jsonObj[i].langId);
sh3.getRange(rowNum, 2).setValue(jsonObj[i].dataCategories[k].id);
sh3.getRange(rowNum, 3).setValue(jsonObj[i].dataCategories[k].name);
sh3.getRange(rowNum++, 4).setValue(jsonObj[i].dataCategories[k].description);
// sh3.getRange(rowNum, 5).setValue(jsonObj[i].dataCategories[k].vendorGuidance); // enable when we output the vendorGuidance
}
// end of masterDataTaxonomy
}
} // end of jsonObj.length for loop
Logger.log("All Done. Imported " + recordsImported + " of " + jsonObj.length + ". Please check the sheets with suffix _NEW.")
}