Skip to content

Instantly share code, notes, and snippets.

@matthiaseisen
Created September 21, 2012 14:21
Show Gist options
  • Save matthiaseisen/3761721 to your computer and use it in GitHub Desktop.
Save matthiaseisen/3761721 to your computer and use it in GitHub Desktop.
Google Apps Script: how to create calendar events from contact's dates
// This script uses the underscoreGS library: https://sites.google.com/site/scriptsexamples/custom-methods/underscoregs
var MONTHS = [ ContactsApp.Month.JANUARY,
ContactsApp.Month.FEBRUARY,
ContactsApp.Month.MARCH,
ContactsApp.Month.APRIL,
ContactsApp.Month.MAY,
ContactsApp.Month.JUNE,
ContactsApp.Month.JULY,
ContactsApp.Month.AUGUST,
ContactsApp.Month.SEPTEMBER,
ContactsApp.Month.OCTOBER,
ContactsApp.Month.NOVEMBER,
ContactsApp.Month.DECEMBER ];
// Take a date from Contact and make it usable for Calendar
function convertDate(dateFromContact) {
var now = new Date();
var monthAsInt = underscore._indexOf(MONTHS, dateFromContact.getMonth());
if (monthAsInt == -1) {
return false;
}
return new Date( now.getFullYear(), monthAsInt, dateFromContact.getDay() );
}
function rebuild() {
// [ contact1, ..., contactN ]
var contactCache = ContactsApp.getContacts();
// { 'calendar1': calendar1, ..., 'calendarN': calendarN }
var calendarCache = {};
// { 'calendar1': [ event1, event2, ..., eventN ], ..., 'calendarN': [ event1, event2, ..., eventN ] }
var eventCache = {};
var now = new Date();
var aYearFromNow = new Date(now.getFullYear() + 1, now.getMonth(), now.getDate());
if (contactCache.length > 0) {
// Iterate over all Contacts:
for (var i = 0; i < contactCache.length; i++) {
var currentContact = contactCache[i];
var allDatesOfCurrentContact = currentContact.getDates();
if (allDatesOfCurrentContact.length > 0) {
// Iterate over all Dates of the current Contact:
for (var j = 0; j < allDatesOfCurrentContact.length; j++) {
var currentDateFromContact = allDatesOfCurrentContact[j];
// Has this Calendar already been retrieved?
if (underscore._has(calendarCache, currentDateFromContact.getLabel())) {
var calendarForCurrentDate = calendarCache[currentDateFromContact.getLabel()];
// This should be true since we wouldn't retrieve a calendar without its events:
if (underscore._has(eventCache, currentDateFromContact.getLabel())) {
// If this Date has already been retrieved there's obviously a CalendarEvent for it. It that case our work here is done...
if (underscore._any(eventCache[currentDateFromContact.getLabel()], function(eventToLookFor){
var dateToLookFor = eventToLookFor.getStartTime();
if ( eventToLookFor.getTag('contactId') == currentContact.getId() && eventToLookFor.isAllDayEvent() ) {
if ( currentDateFromContact.getDay() == dateToLookFor.getDate() && dateToLookFor.getMonth() == underscore._indexOf(MONTHS, currentDateFromContact.getMonth()) ) {
return true
}
}
return false
})) {
Logger.log('EVENT EXISTS');
}
// If there isn't a CalendarEvent for this Date we need to create it:
else {
var dateForNewEvent = new Date( now.getFullYear(), underscore._indexOf(MONTHS, currentDateFromContact.getMonth()), currentDateFromContact.getDay() );
var dateForNewEvent = convertDate(currentDateFromContact);
if ( dateForNewEvent ) {
var titleForNewEvent = currentDateFromContact.getLabel() + ': ' + currentContact.getFullName();
var newEventSeriesId = calendarForCurrentDate.createAllDayEventSeries(titleForNewEvent, dateForNewEvent, CalendarApp.newRecurrence().addYearlyRule()).getId();
// To edit our EventSeries we have to reload it from the server to prevent an etag error (http://code.google.com/p/google-apps-script-issues/issues/detail?id=264):
calendarForCurrentDate.getEventSeriesById(newEventSeriesId).setTag('contactId', currentContact.getId());
calendarForCurrentDate.getEventSeriesById(newEventSeriesId).setVisibility(CalendarApp.Visibility.PRIVATE);
calendarForCurrentDate.getEventSeriesById(newEventSeriesId).addEmailReminder(1);
Logger.log('EVENT CREATED');
}
}
}
}
// Seems like we didn't retrieve this calendar yet...
else {
// Let's see if we can find it on the server:
var allMatchingCalendars = CalendarApp.getCalendarsByName(currentDateFromContact.getLabel());
if (allMatchingCalendars.length > 0) {
var calendarForCurrentDate = allMatchingCalendars[0];
Logger.log('CALENDAR EXISTS: ' + calendarForCurrentDate.getName());
}
// We couldn't find it. This means we have to create it:
else {
var calendarForCurrentDate = CalendarApp.createCalendar(currentDateFromContact.getLabel());
Logger.log('CREATING CALENDAR: ' + calendarForCurrentDate.getName());
}
// Let's load this Calendar and it's Event into our caching variables:
Logger.log('CACHING...');
calendarCache[currentDateFromContact.getLabel()] = calendarForCurrentDate;
eventCache[currentDateFromContact.getLabel()] = calendarForCurrentDate.getEvents(now, aYearFromNow);
}
}
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment