|
// 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 = ['[email protected]']; |
|
|
|
// 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); |
|
}); |
|
|
|
} |
|
|