Last active
December 31, 2020 18:16
-
-
Save mkfink/c020daf3ad01532e013cf4918f9d40bf to your computer and use it in GitHub Desktop.
Laser cutter training quiz auto-email when user passes
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
/******************************************************************************** | |
* Script that runs on google forms quiz result spreadsheet every time the form is | |
* submitted or resubmitted. If the submitter scored full points on the quiz, send | |
* them an email congratulating them. | |
* Based on: https://developers.google.com/gsuite/solutions/content-signup | |
* To add this to a form, in the response sheet, open Tools > Script Editor and | |
* paste this in there. | |
*********************************************************************************/ | |
var EMAIL_TEMPLATE_DOC_URL = 'https://docs.google.com/document/d/1WXehtMLC--4R4zvhBm-qtVq7tqHlUYsgQ7YP5BrcuvU/edit?usp=sharing' | |
var EMAIL_SUBJECT = 'Congrats, you\'re now authorized to use Cyclops!' | |
/** | |
* Installs a trigger on the Spreadsheet for when a Form response is (re)submitted. | |
* Use the "Run" button in the script editor to run this _once_ to actually | |
* install the trigger on the sheet. Nothing will happen until you do that. | |
* If you run the install function more than once, the trigger will fire multiple | |
* times, and you will look silly sending people duplicate emails. | |
*/ | |
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 | |
* | |
* e.namedValues contains key value pairs of every field in the form, with the | |
* caveat that if the form is re-submitted, ONLY the values that changed will | |
* be filled in. The rest will be null. | |
* So for example, we can count on score changing with each submission (since | |
* we only care about the submission if the score has become 20/20. But email | |
* address doesn't change, hence grabbing the cell value instead of using | |
* responses['Email Address'] which will only be correct on the first submission. | |
*/ | |
function onFormSubmit(e) { | |
var responses = e.namedValues; | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var row = sheet.getActiveRange().getRow(); | |
// If the question title is a label, it can be accessed as an object field. | |
// If it has spaces or other characters, it can be accessed as a dictionary. | |
var timestamp = responses.Timestamp[0]; | |
var email = sheet.getRange(row,2).getValue(); | |
var name = sheet.getRange(row,4).getValue(); | |
var score = responses.Score | |
// If there is at least one topic selected, send an email to the recipient. | |
var status = ''; | |
if (score[0] == "20 / 20") { | |
MailApp.sendEmail({ | |
to: email, | |
subject: EMAIL_SUBJECT, | |
htmlBody: createEmailBody(name, score), | |
}); | |
status = 'sent'; | |
} | |
// Append the status on the spreadsheet to the response's row if an email was sent. | |
var column = e.values.length + 1; | |
sheet.getRange(row, column).setValue(status); | |
// Can view logs here: https://script.google.com/home/executions | |
Logger.log("email=" + email + " name=" + name + " score=" + score); | |
} | |
/** | |
* Creates email body | |
* | |
* @param {string} name - The recipient's name. | |
* @return {string} - The email body as an HTML string. | |
*/ | |
function createEmailBody(name) { | |
// Make sure to update the emailTemplateDocId at the top. | |
var docId = DocumentApp.openByUrl(EMAIL_TEMPLATE_DOC_URL).getId(); | |
var emailBody = docToHtml(docId); | |
emailBody = emailBody.replace(/{{Name}}/g, name); | |
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