Skip to content

Instantly share code, notes, and snippets.

@devtanc
Last active February 17, 2016 22:45
Show Gist options
  • Save devtanc/e71465094a2b35757e8d to your computer and use it in GitHub Desktop.
Save devtanc/e71465094a2b35757e8d to your computer and use it in GitHub Desktop.
Calendars and Events in Google Calendars
function myFunction() {
Logger.log('Getting your default calendar');
var cal = CalendarApp.getDefaultCalendar();
Logger.log(cal.getId());
Logger.log(cal.getName());
Logger.log('________________________________________')
Logger.log('Getting all calendars');
var cals = CalendarApp.getAllCalendars();
cals.forEach(function(cal) {
Logger.log(cal.getName())
});
Logger.log('________________________________________')
Logger.log('Getting calendar by NAME');
var calByName = CalendarApp.getCalendarsByName('INSERT NAME HERE');
Logger.log(calByName[0].getName())
//If you're getting a calendar by name, you must pass the exact name (case sensitive) and the return value is an ARRAY of calendars (notice the [] syntax)
//See: https://developers.google.com/apps-script/reference/calendar/calendar-app#getCalendarsByName(String)
Logger.log('________________________________________')
Logger.log('Getting calendar by ID');
var calById = CalendarApp.getCalendarById('INSERT FULL ID HERE');
Logger.log(calById.getName())
//If you are getting a calendar by ID, you must pass the full ID (which looks like an email) and it returns a calendar object representing that calendar
//See: https://developers.google.com/apps-script/reference/calendar/calendar-app#getOwnedCalendarById(String)
//Once you have the calendar (in this case in the calById object) then you can get and create events
//See: https://developers.google.com/apps-script/reference/calendar/calendar
//REMEMBER THAT YOU ARE WORKING WITH THE JAVASCRIPT DATE LIBRARY TO GET AND MANIPULATE EVENTS
//See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
//For instance, to get all the events today:
Logger.log('________________________________________')
Logger.log('Getting all today\'s events');
var today = new Date();
var events = calById.getEventsForDay(today)
Logger.log('Number of events: ' + events.length);
events.forEach(function(event, index) {
Logger.log('***** Event [' + (index + 1) + '] *****')
Logger.log('Title: ' + event.getTitle());
Logger.log('Created: ' + event.getDateCreated());
Logger.log('Updated: ' + event.getLastUpdated());
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment