Daily calendar email with Google script - new event section
Last week I fixed a bug in my daily calendar email. This week I am going to talk about making an improvement to the script. Because my wife and I share calendars and we both get this email I don’t always find out about new events she adds that are more than 7 days our (since the daily email only includes events for the next 7 days). I feel an improvement that could be easily made is to include a section at the bottom of the email that shows all new events that were added in the last 24 hours, and that is exactly what we I talk about in this post.
function mailWeeklyCalInfo() { //get all the calendars associated with my account and store the IDs in an array var calendars = CalendarApp.getAllCalendars(); var id = []; for(i = 0; i < calendars.length; i++) { id.push(calendars[i].getId()); } //grab the date as well as create a date one week in the future var now = new Date(); var oneWeekFromNow = new Date(now.getTime() + (7 * 24 * 60 * 60 * 1000)); //(week / day / hour / min / second) var oneYearFromNow = new Date(now.getTime() + (52 * 7 * 24 * 60 * 60 * 1000)); var yesterday = new Date(now.getTime() - (24 * 60 * 60 * 1000)); //grab all the events for each calendar var events = []; for(i = 0; i < id.length; i++) { events.push(CalendarApp.getCalendarById(id[i]).getEvents(now, oneWeekFromNow)); } //grab all events for the next year var allEvents = []; for(i = 0; i < id.length; i++) { allEvents.push(CalendarApp.getCalendarById(id[i]).getEvents(now, oneYearFromNow)); } var newEvents = []; for(i = 0; i < id.length; i++) { for(j = 0; j < allEvents[i].length; j++) { var createDate = allEvents[i][j].getDateCreated(); if (createDate > yesterday) { newEvents.push(allEvents[i][j]); } } } //get the deatils (title, datetime, and color for the calendar) for each event var eventDetails = [] for(i = 0; i < id.length; i++) { for(j = 0; j < events.length; j++) { if(events[i][j]) { if(CalendarApp.getCalendarById(events[i][j].getOriginalCalendarId())) { eventDetails.push({title: events[i][j].getTitle(), date: events[i][j].getStartTime(), color: CalendarApp.getCalendarById(events[i][j].getOriginalCalendarId()).getColor()}); } else { eventDetails.push({title: events[i][j].getTitle(), date: events[i][j].getStartTime(),color: "black"}); } } } } //sort all the events by date (overall, not by calendar) eventDetails.sort(function (a, b) { if (a.date > b.date) { return 1; } if (a.date < b.date) { return -1; } // a must be equal to b return 0; }); //create an email with the info and email it! var body = ""; for (i = 0; i < eventDetails.length; i++) { var color = eventDetails[i].color; body += "<p style='color:" + color + ";'>EVENT TITLE: " + eventDetails[i].title + "<br>EVENT DATE: " + eventDetails[i].date.toDateString() + "<br>EVENT START TIME: " + eventDetails[i].date.toLocaleTimeString() + "<br></p>"; } if (newEvents.length > 0) { //add a header for the new events section body = body + "<hr><br><p>Newly Added Events:<br>"; //loop through the new events and add to email for (i = 0; i < newEvents.length; i++) { body = body + "Event Title: " + newEvents[i].getTitle() + "<br>Event Date: " + newEvents[i].getStartTime().toDateString() + "<br>Event Start Time: " + newEvents[i].getStartTime().toLocaleTimeString() + "<br>"; } body = body + "</p>"; } GmailApp.sendEmail("[email protected]", "schedule for the next 7 days", "", { htmlBody: body, cc: "[email protected]", name: "me" }); }
There are several things to note here:
1) There are several more variables to hold dates for Yesterday and OneYearFromNow. I have decided to only show events that are added for the next year. that is where the OneYearFromNow date comes into play. I am showing events that were added in the last 24 hours and that is what the Yesterday variable is for.
2) There is a new array that will hold all events for the next year.
3) I then loop through that array and add any events with a create date that is greater than 24 hours ago (i.e. it was created yesterday) to an array for newEvents.
4) The last new item is in the email building section of the script and it simply checks if there any objects in the newEvent array, and if there are it loops through it and adds them to the bottom of the email.
Once again I have posted the code on Github for you to check out if you like.
Google scripts is easy to use, power due to its hook into all of googles APIs and fun to use. I will be posting more about google app scripts soon.













