-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHtmlFormApp.js
More file actions
293 lines (282 loc) · 10.8 KB
/
HtmlFormApp.js
File metadata and controls
293 lines (282 loc) · 10.8 KB
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
/**
* GitHub https://github.com/tanaikech/HtmlFormApp<br>
* Library name
* @type {string}
* @const {string}
* @readonly
*/
var appName = "HtmlFormApp";
/**
* Append HTML form data to Spreadsheet.<br>
* @param {object} object Object including formData returned from HtmlFormObjectParserForGoogleAppsScript_js.
* @param {number} rowNumber The row number to edit (1-based index).
* @return {object} object Object including Class Spreadsheet, Class Sheet, Class Range of the row put the values, values
*/
function appendFormData(object, row = null) {
return new HtmlFormApp(object).appendFormData(row);
}
;
(function (r) {
var HtmlFormApp;
HtmlFormApp = (function () {
var checkHeader, checkObject, convObj, setHyperLinks;
class HtmlFormApp {
constructor(obj_) {
this.name = appName;
this.obj = {};
checkObject.call(this, obj_);
}
appendFormData(row_) {
var header, hyperlinks, keyConvertObj, range, values;
header = checkHeader.call(this);
keyConvertObj = Object.entries(this.formObject).reduce((o, [k, v]) => {
o[k.trim().toLocaleLowerCase()] = v;
return o;
}, {});
hyperlinks = false;
values = [
header.map((hv) => {
var h;
if (this.headerConversion && (!this.ignoreHeader || this.ignoreHeader === false)) {
hv = this.headerConversion[hv] || hv;
}
h = hv.trim().toLocaleLowerCase();
if (h === "date") {
return new Date();
} else if (keyConvertObj[h] && keyConvertObj[h].some((e) => {
return e.files;
})) {
return (keyConvertObj[h].reduce((ar,
e) => {
if (e.files && e.files.length > 0) {
hyperlinks = true;
e.files.forEach(({ bytes,
mimeType,
filename }) => {
return ar.push(this.obj.folder.createFile(Utilities.newBlob(bytes,
mimeType,
filename)).getUrl());
});
} else {
ar.push("");
}
return ar;
},
[])).join(this.delimiter);
} else if (keyConvertObj[h] && (keyConvertObj[h][0].type === "checkbox" || keyConvertObj[h][0].type === "radio")) {
if (this.choiceFormat && this.choiceFormat === true) {
return (keyConvertObj[h].map(({ checked,
value }) => {
return `${value}(${checked === true ? "checked" : "unchecked"})`;
})).join(this.delimiter);
}
return (keyConvertObj[h].reduce((arr,
{ checked,
value }) => {
if (checked === true) {
arr.push(value);
}
return arr;
},
[])).join(this.delimiter);
}
if (keyConvertObj[h]) {
if (keyConvertObj[h].length === 1) {
return keyConvertObj[h][0].value;
} else {
return (keyConvertObj[h].map(({ value }) => {
return value;
})).join(this.delimiter);
}
}
return "";
})
];
if (this.lastRow === 0 && (!this.ignoreHeader || this.ignoreHeader === false)) {
values.unshift(header);
}
range = this.sheet.getRange(row_ || this.lastRow + 1, 1, values.length, values[0].length);
range.setValues(values);
if (hyperlinks === true) {
setHyperLinks.call(this, range, values);
}
return {
spreadsheet: this.obj.spreadsheet,
sheet: this.sheet,
range: range,
values: values
};
}
};
HtmlFormApp.name = appName;
checkHeader = function () {
var header, hedFromSheet, oo;
header = ["Date", ...this.orderOfFormObject];
if (this.lastRow > 0 && (!this.ignoreHeader || this.ignoreHeader === false)) {
hedFromSheet = this.sheet.getRange(1, 1, 1, this.sheet.getLastColumn()).getValues()[0];
oo = this.orderOfFormObject.reduce((o, e) => {
o[e.toLocaleLowerCase()] = true;
return o;
}, {});
if (hedFromSheet.some((e) => {
return oo[e.toLocaleLowerCase()];
})) {
header = hedFromSheet;
}
}
return header;
};
checkObject = function (obj_) {
var _;
if (!obj_) {
throw new Error("Object for using this library is not given.");
}
if (!obj_.hasOwnProperty("formData")) {
throw new Error("'formData' is not included in your object.");
}
this.obj = obj_;
if (this.obj.formData.hasOwnProperty("parameters") && this.obj.formData.hasOwnProperty("parameter")) {
if (this.obj.formData.hasOwnProperty("postData") && this.obj.formData.postData.hasOwnProperty("contents") && this.obj.formData.postData.hasOwnProperty("name") && this.obj.formData.postData.name === "postData") {
if (this.obj.formData.postData.contents === "") {
throw new Error(`Request body is not found.`);
}
try {
this.obj.formData = JSON.parse(this.obj.formData.postData.contents);
} catch (error) {
_ = error;
this.obj.formData = convObj.call(this, this.obj.formData.parameters);
}
} else if (!this.obj.formData.parameter.hasOwnProperty("formData")) {
this.obj.formData = Object.fromEntries(Object.entries(this.obj.formData.parameters).map(([k, v]) => {
return [
k,
v.map((e) => {
return {
value: e
};
})
];
}));
} else {
if (!this.obj.formData.parameter.hasOwnProperty("formData") || this.obj.formData.parameter.hasOwnProperty("formData") === "") {
throw new Error(`Query parameter is not found.`);
}
this.obj.formData = JSON.parse(this.obj.formData.parameter.formData);
}
}
this.obj.folder = this.obj.folderId && this.obj.folderId !== "" ? DriveApp.getFolderById(this.obj.folderId) : DriveApp.getRootFolder();
if (!Object.values(this.obj.formData).every((e) => {
return Array.isArray(e);
})) {
this.obj.formData = convObj.call(this, this.obj.formData);
}
if (this.obj.spreadsheetId && this.obj.spreadsheetId !== "") {
this.obj.spreadsheet = SpreadsheetApp.openById(this.obj.spreadsheetId);
this.obj.new = false;
} else {
this.obj.spreadsheet = SpreadsheetApp.create("spreadsheetCreatedByHtmlFormApp");
this.obj.new = true;
}
if (!this.obj.spreadsheetId) {
this.obj.spreadsheetId = this.obj.spreadsheet.getId();
}
if (this.obj.sheetName && this.obj.sheetName !== "") {
this.obj.sheet = this.obj.spreadsheet.getSheetByName(this.obj.sheetName);
if (!this.obj.sheet) {
throw new Error(`Sheet of ${this.obj.sheetName} is not found.`);
}
}
if (this.obj.sheetId && this.obj.sheetId !== "") {
this.obj.sheet = this.obj.spreadsheet.getSheets().find((e) => {
return e.getSheetId() === this.obj.sheetId;
});
if (!this.obj.sheet) {
throw new Error(`Sheet of ${this.obj.sheetId} is not found.`);
}
}
if (!this.obj.sheetName && !this.obj.sheetId) {
this.obj.sheet = this.obj.spreadsheet.getSheets()[0];
}
this.sheet = this.obj.sheet;
this.formObject = this.obj.formData;
this.orderOfFormObject = this.formObject.orderOfFormObject || Object.keys(this.formObject);
this.headerConversion = this.obj.headerConversion;
this.ignoreHeader = this.obj.ignoreHeader;
this.lastRow = this.sheet.getLastRow();
this.choiceFormat = this.obj.choiceFormat;
this.delimiter = this.obj.delimiterOfMultipleAnswers || ",";
this.valueAsRaw = this.obj.valueAsRaw || false;
if (!this.valueAsRaw) {
return Object.entries(this.formObject).forEach(([k, v]) => {
var t;
if (k !== "orderOfFormObject" && Array.isArray(v) && v[0].hasOwnProperty("type")) {
t = v[0].type.toLowerCase();
if (t === "number") {
v.forEach((e, i) => {
return this.formObject[k][i].value = Number(e.value);
});
}
if (t === "date") {
v.forEach((e, i) => {
return this.formObject[k][i].value = new Date(e.value + "T00:00:00");
});
}
if (t === "datetime-local") {
return v.forEach((e, i) => {
return this.formObject[k][i].value = new Date(e.value);
});
}
}
});
}
};
convObj = function (obj_) {
return Object.fromEntries(Object.entries(obj_).map(([k, v]) => {
if (typeof v === "object" && v.hasOwnProperty("contents") && v.hasOwnProperty("length") && v.hasOwnProperty("name") && v.hasOwnProperty("type")) {
v = this.obj.folder.createFile(v).getUrl();
}
return [
k,
Array.isArray(v) ? v.map((e) => {
return {
value: e
};
}) : [
{
value: v
}
]
];
}));
};
setHyperLinks = function (range, values) {
var re;
re = new RegExp(this.delimiter, "g");
return values.forEach((r, i) => {
return r.forEach((c, j) => {
var indexes, offset, rich, t;
if (c && /https?:\/\//i.test(c)) {
t = [...c.matchAll(re)];
rich = SpreadsheetApp.newRichTextValue().setText(c);
if (t.length === 0) {
rich.setLinkUrl(c);
} else if (c.includes(this.delimiter)) {
offset = 0;
t.forEach((e) => {
var indexes;
indexes = [offset, e.index];
rich.setLinkUrl(...indexes, c.slice(...indexes));
return offset = e.index + 1;
});
indexes = [t.pop().index + 1, c.length];
rich.setLinkUrl(...indexes, c.slice(...indexes));
}
return range.offset(i, j).setRichTextValue(rich.build());
}
});
});
};
return HtmlFormApp;
}).call(this);
return r.HtmlFormApp = HtmlFormApp;
})(this);