-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentiment.gs
93 lines (69 loc) · 2.54 KB
/
sentiment.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
/**
* Sentiment
* A tool to collect formative feedback
* https://github.com/ashenm/sentiment
*
* Ashen Gunaratne
* mail@ashenm.ml
*
*/
var SPREADSHEET = SpreadsheetApp.getActiveSpreadsheet();
function doGet (event) {
return ContentService.createTextOutput(JSON.stringify({
error: { errors: [{ domain: 'global', reason: 'httpMethodNotAllowed' }], code: 405 }
})).setMimeType(ContentService.MimeType.JSON);
}
function doPost (event) {
var COURSE;
// ensure content
if (!event.postData)
return ContentService.createTextOutput(JSON.stringify({
error: { errors: [{ domain: 'global', reason: 'required' }], code: 400 }
})).setMimeType(ContentService.MimeType.JSON);
// handle invalid content
try {
var data = JSON.parse(event.postData.contents);
} catch (e) {
return ContentService.createTextOutput(JSON.stringify({
error: { errors: [{ domain: 'global', reason: 'parseError' }], code: 400 }
})).setMimeType(ContentService.MimeType.JSON);
}
// ensure requisites
if (!(data.course && data.sentiment))
return ContentService.createTextOutput(JSON.stringify({
error: { errors: [{ domain: 'global', reason: 'required' }], code: 400 }
})).setMimeType(ContentService.MimeType.JSON);
// ensure valid course
if (!(COURSE = SPREADSHEET.getSheetByName(data.course)))
return ContentService.createTextOutput(JSON.stringify({
error: { errors: [{ domain: 'global', reason: 'requestedRangeNotSatisfiable' }], code: 416 }
})).setMimeType(ContentService.MimeType.JSON);
// insert feedback
try {
// last column will hold timestamp
var offset = COURSE.getLastColumn() - 1;
// append record to end
var insertion = COURSE.getLastRow() + 1;
// extract headers
var [headers] = COURSE.getRange(1, 1, 1, offset).getValues();
// construct insertion data
var rowData = headers.reduce(function (accumulator, header) {
return accumulator.concat(data[header] || '');
}, []);
// add timestamp
rowData.push(new Date().toISOString());
// insert record
COURSE.appendRow(rowData);
return ContentService.createTextOutput(JSON.stringify({
feedback: { row: insertion, code: 201 }
})).setMimeType(ContentService.MimeType.JSON);
} catch (e) {
// handle server errors
return ContentService.createTextOutput(JSON.stringify({
error: { errors: [{ domain: 'global', reason: 'backendError' }], code: 500 }
})).setMimeType(ContentService.MimeType.JSON);
} finally {
// commit any pending changes
SpreadsheetApp.flush();
}
}