Skip to content

Instantly share code, notes, and snippets.

@divergentdave divergentdave/Code.gs
Last active Nov 23, 2018

Embed
What would you like to do?
Calendar time zone check - Google Apps Script
// This script checks for Google Calendar events with non-default
// time zones, and sends an email whenever it finds one.
// Usage:
// 1. Create a new Google Docs spreadsheet
// 2. Click on Tools -> Script editor...
// 3. In the script editor, click on Resources -> Advanced Google services...
// 4. Click on the enable toggle for "Calendar API", and use the default name "Calendar"
// 5. Click on the "Google API Console" link in the Advanced Google Services dialog box
// 6. In the Google API Console, click on the link to "View All" APIs
// 7. In the "G Suite" section, click on "Google Calendar API"
// 8. On the Google Calendar API detail page, click on "Enable"
// 9. Return to the script editor and click "OK" on the dialog box if you have not yet
// 10. In the script editor, delete the default definition of "myFunction"
// 11. Copy and paste this script into the script editor
// 12. Click on the run button to run "checkCalendar", and authorize the script to access your account
// 13. Click on Edit -> Current project's triggers
// 14. Click on the link to add a trigger
// 15. In the drop down boxes, choose "checkCalendar", "Time-driven", "Day timer", and any hour of your choice
// 16. Click on the link for execution failure notifications
// 17. Confirm that execution failure notifications will be emailed to you
// 18. Click "OK" and "Save" to exit the dialog boxes
// 19. To test the script, add an event to your calendar with the wrong timezone, click the run button in the script editor, and check if you are sent an email
function checkCalendar() {
MailApp.getRemainingDailyQuota(); // make sure OAuth flow runs?
var now = new Date();
var twoDaysHence = new Date(now.getTime() + (2 * 24 * 60 * 60 * 1000));
var calendar = CalendarApp.getDefaultCalendar();
var id = calendar.getId();
var calendarResponse = Calendar.Calendars.get(id);
var defaultTimeZone = calendarResponse.timeZone;
var eventsResponse = Calendar.Events.list(id, {
timeMin: now.toISOString(),
timeMax: twoDaysHence.toISOString(),
});
for (var i = 0; i < eventsResponse.items.length; i++) {
if (eventsResponse.items[i].start) {
var timeZone = eventsResponse.items[i].start.timeZone;
var summary = eventsResponse.items[i].summary;
if (timeZone && timeZone != defaultTimeZone) {
MailApp.sendEmail(
id,
"Time zone warning regarding " + summary,
"An upcoming event, " + summary + " was created in the " + timeZone + " time zone."
);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.