Last active
February 17, 2016 22:45
-
-
Save devtanc/e71465094a2b35757e8d to your computer and use it in GitHub Desktop.
Calendars and Events in Google Calendars
This file contains hidden or 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
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