-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.js
54 lines (44 loc) · 1.66 KB
/
Code.js
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
// Google Scripts code for form submissions and email notifications
var scriptProp = PropertiesService.getScriptProperties();
function doGet(request) {
scriptProp.setProperty('sheetName', request.parameter.formUsed);
return ContentService.createTextOutput(request.parameter.formUsed);
}
function setup() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
scriptProp.setProperty('key', doc.getId());
}
function doPost(request) {
var lock = LockService.getScriptLock();
lock.waitLock(10000);
try {
var doc = SpreadsheetApp.openById(scriptProp.getProperty('key'));
var sheet = doc.getSheetByName(scriptProp.getProperty('sheetName'));
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var nextRow = sheet.getLastRow() + 1;
var emailHeaders = headers;
var emailContent = [];
var newRow = headers.map(function(header) {
emailContent.push(request.parameter[header]);
return header === 'timestamp' ? new Date() : request.parameter[header];
});
//Build Email
var fullEmailContent = '';
for (i in emailHeaders) {
fullEmailContent += emailHeaders[i] + ': ' + emailContent[i] + '\n';
}
MailApp.sendEmail('korndragon@gmail.com', 'Louisianagroups.com Submission (' + request.parameter.formUsed + ')', fullEmailContent);
sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow]);
return ContentService.createTextOutput(JSON.stringify({
'result': 'success',
'row': nextRow
})).setMimeType(ContentService.MimeType.JSON);
} catch (e) {
return ContentService.createTextOutput(JSON.stringify({
'result': 'error',
'error': e
})).setMimeType(ContentService.MimeType.JSON);
} finally {
lock.releaseLock();
}
}