Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var BASE_URL = 'https://sites.google.com/a/hawksey.info/sandbox/mozilla-open-badges-issuer-example'; // Google Sites page
// for script to handle issuing badges see https://script.google.com/d/1wOxT8VA2mhPVR4b4v7jMTOiIa3hNkUJyAcHUzUh6sObSITyOu3wKi4NS/edit?usp=sharing
/**
* Handle form submit
*/
function onSubmit(e) {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName("IssuedBadges"); // note: assumed sheet name
var email = e.namedValues['Email'][0]; // note: assumed question name with person's email address
var uid = getUID();
// write data to IssuedBadges sheet (you can include more info)
sheet.getRange(sheet.getLastRow()+1, 1, 1, 3).setValues([[uid, email, e.namedValues['Timestamp'][0]]]);
// send email
sendEmail(email, uid);
}
/**
* Sends an email
* @param {String} email address of person receiving badge.
* @param {String} uuid used to identify issued badge.
*/
function sendEmail(email, uuid) {
var claimString = '?key='+SpreadsheetApp.getActiveSpreadsheet().getId()+'&uid='+uuid;
var url = BASE_URL + claimString;
var text = 'Thank you for taking time to try this App Script Open Badges Issuer. To claim your badge in Google Sites visit ' + url;
var textHtml = HtmlService.createTemplateFromFile('mailText');
textHtml.url = url; // passing variables into templates https://developers.google.com/apps-script/guides/html/templates#pushing_variables_to_templates
MailApp.sendEmail(email,
"Thank you for trying the App Script Open Badges Issuer",
text,
{replyTo:"no-reply@hawksey.info", htmlBody: textHtml.evaluate().getContent()});
}
/**
* Gets RFC4122 version 4 compliant UUID. Based on http://stackoverflow.com/a/8809472/1027723
* @return {String} uuid.
*/
function getUID() {
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment