Skip to content

Instantly share code, notes, and snippets.

@brainysmurf
Last active October 13, 2015 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainysmurf/f029d9e9565a6bddb5e5 to your computer and use it in GitHub Desktop.
Save brainysmurf/f029d9e9565a6bddb5e5 to your computer and use it in GitHub Desktop.
Attach to the responses spreadsheet and set up trigger. Anyone with editing or viewing privileges will be emailed a simple summary of any entries that happened on that day.

“Today's Entries" an administrative tool for schools.

  • Adds notifications and data collation to the "responses" Google Sheet (or any Google Sheet)

Copy and paste the below into the script editor, and configure the trigger to run once per day. For further modifications, see the comments in the script.

// We use a library here called "Moment"
// It's a dependency, so the below won't work until you have done this:
// Go to Resources -> Libraries and enter this product key: MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48
var moment = Moment.load();
// Setup the sheet and spreadsheet variables
// Confusing thing about GAS is that a Spreadsheet is the document but the sheet is the ... sheet
// Anyway it always confuses me ;)
var sheet = SpreadsheetApp.getActiveSheet();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Setup the variables we need for date comparisons
// "now" represents the time the script is run
var now = moment(moment().format());
var dateFormat = "dddd, MMMM Do YYYY";
var timestampFormat = "M/DD/YYYY H:m:ss";
// Define a function which we'll use to filter out bad data
// Returns false if it item should be filtered out
function filterOutValues (row, index, obj) {
// row[0] is the first column
if (row[0] === "") {
return false;
}
return true;
}
// This builds the body of the email
function buildBody() {
// Grab the data from the sheet
var range = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn());
var values = range.getValues();
var row, timestamp, username, context, ant, beh, other, result;
// Make the "body" a string that contains the message we want to send
var body = "";
var incidents = 0; // tally of how many incidents
// forEach function loops through after filtering out bad stuff
values.filter(filterOutValues).forEach(function (row, index, arr) {
timestamp = moment(row[0]);
// Ensure that today is a weekday
if (timestamp.weekday() == 0 || timestamp.weekday() > 5) {
return
}
// Another filter to only collate items that have been entered today
if (timestamp.format(dateFormat) == now.format(dateFormat)) {
body += '----------------\n\n';
username = row[1];
context = row[2];
ant = row[3];
beh = row[4];
other = row[5];
result = row[6];
body += 'Username:\n' + username + '\n\n';
body += 'Behaviour:\n' + beh + '\n\n';
body += 'Context:\n' + context + '\n\n';
body += 'Antecedent:\n' + ant + '\n\n';
body += 'Other:\n' + other + '\n\n';
incidents += 1;
body += '----------------\n\n';
}
});
// Massage the body of the email to summarize the information
if (incidents == 0) {
body += "Congratulations, there were no incidents today!";
} else {
body = 'There was/were ' + incidents.toString() + ' incidents today\n\n' + body;
}
return body;
}
function emailAgents() {
// Determine who should get the email, and actually send it
// No duplicate sends
var agents = [];
var body = buildBody();
spreadsheet.getViewers().forEach(function (user, index, obj) {
// Add this user's email only if not already there
if (agents.indexOf(user.getEmail()) <= -1) {
agents.push(user.getEmail());
}
});
spreadsheet.getEditors().forEach(function (user, index, obj) {
// Add this user's email only if not already there
if (agents.indexOf(user.getEmail()) <= -1) {
agents.push(user.getEmail());
}
});
// For debugging, define agents here and test
// agents = ['adam.morris@igbis.edu.my'];
// Now email them out to everyone who is a viewer or editor
agents.forEach(function (value, _, obj) {
MailApp.sendEmail(value, 'Lucy\'s Behavior: ' + now.format(dateFormat), body);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment