… and call it “forEachMinion“. It will look like this:
function forEachMinion(callback) {
const sheet = SpreadsheetApp.getActiveSheet();
const dataRange = sheet.getRange("A2:C6");
const data = dataRange.getValues();
data.forEach(function (rowData) {
const recipient = rowData[0];
const emailAddress = rowData[1];
const bananasCount = rowData[2];
callback(recipient, emailAddress, bananasCount);
})
}
We’ll use the function above for both reports – daily and monthly.
Now let’s create separate scripts for each report.
bananaDailyReport:
function bananaDailyReport() {
forEachMinion(function(recipient, emailAddress, bananasCount) {
if (bananasCount < 20) {
return
}
const greeting = 'Dear ' + recipient + ',\n'
const bananasCountMessage = 'You have ' + bananasCount + ' bananas accumulated.';
const greatJobMessage = 'Great job! Come pick them up :)';
const message = [greeting, bananasCountMessage, greatJobMessage].join('\n');
const subject = 'Good news, you have some bananas waiting for you!';
MailApp.sendEmail(emailAddress, subject, message);
})
}
bananaMonthlyReport:
function bananaMonthlyReport() {
forEachMinion(function(recipient, emailAddress, bananasCount) {
const greeting = 'Dear ' + recipient + ',\n'
const bananasCountMessage = 'You have ' + bananasCount + ' bananas on your account. Keep it going!
Remember you can come pick them up once you reach 20 or more.';
const message = [greeting, bananasCountMessage].join('\n');
const subject = 'Here's your weekly banana summary!';
MailApp.sendEmail(emailAddress, subject, message);
})
}
You can run each of these scripts manually as we did before but that’s not the point, right?
Luckily, Google already implemented a simple tool for triggering reports. It’s aptly namedTime-driven triggers, and we’ll use it today.
Let’s create another script, which will set up triggers responsible for calling our mailing functions. We’ll name it setupBananaCountEmailsSchedule.
function setupBananaCountEmailsSchedule() {
ScriptApp.newTrigger('bananaDailyReport')
.timeBased()
.everyDays(1)
.atHour(23)
.create();
ScriptApp.newTrigger('bananaMonthlyReport')
.timeBased()
.onMonthDay(30)
.atHour(23)
.create();
}
That would be all. But if at some point you decide to delete these triggers, you can use the following code:
function deleteBananaCountEmailSchedule() {
const allTriggers = ScriptApp.getProjectTriggers();
allTriggers.forEach(function(trigger) {
if (['bananaDailyReport', 'bananaMonthlyReport'].indexOf(trigger.getHandlerFunction()) > -1) {
ScriptApp.deleteTrigger(trigger);
}
})
}
You’ll still be able to trigger daily and monthly reports manually but nothing will be sent out automatically. Hopefully, the minions will continue working as usual. 😉
Other considerations
To make things simpler, an automated email script can fetch all the necessary details from a spreadsheet. It doesn’t look pretty but can be useful in some situations. For a specific example with a code, check outGoogle Developer pages.
We hope you found our guide useful. Until next time!
I hope our google apps script to send emails was useful for you. It was initially published in the article "Sending emails from Google Sheets"in the Mailtrap Blog.