-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExport Google Form Responses to PDF.txt
More file actions
89 lines (76 loc) · 3.87 KB
/
Export Google Form Responses to PDF.txt
File metadata and controls
89 lines (76 loc) · 3.87 KB
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
function onFormSubmit(e) {
// Get the form and responses
var form = e.source;
var formResponses = e.response.getItemResponses();
var allItems = form.getItems();
var lastQuestionIndex = allItems.length - 1;
//This is a magic value :/
//https://adventuresusingai.com/google-drive-folder-id/
const destinationFolderId = "SECRET"
const destinationFolder = DriveApp.getFolderById(destinationFolderId)
// --- Get Applicant Name and RFI from the Third Question ---
var applicantName = "";
if (allItems.length > 2 && formResponses.length > 2) {
var firstQuestionItem = allItems[0];
if (firstQuestionItem.getType() === FormApp.ItemType.TEXT ||
firstQuestionItem.getType() === FormApp.ItemType.PARAGRAPH_TEXT) {
applicantName = formResponses[0].getResponse();
// Sanitize the applicant name for the filename
applicantName = applicantName.replace(/\s+/g, '_'); // Replace spaces with underscores
applicantName = "_" + applicantName; // Add an underscore to separate from the base filename
} else {
Logger.log("Warning: The first question is not a text-based question. Applicant name will not be added to the filename.");
}
} else {
Logger.log("Warning: There are fewer than three questions in the form. Applicant name will not be added to the filename.");
}
// Create a new Document
var docName = form.getTitle() + " - Response " + applicantName + e.response.getId();
var doc = DocumentApp.create(docName);
var body = doc.getBody();
// --- Improved Formatting ---
// Add a title with larger font and bold
var titleParagraph = body.appendParagraph(form.getTitle() + " Submission" + "For" + applicantName);
titleParagraph.setHeading(DocumentApp.ParagraphHeading.HEADING1);
// Add the timestamp with a clear label
var timestamp = e.response.getTimestamp();
body.appendParagraph("Submitted on: " + Utilities.formatDate(timestamp, Session.getTimeZone(), "MMMM dd, yyyy 'at' HH:mm z"));
body.appendParagraph(""); // Spacing
// Iterate through questions and answers (excluding the last one)
for (var i = 0; i < lastQuestionIndex; i++) {
//skip the 3rd (zero indexed) question to dis-include the intro
if(i == 2) {
continue
}
var formItem = allItems[i];
var question = formItem.getTitle();
var answer = formResponses[i] ? formResponses[i].getResponse() : "No answer provided";
// Format the question as bold text
body.appendParagraph(question + ":").setBold(true);
// Check if the answer is an array (for "Choose many" questions)
if (Array.isArray(answer)) {
// Join the array elements into a single string with line breaks and no bold
body.appendParagraph(answer.join("\n")).setIndentFirstLine(20).setIndentStart(20).setBold(false);
} else {
// If it's a single value, append it directly with no bold
body.appendParagraph(answer).setIndentFirstLine(20).setIndentStart(20).setBold(false);
}
body.appendParagraph(""); // Spacing
}
// --- Saving the PDF with a more descriptive name including Applicant Name ---
var docId = doc.getId();
doc.saveAndClose();
var completeDoc = DriveApp.getFileById(docId);
var pdfBlob = completeDoc.getAs('application/pdf');
// Create a filename that includes the timestamp and applicant name
var formattedTimestamp = Utilities.formatDate(timestamp, Session.getTimeZone(), "yyyyMMdd_HHmmss");
var pdfFilename = form.getTitle().replace(/\s+/g, '_') + applicantName + "_Response_" + formattedTimestamp; // Added applicantName
savePdfToDrive(pdfBlob, pdfFilename, destinationFolder)
completeDoc.setTrashed(true)
}
function savePdfToDrive(pdfBlob, filename, destinationFolder) {
Logger.log("Saving file with name " + filename + " to folder " + destinationFolder)
var pdfFile = destinationFolder.createFile(pdfBlob);
pdfFile.setName(filename + ".pdf");
Logger.log("PDF saved to Drive as: " + filename + ".pdf");
}