-
Notifications
You must be signed in to change notification settings - Fork 18
/
form-notifications.js
81 lines (62 loc) · 2.02 KB
/
form-notifications.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* ----------------------------------
* Google Forms Email Notifications
* ----------------------------------
*
* Get Google Forms response in an email message
*
* license: MIT
* language: Google Apps Script
* author: Amit Agarwal
* email: [email protected]
* web: https://digitalinspiration.com/
* blog: http://labnol.org/?p=20884
*
*/
/**
* @OnlyCurrentDoc
*/
function Initialize() {
try {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers)
ScriptApp.deleteTrigger(triggers[i]);
ScriptApp.newTrigger("EmailGoogleForms")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit().create();
} catch (error) {
throw new Error("Please add this code in the Google Spreadsheet");
}
}
function EmailGoogleForms(e) {
if (!e) {
throw new Error("Please go the Run menu and choose Initialize");
}
if (MailApp.getRemainingDailyQuota() < 1) {
throw new Error("Sorry, your email quota for Gmail is over");
}
try {
// Replace with your email address
var email = "[email protected]";
// Replace with a custom subject
var subject = "New response submitted"
var message = [],
ss = SpreadsheetApp.getActiveSheet(),
cols = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
// Iterate through the Form Fields
for (var keys in cols) {
var key = cols[keys];
var entry = e.namedValues[key] ? e.namedValues[key].toString() : "";
// Only include form fields that are not blank
if (entry && entry.replace(/,/g, "") !== "")
message.push(key + ' :: ' + entry);
}
// Use MailApp if Gmail is disabled for your GSuite Domain
GmailApp.sendEmail(email, subject, message.join("\n\n"), {
cc: "[email protected]",
bcc: "[email protected]"
});
} catch (error) {
Logger.log(error.toString());
}
}