forked from subhranshuchoudhury/Google-Sheet-Quiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.gs
30 lines (25 loc) · 879 Bytes
/
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
function doGet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var headers = data.shift();
var jsonArray = data.map(function (row) {
var questionData = {};
var questionOptionData = {};
headers.forEach(function (header, index) {
if (row[index] === "") {
return; // Skip empty values in the row
} else if (header.toLowerCase().includes("option")) {
questionOptionData[header] = row[index];
} else {
questionData[header] = row[index];
}
});
if (Object.keys(questionOptionData).length > 0) {
questionData.options = questionOptionData;
}
return questionData;
});
var json = JSON.stringify(jsonArray);
Logger.log(json);
return ContentService.createTextOutput(json).setMimeType(ContentService.MimeType.JSON);
}