Created
January 12, 2013 05:18
-
-
Save edsammy/4516246 to your computer and use it in GitHub Desktop.
Google script to send weekly summaries of Google calendar events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EMAIL = "[email protected]"; | |
CALENDAR = "calendarname"; | |
function weeklyReminder() { | |
var StartDate = new Date(); | |
var StartDateDD = StartDate.getDate() + 1; | |
// Months are based on 0-11 so +1 | |
var StartDateMM = StartDate.getMonth() + 1; | |
var EndDateDD = StartDate.getDate() + 7; | |
// Across months, the date will get messed up so...(NEEDS TESTING!) | |
if (EndDateDD > 31) { | |
var EndDateMM = StartDateMM + 1; | |
EndDateDD = EndDateDD - 31; | |
} | |
else { | |
var EndDateMM = StartDateMM; | |
} | |
var EndDate = new Date(StartDate.getYear(), EndDateMM, EndDateDD); | |
var cal = CalendarApp.getCalendarById(CALENDAR); | |
var weeklyEvents = cal.getEvents(StartDate, EndDate); | |
var eventTitle = []; | |
var eventStartTime = []; | |
var previousStartTime = new Date(1970); | |
var eventEndTime = []; | |
var eventDescription = []; | |
var eventLocation = []; | |
var message = "<h1>Weekly Events</h1>"; | |
for (var i = 0; i < weeklyEvents.length; ++i){ | |
eventTitle[i] = weeklyEvents[i].getTitle(); | |
eventStartTime[i] = weeklyEvents[i].getStartTime(); | |
eventEndTime[i] = weeklyEvents[i].getEndTime(); | |
eventDescription[i] = weeklyEvents[i].getDescription(); | |
eventLocation[i] = weeklyEvents[i].getLocation(); | |
// If there are multiple events on same day, dont print the date out more than once | |
if (eventStartTime[i].getMonth() == previousStartTime.getMonth() && eventStartTime[i].getDate() == previousStartTime.getDate() && eventStartTime[i].getYear() == previousStartTime.getYear()){ | |
message = message + | |
"<br><u><b>" + eventTitle[i] + "</b></u><br>" + | |
"<font color = 'red'>" + formatTime(eventStartTime[i]) + " – " + formatTime(eventEndTime[i]) +"</font><br>" + | |
"<i>" + eventDescription[i] + "</i><br>"; | |
} | |
else { | |
message = message + | |
"<hr noshade width='75%' align = 'left'><h3>" + ((eventStartTime[i].toDateString()).slice(0,10))+ ":</h3>" + | |
"<u><b>" + eventTitle[i] + "</b></u><br>" + | |
"<font color = 'red'>" + formatTime(eventStartTime[i]) + " – " + formatTime(eventEndTime[i]) +"</font><br>" + | |
"<i>" + eventDescription[i] + "</i><br>"; | |
} | |
previousStartTime = eventStartTime[i]; | |
} | |
if (weeklyEvents.length == 0){ | |
message = "There are no events/someone forgot to put any events in the calendar for this week."; | |
} | |
MailApp.sendEmail(EMAIL, "Events: " + StartDateMM+"/"+StartDateDD + "–" + EndDateMM+"/"+EndDateDD, message, {htmlBody: message}); | |
} | |
// Converts 24 hour based time to 12 hour | |
function formatTime(date) { | |
var d = new Date(date); | |
var hh = d.getHours(); | |
var mm = d.getMinutes(); | |
var mod = "AM"; | |
if (hh >= 12){ | |
hh = hh - 12; | |
mod = "PM"; | |
} | |
if (hh == 0){ | |
hh = 12; | |
} | |
mm = mm < 10 ? "0"+mm : mm; | |
return (hh + ":" + mm + " " + mod); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would like to use this code, exactly what I need! Unfortunately, when using the script with EMAIL = address to send to and CALENDAR = google calendar ID, I get the error message
Exception: Event start time must be before event end time. (line 21, file "Code")
.There seems to be an issue with the part
var weeklyEvents = cal.getEvents(StartDate, EndDate);
. Any idea how to overcome this?