Created
November 15, 2020 00:46
-
-
Save slaporte/2db6cf05e19983d66f4cacb696d71e68 to your computer and use it in GitHub Desktop.
Google Apps Script to send an email (from a Doc template) based on a response to a form. Used for Wiki Loves Monuments email confirmation.
This file contains hidden or 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
/** | |
* Based on: https://github.com/googleworkspace/solutions/blob/master/content-signup/src/Code.js | |
*/ | |
var EMAIL_TEMPLATE_DOC_URL = 'https://docs.google.com/document/d/***/edit?usp=sharing'; | |
var EMAIL_SUBJECT = '[Response requested] Please confirm your Wiki Loves Monuments winners'; | |
var CC_RECIPIENTS = '[email protected], [email protected]' | |
/** | |
* Installs a trigger on the Spreadsheet for when a Form response is submitted. | |
*/ | |
function installTrigger() { | |
ScriptApp.newTrigger('onFormSubmit') | |
.forSpreadsheet(SpreadsheetApp.getActive()) | |
.onFormSubmit() | |
.create(); | |
} | |
/** | |
* Sends a customized email for every response on a form. | |
* | |
* @param {Object} e - Form submit event | |
*/ | |
function onFormSubmit(e) { | |
var responses = e.namedValues; | |
var status = ''; | |
var coord_email = responses['Email Address'][0].trim(); | |
var juror_email = responses['Juror: Email'][0].trim(); | |
MailApp.sendEmail({ | |
to: juror_email + ', ' + coord_email, | |
cc: CC_RECIPIENTS, | |
subject: EMAIL_SUBJECT, | |
htmlBody: createEmailBody(responses), | |
}); | |
status = 'Sent'; | |
// Append the status on the spreadsheet to the responses' row. | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var row = sheet.getActiveRange().getRow(); | |
var column = e.values.length + 1; | |
sheet.getRange(row, column).setValue(status); | |
Logger.log('status=' + status + '; responses=' + JSON.stringify(responses)); | |
} | |
/** | |
* Creates email body | |
* | |
* @param {Object} responses - e.namedValues from Form submit event | |
* @return {string} - The email body as an HTML string. | |
*/ | |
function createEmailBody(responses) { | |
var docId = DocumentApp.openByUrl(EMAIL_TEMPLATE_DOC_URL).getId(); | |
var emailBody = docToHtml(docId); | |
var timestamp = responses.Timestamp[0]; | |
var coord_name = responses['Your name or username'][0].trim(); | |
var juror_name = responses['Juror: Name or username'][0].trim(); | |
var country = responses['Your national competition/country'][0].trim() | |
var published = responses['Are your winners published?'][0].trim() | |
var jurors_url = responses['Where do you publish your jury members?'][0].trim() | |
var photo_1_name = responses['Photo 1: File Name'][0].trim() | |
var photo_2_name = responses['Photo 2: File Name'][0].trim() | |
var photo_3_name = responses['Photo 3: File Name'][0].trim() | |
var photo_4_name = responses['Photo 4: File Name'][0].trim() | |
var photo_5_name = responses['Photo 5: File Name'][0].trim() | |
var photo_6_name = responses['Photo 6: File Name'][0].trim() | |
var photo_7_name = responses['Photo 7: File Name'][0].trim() | |
var photo_8_name = responses['Photo 8: File Name'][0].trim() | |
var photo_9_name = responses['Photo 9: File Name'][0].trim() | |
var photo_10_name = responses['Photo 10: File Name'][0].trim() | |
var photo_1_url = responses['Photo 1: URL'][0].trim() | |
var photo_2_url = responses['Photo 2: URL'][0].trim() | |
var photo_3_url = responses['Photo 3: URL'][0].trim() | |
var photo_4_url = responses['Photo 4: URL'][0].trim() | |
var photo_5_url = responses['Photo 5: URL'][0].trim() | |
var photo_6_url = responses['Photo 6: URL'][0].trim() | |
var photo_7_url = responses['Photo 7: URL'][0].trim() | |
var photo_8_url = responses['Photo 8: URL'][0].trim() | |
var photo_9_url = responses['Photo 9: URL'][0].trim() | |
var photo_10_url = responses['Photo 10: URL'][0].trim() | |
emailBody = emailBody.replace(/{{juror_name}}/g, juror_name); | |
emailBody = emailBody.replace(/{{coord_name}}/g, coord_name); | |
emailBody = emailBody.replace(/{{country}}/g, country); | |
emailBody = emailBody.replace(/{{timestamp}}/g, timestamp); | |
emailBody = emailBody.replace(/{{published}}/g, published); | |
emailBody = emailBody.replace(/{{jurors_url}}/g, jurors_url); | |
emailBody = emailBody.replace(/{{photo_1_name}}/g, photo_1_name); | |
emailBody = emailBody.replace(/{{photo_2_name}}/g, photo_2_name); | |
emailBody = emailBody.replace(/{{photo_3_name}}/g, photo_3_name); | |
emailBody = emailBody.replace(/{{photo_4_name}}/g, photo_4_name); | |
emailBody = emailBody.replace(/{{photo_5_name}}/g, photo_5_name); | |
emailBody = emailBody.replace(/{{photo_6_name}}/g, photo_6_name); | |
emailBody = emailBody.replace(/{{photo_7_name}}/g, photo_7_name); | |
emailBody = emailBody.replace(/{{photo_8_name}}/g, photo_8_name); | |
emailBody = emailBody.replace(/{{photo_9_name}}/g, photo_9_name); | |
emailBody = emailBody.replace(/{{photo_10_name}}/g, photo_10_name); | |
emailBody = emailBody.replace(/{{photo_1_url}}/g, photo_1_url); | |
emailBody = emailBody.replace(/{{photo_2_url}}/g, photo_2_url); | |
emailBody = emailBody.replace(/{{photo_3_url}}/g, photo_3_url); | |
emailBody = emailBody.replace(/{{photo_4_url}}/g, photo_4_url); | |
emailBody = emailBody.replace(/{{photo_5_url}}/g, photo_5_url); | |
emailBody = emailBody.replace(/{{photo_6_url}}/g, photo_6_url); | |
emailBody = emailBody.replace(/{{photo_7_url}}/g, photo_7_url); | |
emailBody = emailBody.replace(/{{photo_8_url}}/g, photo_8_url); | |
emailBody = emailBody.replace(/{{photo_9_url}}/g, photo_9_url); | |
emailBody = emailBody.replace(/{{photo_10_url}}/g, photo_10_url); | |
return emailBody; | |
} | |
/** | |
* Downloads a Google Doc as an HTML string. | |
* | |
* @param {string} docId - The ID of a Google Doc to fetch content from. | |
* @return {string} The Google Doc rendered as an HTML string. | |
*/ | |
function docToHtml(docId) { | |
// Downloads a Google Doc as an HTML string. | |
var url = 'https://docs.google.com/feeds/download/documents/export/Export?id=' + | |
docId + '&exportFormat=html'; | |
var param = { | |
method: 'get', | |
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()}, | |
muteHttpExceptions: true, | |
}; | |
return UrlFetchApp.fetch(url, param).getContentText(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment