-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
344 lines (273 loc) · 9.8 KB
/
Code.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
function doGet() {
return HtmlService.createHtmlOutputFromFile('Interface');
}
function getOrganisationData() {
const query = '"Apps Script" stars:">=100"';
const url = 'https://sandbox.dissco.tech/api/v1/organisation/tuples' + '?sort=stars' + '&q=' + encodeURIComponent(query);
const response = UrlFetchApp.fetch(url, { 'muteHttpExceptions': true });
const data = JSON.parse(response.getContentText());
let check = [];
let numberCheck = {};
let returnArray = [];
/* Check for double entities */
for (key in data) {
if (!check.includes(data[key]['name'])) {
check.push(data[key]['name']);
numberCheck[data[key]['name']] = 1;
} else {
numberCheck[data[key]['name']] += 1;
data[key] = data[key]['name'] + ' (' + numberCheck[data[key]['name']] + ')';
}
returnArray[data[key]['name']] = data[key];
}
return returnArray;
}
function postOrganisationData(formData, callback) {
/* For each response to form, insert new record */
const query = '"Apps Script" stars:">=100"';
let url = 'https://sandbox.dissco.tech/api/v1/organisation/document' + '?sort=stars' + '&q=' + encodeURIComponent(query);
let options = {
'method': 'POST',
'contentType': 'application/json'
}
for (let response in formData) {
let document = {
'organisation_id': formData[response]['organisation_id'],
'document_id': formData[response]['form_id'],
'document_title': formData[response]['form_title'],
'document_type': formData[response]['form_type'],
'document': formData[response]['questions']
};
options['payload'] = JSON.stringify(document);
UrlFetchApp.fetch(url, options);
}
callback(true);
}
function fillMonkeyForm(auth, survey_id, page = 1) {
/* Check if page is greater than zero */
if (!page > 0) {
page = 1;
}
const organisationData = getOrganisationData();
/* Find form data */
// findMonkeyForm(auth, process);
findMonkeyPage(auth, survey_id, page, process_further);
/* Process form id, find page data */
// function process(survey_data) {
// let survey_id = survey_data.id;
// }
/* Process page data, insert organisation multiple choice */
function process_further(survey_id, page_data) {
/* Set multiple choice options */
let choices = [];
for (key in organisationData) {
choices.push({
"text": key
})
}
/* Add field to form */
let data = {
"family": "matrix",
"subtype": "menu",
"answers": {
"rows": [{
"text": "Organisation:"
}],
"cols": [{
"text": "Choose",
"choices": choices
}]
},
"headings": [
{
"heading": "Choose a DiSSCo Organisation:"
}
]
}
let options = {
'method': 'post',
'headers': { 'Authorization': 'bearer ' + auth },
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
'payload': JSON.stringify(data),
};
let response = UrlFetchApp.fetch('https://api.surveymonkey.com/v3/surveys/' + survey_id + '/pages/' + page_data.id + '/questions', options);
}
}
function getMonkeyForms(auth) {
let options = {
'method': 'GET',
'headers': { 'Authorization': 'Bearer ' + auth },
'contentType': 'application/json'
};
let response = UrlFetchApp.fetch('https://api.surveymonkey.com/v3/surveys', options);
let data = JSON.parse(response.getContentText());
return data;
}
function findMonkeyForm(auth, survey_id, callback) {
let survey_data;
let options = {
'method': 'GET',
'headers': { 'Authorization': 'bearer ' + auth },
'contentType': 'application/json'
};
let result = UrlFetchApp.fetch('https://api.surveymonkey.net/v3/surveys/' + survey_id, options);
if (result.getResponseCode() == 200) {
let params = JSON.parse(result.getContentText());
}
callback(survey_data);
}
function findMonkeyPage(auth, survey_id, page = 1, callback) {
let options = {
'method': 'get',
'headers': { 'Authorization': 'bearer ' + auth },
'contentType': 'application/json'
};
let response = UrlFetchApp.fetch('https://api.surveymonkey.com/v3/surveys/' + survey_id + '/pages', options);
let params = JSON.parse(response.getContentText());
let page_data = params.data[(page - 1)];
callback(survey_id, page_data);
}
function fillGoogleForm(form_id) {
/* Receiving organisations from API */
const organisationData = getOrganisationData();
form_id = '1FrPOiVcSh4TSxtPcO-Il3pbdTO8noW-AgyhNMbMA86Y';
/* Opening form */
let form = FormApp.openById(form_id);
/* Setting drop down item with API */
let o = form.addListItem();
choices = [];
for (key in organisationData) {
choices.push(o.createChoice(key));
}
o.setChoices(choices);
o.setTitle('Choose a DiSSCo Organisation:');
}
/* Read data from forms */
function exportGoogleForm(form_id) {
/* Retrieve organisations with ROR id */
let organisationsData = getOrganisationData();
let form = FormApp.openById(form_id);
let responses = {};
let formTitle = form.getTitle();
let formType = 'Google Forms';
var formResponses = form.getResponses();
for (var i = 0; i < formResponses.length; i++) {
responses['response: ' + i] = {
'questions': {}
};
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
/* Set questions and answers */
var itemResponse = itemResponses[j];
if (itemResponse.getItem().getTitle() == 'Choose a DiSSCo Organisation:') {
let ror = organisationsData[itemResponse.getResponse()]['ror'];
if (!responses['response: ' + i]['organisation_id']) {
responses['response: ' + i]['organisation_id'] = ror;
}
} else {
if (!responses['response: ' + i]['questions'][itemResponse.getItem().getTitle()]) {
responses['response: ' + i]['questions'][itemResponse.getItem().getTitle()] = [itemResponse.getResponse()];
} else {
responses['response: ' + i]['questions'][itemResponse.getItem().getTitle()].push(itemResponse.getResponse());
}
}
/* Set general form info */
responses['response: ' + i]['form_id'] = form_id;
responses['response: ' + i]['form_type'] = formType;
responses['response: ' + i]['form_title'] = formTitle;
}
if (!responses['response: ' + i]['organisation_id']) {
delete responses['response: ' + i];
}
}
/* Insert records into #document store# (database) */
postOrganisationData(responses, process);
function process(check) {
return check;
}
}
function exportMonkeyForm(survey_id, auth, survey_title) {
let options = {
'method': 'GET',
'headers': { 'Authorization': 'bearer ' + auth },
'contentType': 'application/json'
};
let result = UrlFetchApp.fetch('https://api.surveymonkey.net/v3/surveys/' + survey_id + '/responses/bulk?simple=true', options);
if (result.getResponseCode() == 200) {
let params = JSON.parse(result.getContentText());
let data = params['data'];
let organisationsData = getOrganisationData();
let responses = {};
let formType = 'SurveyMonkey';
let i = 0;
for (key in data) {
responses['response: ' + i] = {
'questions': {}
};
let responseIndicator = 'response: ' + i;
let respondent = data[key];
for (key in respondent['pages']) {
let page = respondent['pages'][key];
for (key in page['questions']) {
/* Set questions and answers */
let question = page['questions'][key];
if (question.heading == 'Choose a DiSSCo Organisation:') {
let organisationText = question['answers'][0]['simple_text'].replace('Organisation: | Choose | ', '');
let ror = organisationsData[organisationText]['ror'];
if (!responses[responseIndicator]['organisation_id']) {
responses[responseIndicator]['organisation_id'] = ror;
}
} else {
if (!responses[responseIndicator]['questions'][question['heading']]) {
let answersList;
if (question['answers'].length > 1) {
answersList = [];
question['answers'].forEach((answer) => {
answersList.push(answer['simple_text']);
});
} else {
answersList = question['answers'][0]['simple_text'];
}
responses[responseIndicator]['questions'][question['heading']] = [answersList];
} else {
let answersList;
if (question['answers'].length > 1) {
answersList = [];
question['answers'].forEach((answer) => {
answersList.push(answer['simple_text']);
});
} else {
answersList = question['answers'][0]['simple_text'];
}
responses[responseIndicator]['questions'][question['heading']].push(answersList);
}
}
}
}
/* Set general form info */
responses[responseIndicator]['form_id'] = survey_id;
responses[responseIndicator]['form_type'] = formType;
responses[responseIndicator]['form_title'] = survey_title;
if (!responses['response: ' + i]['organisation_id']) {
delete responses['response: ' + i];
}
i++;
}
postOrganisationData(responses, process);
function process(check) {
return check;
}
}
}
function readMonkeyForm(auth, survey_id) {
let options = {
'method': 'post',
'headers': { 'Authorization': 'bearer ' + auth },
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
'payload': JSON.stringify(data),
};
let response = UrlFetchApp.fetch('https://api.surveymonkey.com/v3/surveys/' + survey_id + '/responses');
}