-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.gs
335 lines (291 loc) · 10.1 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
/**
* Create and add a menu to the UI
*/
function onOpen() {
// Get the UI
const ui = SpreadsheetApp.getUi();
// Create a new menu and add items to it
ui.createMenu("ODK Form Updater")
.addItem("Update existing form draft", "createDraftForm")
.addItem("Create new form", "createForm")
.addItem("Configure", "configure")
.addToUi();
}
/**
* Displays a modal dialog box prompting the user to configure the add-on.
*/
function configure() {
const ui = SpreadsheetApp.getUi();
const widget = HtmlService.createHtmlOutputFromFile("ConfigurationForm.html");
widget.setHeight(400);
ui.showModalDialog(widget, "Configuration");
}
/**
* Uploads a new draft form to ODK Central using the current configuration.
* If the configuration is faulty, user is notified through a toast message.
* If the response status of the request is not 200, the user is notified through an alert message.
*/
function createDraftForm() {
const formUrl = getFormUrl();
const prep = prepareRequest();
if (prep === null) {
return;
}
errorMessage = postRequest(formUrl + "/draft?ignoreWarnings=false",
prep["token"], prep["formId"], prep["sheet"], "Create draft form");
if (errorMessage == null) {
return;
}
const ui = SpreadsheetApp.getUi();
const confirmation = ui.alert(
errorMessage + "\n" +
"Do you want to resubmit with ignoreWarnings=true?\n",
ui.ButtonSet.YES_NO);
// if confirmation is YES
if (confirmation == ui.Button.YES) {
postRequest(formUrl + "/draft?ignoreWarnings=true",
prep["token"], prep["formId"], prep["sheet"], "Create draft form");
}
}
/**
* Uploads a new form to ODK Central using the current configuration.
* If the configuration is faulty, user is notified through a toast message.
* If the response status of the request is not 200, the user is notified through an alert message.
*/
function createForm() {
const projectUrl = getProjectUrl();
const prep = prepareRequest();
if (prep === null) {
return;
}
errorMessage = postRequest(projectUrl + "/forms?ignoreWarnings=false&publish=false",
prep["token"], prep["formId"], prep["sheet"], "Create new form");
if (errorMessage == null) {
return;
}
const ui = SpreadsheetApp.getUi();
const confirmation = ui.alert(
errorMessage + "\n" +
"Do you want to resubmit with ignoreWarnings=true?\n",
ui.ButtonSet.YES_NO);
// if confirmation is YES
if (confirmation == ui.Button.YES) {
postRequest(projectUrl + "/forms?ignoreWarnings=false&publish=false",
prep["token"], prep["formId"], prep["sheet"], "Create new form");
}
}
/**
* Retrieves user configuration properties and prepares a request object with email, token,
* form ID, and sheet data. If any configuration properties are missing or invalid, it displays
* an error message and returns null.
*
* @return {Object|null} The request object containing email, token, form ID, and sheet data or
* null if there is an error.
*/
function prepareRequest() {
// get user configuration properties
const properties = PropertiesService.getUserProperties();
const email = properties.getProperty("email");
const password = properties.getProperty("password");
const formId = properties.getProperty("formId");
const sessionUrl = getSessionUrl();
// check if any configuration properties are missing, if yes, toast and exit
if (!email || !password || !formId || !sessionUrl) {
SpreadsheetApp.getActiveSpreadsheet().toast("No authentication detected. Please configure in the menu");
return null;
}
// get authentication token using email, password and session url
const token = getToken(email, password, sessionUrl);
// if authentication failed, toast and exit
if (!token) {
SpreadsheetApp.getActiveSpreadsheet().toast("Authentication failed. Please reconfigure in the menu");
return null;
}
// get sheet data
const sheet = getSheet();
// if sheet data is invalid, toast and exit
if (!sheet) {
SpreadsheetApp.getActiveSpreadsheet().toast("Sheet error. Please validate the current sheet");
return null;
}
return {
"email": email,
"token": token,
"formId": formId,
"sheet": sheet
};
}
/**
* Sends a POST request to a specified URL with a token, form ID, and spreadsheet data.
* If the response is not 200,
* - if it is an error, displays an error message with details if available.
* - if it is an warning, return the warning message
* Otherwise, it displays a success message.
*
* @param {string} url - The URL to send the request to.
* @param {string} token - The token to include in the request header.
* @param {string} formId - The form ID to include in the request header.
* @param {object} sheet - The spreadsheet data to include in the request payload.
* @param {string} successMessage - The message to display on success.
*/
function postRequest(url, token, formId, sheet, successMessage) {
const response = UrlFetchApp.fetch(
url, {
"method": "post",
"muteHttpExceptions": true,
"contentType": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"headers": {
"Authorization": "Bearer " + token,
"X-XlsForm-FormId-Fallback": formId,
},
"payload": sheet,
});
if (response.getResponseCode() !== 200) {
const error = JSON.parse(response.getContentText());
let errorMessage = "Error Code: " + error["code"] + "\nMessage: " + error["message"];
// Add details if it exists
if ("details" in error) {
// If it is a warning, return the warning message
if ("warnings" in error["details"]) {
errorMessage += "\n\nWarnings:\n\n";
for (let key in error["details"]["warnings"]) {
errorMessage += key + ": " + JSON.stringify(error["details"]["warnings"][key]) + "\n";
}
return errorMessage;
}
// Otherwise, add error details
errorMessage += "\nDetails:\n\n";
for (let key in error["details"]) {
errorMessage += key + ": " + JSON.stringify(error["details"][key]) + "\n";
}
}
SpreadsheetApp.getUi().alert(errorMessage);
return null;
} else {
SpreadsheetApp.getActiveSpreadsheet().toast("Success: " + successMessage);
return null;
}
}
/**
* Retrieves the data from the active spreadsheet and returns it as a string.
* Returns null if there is an error.
*
* @return {string|null} The data from the active spreadsheet or null if there is an error.
*/
function getSheet() {
// Get the active spreadsheet ID.
const spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
// Construct the export URL
url = "https://docs.google.com/spreadsheets/export?id=" + spreadsheetId + "&exportFormat=xlsx";
// Get the OAuth token and send a GET request to the export URL.
const token = ScriptApp.getOAuthToken();
const response = UrlFetchApp.fetch(url, {
headers: {
"Authorization": "Bearer " + token
}
});
// Check if the GET request is successful and return the data from the active spreadsheet.
if (response.getResponseCode() !== 200) {
return null;
}
return response.getContent();
}
/**
* Sets the configuration parameters in the UserProperties and displays a success message.
*
* @param {string} email - The email address
* @param {string} password - The password
* @param {string} baseUrl - The base URL
* @param {string} projectId - The ID of the project
* @param {string} formId - The ID of the form
*/
function setConfig(email, password, baseUrl, projectId, formId) {
PropertiesService.getUserProperties().setProperties({
"email": email,
"password": password,
"baseUrl": baseUrl,
"projectId": projectId,
"formId": formId
});
SpreadsheetApp.getActiveSpreadsheet().toast("Success: Configuration");
}
/**
* Returns the session URL
*
* @return {string|null} The session URL.
*/
function getSessionUrl() {
const properties = PropertiesService.getUserProperties();
if (properties.getProperty("baseUrl") !== null) {
return properties.getProperty("baseUrl") + "/v1/sessions";
}
return null;
}
/**
* Returns the project URL
*
* @return {string} The project URL.
*/
function getProjectUrl() {
const properties = PropertiesService.getUserProperties();
if (properties.getProperty("baseUrl") !== null && properties.getProperty("projectId") !== null) {
return properties.getProperty("baseUrl") +
"/v1/projects/" +
properties.getProperty("projectId")
}
return null;
}
/**
* Returns the form URL
*
* @return {string} The form URL.
*/
function getFormUrl() {
const properties = PropertiesService.getUserProperties();
if (getProjectUrl() !== null && properties.getProperty("formId") !== null) {
return getProjectUrl() +
"/forms/" +
properties.getProperty("formId");
}
return null;
}
/**
* Returns the previous configuration parameters, except the password, as an array.
*
* @return {Array} An array containing the email, project URL, and form ID.
*/
function getConfigWithNoPassword() {
const properties = PropertiesService.getUserProperties();
const email = properties.getProperty("email");
const projectUrl = getProjectUrl();
const formId = properties.getProperty("formId");
return [email, projectUrl, formId];
}
/**
* Makes a POST request to the session URL to retrieve an authentication token.
*
* @param {string} email - The email address
* @param {string} password - The password
* @param {string} sessionUrl - The session URL
* @return {string|null} The authentication token or null if the POST request is unsuccessful.
*/
function getToken(email, password, sessionUrl) {
// Define the request body and parameters.
const bodyOfRequest = {
"email": email,
"password": password
};
const parametersOfRequest = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(bodyOfRequest),
"muteHttpExceptions": true
};
// Send the POST request to the session URL.
const response = UrlFetchApp.fetch(sessionUrl, parametersOfRequest);
// Check if the POST request is successful and return the authentication token.
if (response.getResponseCode() !== 200) {
return null;
}
return JSON.parse(response.getContentText())["token"];
}