Last active
August 20, 2019 21:37
-
-
Save RyanSchw/4c54cba93c9c91ea106e03b85b416fd5 to your computer and use it in GitHub Desktop.
Google Form Post-Submit Hook
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
/** | |
* @OnlyCurrentDoc | |
* | |
* The above comment directs Apps Script to limit the scope of file | |
* access for this add-on. It specifies that this add-on will only | |
* attempt to read or modify the files in which the add-on is used, | |
* and not all of the user's files. The authorization request message | |
* presented to users will reflect this limited scope. | |
*/ | |
// Heavily modified https://github.com/gsuitedevs/apps-script-samples/tree/master/forms/notifications | |
/** | |
* A global constant String holding the title of the add-on. This is | |
* used to identify the add-on in the notification emails. | |
*/ | |
var ADDON_TITLE = 'Post-Submit Hook'; | |
/** | |
* Adds a custom menu to the active form to show the add-on sidebar. | |
* | |
* @param {object} e The event parameter for a simple onOpen trigger. To | |
* determine which authorization mode (ScriptApp.AuthMode) the trigger is | |
* running in, inspect e.authMode. | |
*/ | |
function onOpen(e) { | |
FormApp.getUi() | |
.createAddonMenu() | |
.addItem('Configure notifications', 'showSidebar') | |
.addToUi(); | |
} | |
/** | |
* Runs when the add-on is installed. | |
* | |
* @param {object} e The event parameter for a simple onInstall trigger. To | |
* determine which authorization mode (ScriptApp.AuthMode) the trigger is | |
* running in, inspect e.authMode. (In practice, onInstall triggers always | |
* run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or | |
* AuthMode.NONE). | |
*/ | |
function onInstall(e) { | |
onOpen(e); | |
} | |
/** | |
* Opens a sidebar in the form containing the add-on's user interface for | |
* configuring the notifications this add-on will produce. | |
*/ | |
function showSidebar() { | |
var ui = HtmlService.createHtmlOutputFromFile('Sidebar') | |
.setTitle('Post-Submit Hook'); | |
FormApp.getUi().showSidebar(ui); | |
} | |
/** | |
* Save sidebar settings to this form's Properties, and update the onFormSubmit | |
* trigger as needed. | |
* | |
* @param {Object} settings An Object containing key-value | |
* pairs to store. | |
*/ | |
function saveSettings(settings) { | |
PropertiesService.getDocumentProperties().setProperties(settings); | |
adjustFormSubmitTrigger(); | |
} | |
/** | |
* Queries the User Properties and adds additional data required to populate | |
* the sidebar UI elements. | |
* | |
* @return {Object} A collection of Property values and | |
* related data used to fill the configuration sidebar. | |
*/ | |
function getSettings() { | |
var settings = PropertiesService.getDocumentProperties().getProperties(); | |
// Get all text fields so user can select email question | |
var form = FormApp.getActiveForm(); | |
var textItems = form.getItems(FormApp.ItemType.TEXT); | |
settings.textItems = []; | |
for (var i = 0; i < textItems.length; i++) { | |
settings.textItems.push({ | |
title: textItems[i].getTitle(), | |
id: textItems[i].getId() | |
}); | |
} | |
var listItems = form.getItems(FormApp.ItemType.LIST); | |
settings.listItems = []; | |
for (var i = 0; i < listItems.length; i++) { | |
settings.ListItems.push({ | |
title: listItems[i].getTitle(), | |
id: listItems[i].getId() | |
}); | |
} | |
return settings; | |
} | |
/** | |
* Adjust the onFormSubmit trigger based on user's requests. | |
*/ | |
function adjustFormSubmitTrigger() { | |
var form = FormApp.getActiveForm(); | |
var triggers = ScriptApp.getUserTriggers(form); | |
var settings = PropertiesService.getDocumentProperties(); | |
// Create a new trigger if required; delete existing trigger | |
// if it is not needed. | |
var existingTrigger = null; | |
for (var i = 0; i < triggers.length; i++) { | |
if (triggers[i].getEventType() == ScriptApp.EventType.ON_FORM_SUBMIT) { | |
existingTrigger = triggers[i]; | |
break; | |
} | |
} | |
if (!existingTrigger) { | |
var trigger = ScriptApp.newTrigger('respondToFormSubmit') | |
.forForm(form) | |
.onFormSubmit() | |
.create(); | |
} else if (existingTrigger) { | |
ScriptApp.deleteTrigger(existingTrigger); | |
} | |
} | |
/** | |
* Responds to a form submission event if an onFormSubmit trigger has been | |
* enabled. | |
* | |
* @param {Object} e The event parameter created by a form | |
* submission; see | |
* https://developers.google.com/apps-script/understanding_events | |
*/ | |
function respondToFormSubmit(e) { | |
var settings = PropertiesService.getDocumentProperties(); | |
var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL); | |
// Check if the actions of the trigger require authorizations that have not | |
// been supplied yet -- if so, warn the active user via email (if possible). | |
// This check is required when using triggers with add-ons to maintain | |
// functional triggers. | |
if (authInfo.getAuthorizationStatus() == | |
ScriptApp.AuthorizationStatus.REQUIRED) { | |
// Re-authorization is required. In this case, the user needs to be alerted | |
// that they need to reauthorize; the normal trigger action is not | |
// conducted, since authorization needs to be provided first. Send at | |
// most one 'Authorization Required' email a day, to avoid spamming users | |
// of the add-on. | |
sendReauthorizationRequest(); | |
} else { | |
// All required authorizations have been granted, so continue to respond to | |
// the trigger event. | |
pingServer(e.response); | |
} | |
} | |
/** | |
* Called when the user needs to reauthorize. Sends the user of the | |
* add-on an email explaining the need to reauthorize and provides | |
* a link for the user to do so. Capped to send at most one email | |
* a day to prevent spamming the users of the add-on. | |
*/ | |
function sendReauthorizationRequest() { | |
var settings = PropertiesService.getDocumentProperties(); | |
var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL); | |
var lastAuthEmailDate = settings.getProperty('lastAuthEmailDate'); | |
var today = new Date().toDateString(); | |
if (lastAuthEmailDate != today) { | |
if (MailApp.getRemainingDailyQuota() > 0) { | |
var template = | |
HtmlService.createTemplateFromFile('AuthorizationEmail'); | |
template.url = authInfo.getAuthorizationUrl(); | |
template.notice = NOTICE; | |
var message = template.evaluate(); | |
MailApp.sendEmail(Session.getEffectiveUser().getEmail(), | |
'Authorization Required', | |
message.getContent(), { | |
name: ADDON_TITLE, | |
htmlBody: message.getContent() | |
}); | |
} | |
settings.setProperty('lastAuthEmailDate', today); | |
} | |
} | |
function pingServer(response) { | |
var form = FormApp.getActiveForm(); | |
var settings = PropertiesService.getDocumentProperties(); | |
var apiurl = settings.getProperty('apiurl'); | |
var secretkey = settings.getProperty('secretkey'); | |
var emailtype = settings.getProperty('emailtype'); | |
var form = FormApp.getActiveForm(); | |
var settings = PropertiesService.getDocumentProperties(); | |
var emailId = settings.getProperty('respondentEmailItemId'); | |
var emailItem = form.getItemById(parseInt(emailId)); | |
var respondentEmail = response.getResponseForItem(emailItem) | |
.getResponse(); | |
var nameId = settings.getProperty('respondentNameItemId'); | |
var nameItem = form.getItemById(parseInt(nameId)); | |
var respondentName = response.getResponseForItem(nameItem) | |
.getResponse(); | |
var subteam = settings.getProperty('subteam'); | |
var subteamItem = form.getItemById(parseInt(subteam)); | |
var respondentSubteam = response.getResponseForItem(subteamItem) | |
.getResponse(); | |
var payload = { | |
"email": respondentEmail, | |
"type": emailtype, | |
"name": respondentName, | |
"subteam": respondentSubteam | |
}; | |
var options = { | |
"method": "POST", | |
"body" : payload | |
}; | |
Logger.log(payload) | |
var response = UrlFetchApp.fetch(apiurl, options) | |
Logger.log(response) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment