-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet_script.js
63 lines (48 loc) · 1.85 KB
/
sheet_script.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
55
56
57
58
59
60
61
62
63
// Original code from https://github.com/jamiewilson/form-to-google-sheets
const sheetName = 'Sheet1'
const scriptProp = PropertiesService.getScriptProperties()
function initialSetup() {
const activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet()
scriptProp.setProperty('key', activeSpreadsheet.getId())
// testSendEmail()
createTrigger()
sendNotification()
}
function createTrigger() {
const doc = SpreadsheetApp.openById(scriptProp.getProperty('key'))
const sheet = doc.getSheetByName(sheetName)
ScriptApp.newTrigger('sendNotification')
.forSpreadsheet(doc)
.onChange()
.create();
}
function sendNotification() {
const recipientEmail = 'abc@gmail.com,xyz@gmail.com'; //add mail recipients here
const subject = 'New admission alert!';
const body = 'New entry in the school admission sheet, Check it out!';
MailApp.sendEmail(recipientEmail, subject, body);
}
function doPost(e) {
const lock = LockService.getScriptLock();
lock.tryLock(10000);
try {
const doc = SpreadsheetApp.openById(scriptProp.getProperty('key'));
const sheet = doc.getSheetByName(sheetName);
const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
const nextRow = sheet.getLastRow() + 1;
const newRow = headers.map(function (header) {
return header === 'Date' ? new Date() : e.parameter[header];
});
sheet.getRange(nextRow, 1, 1, newRow.length).setValues([newRow]);
sendNotification(); // Call sendNotification() after the form submission
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();
}
}