-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhtmlFormObjectParserForGoogleAppsScript_js.js
More file actions
151 lines (148 loc) · 5.53 KB
/
htmlFormObjectParserForGoogleAppsScript_js.js
File metadata and controls
151 lines (148 loc) · 5.53 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
/**
* GitHub https://github.com/tanaikech/HtmlFormObjectParserForGoogleAppsScript
_js<br>
* Run HtmlFormObjectParserForGoogleAppsScript_js. This is a Javascript library for sending the HTML form object to Google Apps Script using "google.script.run".<br>
* @param {Object} formObj Form object
* @param {String} fields Fields value. Default is "name,type,value,files,checked".
* @param {String} excludeTypes Exclude types. Default is "submit".
* @param {Boolean} includeOrder Include order of each input tag in form. Default is false. When this value is true, a property of "orderOfFormObject" is included in the returned object. This value is the order of HTML form object.
* @return {Object} Return Object
*/
function ParseFormObjectForGAS(formObj, fields, excludeTypes, includeOrder) {
return new HtmlFormObjectParserForGoogleAppsScript(
formObj,
fields,
excludeTypes,
includeOrder
).Do();
}
(function (r) {
var HtmlFormObjectParserForGoogleAppsScript;
HtmlFormObjectParserForGoogleAppsScript = function () {
class HtmlFormObjectParserForGoogleAppsScript {
constructor(obj_, fieldsValue_, excludeTypes_, includeOrder_) {
if (obj_.toString() !== "[object HTMLFormElement]") {
throw new Error("Invalid HTMLFormElement.");
}
this.formObj = Array.from(obj_);
if (this.formObj.length === 0) {
throw new Error("No properties in the inputted form object.");
}
if (!fieldsValue_ || fieldsValue_ === "") {
fieldsValue_ = "name,type,value,files,checked";
} else if (typeof fieldsValue_ !== "string") {
throw new Error(
'Invalid fields. Please set it like "name,type,value,files,checked"'
);
}
if (!excludeTypes_ || excludeTypes_ === "") {
excludeTypes_ = "submit";
} else if (excludeTypes_ === "-") {
excludeTypes_ = "";
} else if (typeof excludeTypes_ !== "string") {
throw new Error('Invalid exclude types. Please set it like "submit"');
}
this.fields = fieldsValue_.split(",").map((g) => {
return g.trim();
});
this.excludeTypes = excludeTypes_.split(",").map((g) => {
return g.trim();
});
this.includeOrder = includeOrder_ || false;
}
async Do() {
var ar, e, initObj, orderOfFormObject;
try {
ar = await Promise.all(
this.formObj.map((obj, i) => {
return new Promise(async (res, rej) => {
var files, htmlObj, temp;
htmlObj = {};
for (i in obj) {
if (typeof obj[i] !== "function" && obj[i] !== null) {
htmlObj[i] = obj[i];
}
}
temp = {
[obj.name || `noName${i + 1}`]: htmlObj,
};
if (obj.type === "file") {
files = obj.files;
temp[obj.name].files = {};
temp[obj.name].files = await (async (files) => {
return await Promise.all(
[...files].map((file) => {
return new Promise((resolve, reject) => {
var fr;
fr = new FileReader();
fr.onload = (f) => {
return resolve({
filename: file.name,
mimeType: file.type,
bytes: [...new Int8Array(f.target.result)],
});
};
fr.onerror = (err) => {
return reject(err);
};
return fr.readAsArrayBuffer(file);
});
})
);
})(files).catch((err) => {
return rej(err);
});
}
return res(temp);
});
})
);
initObj = {};
if (this.includeOrder) {
orderOfFormObject = ar.flatMap((oo) => {
var key;
key = Object.keys(oo)[0];
if (this.excludeTypes.includes(oo[key].type)) {
return [];
} else {
return [key];
}
});
initObj = { orderOfFormObject };
}
return ar.reduce((oo, ee) => {
var k, v;
[[k, v]] = Object.entries(ee);
if (this.fields.length > 0) {
Object.keys(v).forEach((ff) => {
if (!this.fields.includes(ff) && !this.fields.includes("*")) {
return delete v[ff];
}
});
}
if (this.excludeTypes.length > 0) {
if (
this.excludeTypes.some((ff) => {
return v.type === ff;
})
) {
return oo;
}
}
return Object.assign(oo, {
[k]: oo[k] ? oo[k].concat(v) : [v],
});
}, initObj);
} catch (error) {
e = error;
throw new Error(e);
}
}
}
HtmlFormObjectParserForGoogleAppsScript.name =
"HtmlFormObjectParserForGoogleAppsScript_js";
return HtmlFormObjectParserForGoogleAppsScript;
}.call(this);
return (r.HtmlFormObjectParserForGoogleAppsScript =
HtmlFormObjectParserForGoogleAppsScript);
})(this);