Skip to content

Instantly share code, notes, and snippets.

@ao5357
Last active May 12, 2023 00:09
Show Gist options
  • Save ao5357/6993874 to your computer and use it in GitHub Desktop.
Save ao5357/6993874 to your computer and use it in GitHub Desktop.
Google Calendar will send you a daily agenda, but it's at 5:00 AM the day of. This agenda macro (in Google Apps Script) lets you trigger an agenda email to any address on any time interval.
/**
* Gets tomorrow's events and sends along an email agenda.
*/
function mailAgenda(){
// Configs.
var config = {
firstName: 'Brad',
email: 'brad@commercialprogression.com',
ignoreRecurringEvents: true
};
// Create a Date object for tomorrow (as long as this doesn't run close to midnight).
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
// Fetch the events for tomorrow (from the default cal for the account running this macro).
var events = CalendarApp.getDefaultCalendar().getEventsForDay(tomorrow);
// If there are events, construct and send an email.
if (events.length >= 1) {
var emailBody = '<p>Hey ' + config.firstName + ', here are the events you have tomorrow:</p><ul>';
var singleEvent = '';
var thisLocation = '';
var recurringEvents = 0;
for (var i = 0; i < events.length; i++) {
// Don't need to know about stuff that always happens.
if (!config.ignoreRecurringEvents || events[i].isRecurringEvent() === false) {
singleEvent = '<li><strong>' + events[i].getTitle() + '</strong> ';
// Location is not a required field.
thisLocation = events[i].getLocation();
if (thisLocation.length >= 2) {
singleEvent += 'at <em>' + thisLocation + '</em> ';
}
// All day events give midnight start and end times, so just make this nice.
if (events[i].isAllDayEvent() === false) {
singleEvent += 'from ' + events[i].getStartTime().toLocaleTimeString() + ' to ';
singleEvent += events[i].getEndTime().toLocaleTimeString();
}
else {
singleEvent += 'all day';
}
// Close and append.
singleEvent += '</li>';
emailBody += singleEvent;
}
else if (config.ignoreRecurringEvents) {
// Check later whether all events that day are recurring
recurringEvents++;
}
}
emailBody += '</ul>';
// Send email with the general mailer rather than gmail.
if ((events.length - recurringEvents) >= 1) {
MailApp.sendEmail(
config.email,
config.firstName + "'s Agenda for " + tomorrow.toDateString(),
'You have at least one event tomorrow.',
{
noReply: true,
htmlBody: emailBody
}
);
}
}
};
function processInbox(){
var label = GmailApp.getUserLabelByName("Week Old Inbox");
var nowDate = Date.now();
var threads = GmailApp.getInboxThreads();
for (var i = 0; i < threads.length; i++) {
var threadDate = threads[i].getLastMessageDate();
threadDate = Date.parse(threadDate) + 604800000;
if (threadDate < nowDate) {
threads[i].addLabel(label);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment