-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.gs
207 lines (177 loc) · 5.85 KB
/
Parser.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
// Generate all form options to a json file
function generateFormData() {
var formId = '1RggzXqU33Zf3M6wXLX2X5rj41MHwh-OCgBsW62wfk2g'; // Insert your form id here
var outputDir = 'Labs'; // Insert your drive folder
var outputFilename = 'output.json'; // Insert your filename
var form = FormApp.openById(formId);
var formData = {};
formData.id = form.getId();
formData.title = form.getTitle();
formData.description = form.getDescription();
formData.confirmationMessage = form.getConfirmationMessage();
formData.requiresLogin = form.requiresLogin();
formData.collectsEmail = form.collectsEmail();
formData.isAcceptingResponses = form.isAcceptingResponses();
formData.canEditResponse = form.canEditResponse();
formData.hasLimitOneResponsePerUser = form.hasLimitOneResponsePerUser();
formData.hasRespondAgainLink = form.hasRespondAgainLink();
formData.isPublishingSummary = form.isPublishingSummary();
formData.hasProgressBar = form.hasProgressBar();
formData.destinationId = form.getDestinationId();
formData.destinationType = form.getDestinationType().toString();
formData.editUrl = form.getEditUrl();
formData.publishedUrl = form.getPublishedUrl();
formData.summaryUrl = form.getSummaryUrl();
formData.items = [];
var sectionCount = 0;
formData.items[sectionCount] = [];
var items = form.getItems();
for(var i = 0; i < items.length; i++) {
// Set basic properties of item
var item = {
id: items[i].getId(),
index: items[i].getIndex(),
type: items[i].getType().toString(),
title: items[i].getTitle(),
description: items[i].getHelpText()
};
// Cast Instance according to each Item Type
var instance = null;
if(item.type == 'PAGE_BREAK') {
instance = items[i].asPageBreakItem();
}
else if(item.type == 'SECTION_HEADER') {
instance = items[i].asSectionHeaderItem();
}
else if(item.type == 'TEXT') {
instance = items[i].asTextItem();
}
else if(item.type == 'PARAGRAPH_TEXT') {
instance = items[i].asParagraphTextItem();
}
else if(item.type == 'MULTIPLE_CHOICE') {
instance = items[i].asMultipleChoiceItem();
}
else if(item.type == 'CHECKBOX') {
instance = items[i].asCheckboxItem();
}
else if(item.type == 'LIST') {
instance = items[i].asListItem();
}
// else if(item.type == 'FILE_UPLOAD') {
// instance = items[i].as...Item();
// }
else if(item.type == 'CHECKBOX_GRID') {
instance = items[i].asCheckboxGridItem();
}
else if(item.type == 'GRID') {
instance = items[i].asGridItem();
}
else if(item.type == 'SCALE') {
instance = items[i].asScaleItem();
}
else if(item.type == 'DATE') {
instance = items[i].asDateItem();
}
else if(item.type == 'DATETIME') {
instance = items[i].asDateTimeItem();
}
else if(item.type == 'DURATION') {
instance = items[i].asDurationItem();
}
else if(item.type == 'TIME') {
instance = items[i].asTimeItem();
}
else if(item.type == 'IMAGE') {
instance = items[i].asImageItem();
}
else if(item.type == 'VIDEO') {
instance = items[i].asVideoItem();
}
if(item.type == 'PAGE_BREAK') {
var goToPage = instance.getGoToPage();
if(goToPage) {
item.page = {};
item.page.goToId = goToPage.getId();
}
}
if(item.type == 'MULTIPLE_CHOICE' ||
item.type == 'CHECKBOX' ||
item.type == 'LIST') {
if(typeof instance.hasOtherOption === 'function') {
item.hasOtherOption = instance.hasOtherOption();
}
item.choices = [];
var choices = instance.getChoices();
for(var j = 0; j < choices.length; j++) {
item.choices.push(choices[j].getValue());
}
}
if(item.type == 'CHECKBOX_GRID' ||
item.type == 'GRID') {
item.grid = {};
item.grid.rows = instance.getRows();
item.grid.cols = instance.getColumns();
}
if(item.type == 'SCALE') {
item.scale = {};
item.scale.from = instance.getLowerBound();
item.scale.to = instance.getUpperBound();
item.scale.leftLabel = instance.getLeftLabel();
item.scale.rightLabel = instance.getRightLabel();
}
if(item.type == 'DATE' ||
item.type == 'DATETIME') {
item.date = {};
item.date.includesYear = instance.includesYear();
}
if(item.type == 'IMAGE') {
item.image = {};
item.image.alignment = instance.getAlignment().toString();
item.image.width = instance.getWidth();
if(instance.getImage()) {
item.image.blob = instance.getImage().getDataAsString();
}
}
if(item.type == 'VIDEO') {
item.video = {};
item.video.alignment = instance.getAlignment().toString();
item.video.width = instance.getWidth();
}
if(instance) {
if(typeof instance.isRequired === 'function') {
item.isRequired = instance.isRequired();
}
}
if(item.type == 'PAGE_BREAK') {
sectionCount++;
formData.items[sectionCount] = [];
}
formData.items[sectionCount].push(item);
}
var folder = getFolder(outputDir);
if (folder != false) {
updateFile(folder, outputFilename, JSON.stringify(formData));
}
}
// Create or get existing folder
// [One level deep only]
function getFolder (foldername) {
try {
// folder is unique, so we can get first element of iterator
var folder = DriveApp.getFoldersByName(foldername).next();
return folder;
} catch(e) {
return false;
}
}
// Creates or replaces an existing file
function updateFile (folder, filename, data) {
try {
// filename is unique, so we can get first element of iterator
var file = folder.getFilesByName(filename).next();
file.setContent(data);
} catch(e) {
folder.createFile(filename, data);
}
}