-
Notifications
You must be signed in to change notification settings - Fork 18
/
form-pdf.js
97 lines (71 loc) · 2.61 KB
/
form-pdf.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* --------------------------
* Google Forms PDF Document
* --------------------------
*
* Generate PDF documents from Google Form Responses
*
* license: MIT
* language: Google Apps Script
* author: Amit Agarwal
* email: [email protected]
* web: https://digitalinspiration.com/
*
*/
function Initialize() {
try {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers)
ScriptApp.deleteTrigger(triggers[i]);
ScriptApp.newTrigger("EmailGoogleFormsPDF")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit().create();
} catch (error) {
throw new Error("Please add this code in the Google Spreadsheet");
}
}
function EmailGoogleFormsPDF(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"
// Replace with your own document template ID
var templateId = "15bw3meKSE_9kv3p1aarTNfUv3qQ__0xS8cO-cAfziEQ";
var data = {},
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() : "";
data[key] = entry;
// Only include form fields that are not blank
if (entry && entry.replace(/,/g, "") !== "")
message.push(key + ' :: ' + entry);
}
var template = DriveApp.getFileById(templateId);
var targetFile = template.makeCopy("GoogleForms.pdf");
var targetDocument = DocumentApp.openById(targetFile.getId());
var targetBody = targetDocument.getBody();
Object.keys(data).forEach(function (key) {
var searchPattern = "<<" + key + ">>";
targetBody.replaceText(searchPattern, data[key]);
});
targetDocument.saveAndClose();
var blob = targetDocument.getAs("application/pdf").copyBlob();
// Use MailApp if Gmail is disabled for your GSuite Domain
GmailApp.sendEmail(email, subject, message.join("\n\n"), {
attachments: [blob]
});
} catch (error) {
Logger.log(error.toString());
}
}