Created
October 29, 2022 15:12
-
-
Save hearvox/58d14174277e944cf86cd387118579da to your computer and use it in GitHub Desktop.
Upon Google Form submission, send response values to respondent in an emailed PDF attachment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Process form data: make PDF, send email; trigged on Form Submit. | |
* | |
* Documentation: | |
* https://docs.google.com/document/d/1O4Y-3VqhhnL-kXPXM3S2XDoQebevIH7UoXQUxpjjtuo/edit | |
* | |
* @param {e} obj Form data (includes user-submitted field values). | |
* | |
* @return void | |
*/ | |
function tt_afterFormSubmit(e) { | |
const info = e.namedValues; | |
const pdfFile = tt_createPDF(info); | |
tt_sendEmail(e.namedValues['Email Address'][0],pdfFile); | |
/* Write value in sheet col to indicate success. | |
const entryRow = e.range.getRow(); | |
const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses"); | |
// ws.getRange(entryRow, 10).setValue(pdfFile.getName()); // Write filename. | |
ws.getRange(entryRow, 10).setValue('1'); // Edit range col# if # of form questions change. | |
*/ | |
} | |
/** | |
* Send email with attachment. | |
* | |
* @param {email} string Email address. | |
* @param {pdfFile} obj File in Drive app (to be sent as email attachment). | |
* @return void. | |
*/ | |
function tt_sendEmail(email,pdfFile) { | |
// Test data: | |
// email = '[email protected]'; | |
// pdfFile = DriveApp.getFileById('1YytruRpdnFxoXJ86shNYwl12p1sSI--S'); | |
let message = 'Thank you for listening to the Teaching Hard History podcast! Your professional development certificate is attached.'; | |
message += '\n\nPlease note that because Learning for Justice is not a credit-granting agency, we encourage you to check with your administration to determine if your participation will count toward continuing education requirements.'; | |
message += '\n\nHelp us avoid the spam filter! Add [email protected] to your address book.'; | |
MailApp.sendEmail({ | |
to: email, | |
subject: 'Learning for Justice: Professional Development Certificate', | |
replyTo: '[email protected]', | |
name: 'Learning for Justice', | |
body: message, | |
attachments: [pdfFile], | |
}); | |
} | |
/** | |
* Create PDF with form data using a Google Doc as a template.. | |
* | |
* @param {info} array User-submitted form-field values. | |
* @return {pdfFile} obj File in Drive app (to be sent as email attachment). | |
*/ | |
function tt_createPDF(info) { | |
// Test data: | |
// info = {'Full Name':['Jane Doe'],'Podcast Episode':['1: Reframing the Movement']}; | |
// Google Drive folders (for temp files and created PDFs) and Google Doc template. | |
const pdfFolder = DriveApp.getFolderById('1c7DcbUzn_8-8OIb_di6pB5ITqqIbMcJn'); | |
const tempFolder = DriveApp.getFolderById('1TG2Y3amcUD6xS6SbEtPmcshPRWCnd1Vw'); | |
const templateDoc = DriveApp.getFileById('1l-PUQUlpuL5qkLcJrePClSZl7Mq6EiZY3hrZe-8f9mk'); | |
// Today's date (used in doc template text and PDF filename). | |
const today = new Date().toJSON().slice(0,10); // Today's date (YYYY-MM-DD). | |
// Copy template into temp folder. | |
const newTempFile = templateDoc.makeCopy(tempFolder); | |
// Access text in copied Doc template. | |
const openDoc = DocumentApp.openById(newTempFile.getId()); | |
const body = openDoc.getBody(); | |
// Use form data in template. | |
body.replaceText("{{name}}", info['Full name'][0]); | |
body.replaceText("{{episode}}", info['Podcast episode'][0]); | |
body.replaceText("{{date}}", today); | |
openDoc.saveAndClose(); | |
// Filename for PDF (replace space in name with "-"; make date YYYYMMDD). | |
let pdfName = 'Learning-for-Justice-PD-Certification-TMO-' + info['Full name'][0].replace(/ /g, "-"); | |
pdfName += '-' + today.replace(/-/g,''); | |
// Make PDF file; store in PDF folder; move temp flie to Trash. | |
const blobPDF = newTempFile.getAs(MimeType.PDF); | |
const pdfFile = pdfFolder.createFile(blobPDF).setName(pdfName); | |
tempFolder.removeFile(newTempFile); | |
return pdfFile; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment