Skip to content

Instantly share code, notes, and snippets.

@marcusmoore
Forked from mogsdad/GCalUtils.md
Created September 23, 2015 17:19
Show Gist options
  • Select an option

  • Save marcusmoore/5ad1d9129495d359135e to your computer and use it in GitHub Desktop.

Select an option

Save marcusmoore/5ad1d9129495d359135e to your computer and use it in GitHub Desktop.
Collection of Google Calendar related utility functions for Google Apps Script.
/** @module GCalUtils */
/**
* Gets all events that occur within a given time range,
* and that include the specified guest email in the
* guest list.
*
* @param {Calendar} calen Calendar to search
* @param {Date} start the start of the time range
* @param {Date} end the end of the time range, non-inclusive
* @param {String} guestEmail Guest email address to search for
*
* @return {CalendarEvent[]} the matching events
*
* @see {@link https://developers.google.com/apps-script/reference/calendar/calendar-app?hl=en#getEvents(Date,Date)|CalendarApp.getEvents(Date,Date)}
* @see {@link https://developers.google.com/apps-script/reference/calendar/calendar-event|Class CalendarEvent}
*/
function getEventsWithGuest(calen,start,end,guestEmail) {
var events = calen.getEvents(start, end);
var i = events.length;
while (i--) {
if (!events[i].getGuestByEmail(guestEmail)) {
events.splice(i, 1);
}
}
return events;
}
/**
* Create or update a block reservation for a conference room,
* starting 'blockFrom' days from today.
*/
function updateBlockReservation() {
// Get Calendar
var calName = 'Huddle Room';
var cal = CalendarApp.getCalendarsByName(calName)[0];
var title = 'Reserved'; // Reserved events will have this title
var blockFrom = 7; // Days from now until room is blocked
var today = new Date(); // Today's date, ...
today.setHours(0,0,0,0); // at midnight.
var startDate // Daily block reservation starts here
= new Date(today.getTime() + (blockFrom * 24 * 60 * 60 * 1000));
var endTime = new Date(startDate.getTime() + (24 * 60 * 60 * 1000) - 1);
var recurrence = CalendarApp.newRecurrence().addDailyRule();
// Look for existing block reservation
var series = cal.getEvents(startDate, endTime, {search:title});
if (series.length == 0) {
// No block reservation found - create one.
var reserved = cal.createAllDayEventSeries(title, startDate, recurrence);
}
else {
// Block reservation exists - update the recurrence to start later.
reserved = series[0].getEventSeries();
reserved.setRecurrence(recurrence, startDate);
}
debugger; // Pause if running in debugger
}

Google Calendar Utilities

getEventsWithGuest

Gets all events that occur within a given time range, and that include the specified guest email in the guest list.

###Parameters:###

Name Type Description
calen Calendar Calendar to search
start Date the start of the time range
end Date the end of the time range, non-inclusive
guestEmail String Guest email address to search for
See:

###Returns:### the matching events

Type
CalendarEvent[]

updateBlockReservation

Create or update a block reservation for a conference room, starting 'blockFrom' days from today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment