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.

Revisions

  1. @mogsdad mogsdad revised this gist Nov 24, 2014. 2 changed files with 154 additions and 1 deletion.
    91 changes: 90 additions & 1 deletion GCalUtils.md
    Original file line number Diff line number Diff line change
    @@ -71,4 +71,93 @@ the matching events
    ## updateBlockReservation ##

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

    ## getCalId ##

    Get the calendar ID for a named calendar.
    Throws an error if the calendar name is not accessible by the user.

    ###Parameters:###
    <table class="params">
    <thead>
    <tr>
    <th>Name</th>
    <th>Type</th>
    <th class="last">Description</th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td class="name"><code>calName</code></td>
    <td class="type">
    string
    </td>
    <td class="description last">Calendar Name</td>
    </tr>
    </tbody>
    </table>

    ###Returns:###
    the Calendar ID
    <dl>
    <dt>
    Type
    </dt>
    <dd>
    <span class="param-type">string</span>
    </dd>
    </dl>

    ## shareCalendar ##

    Set up calendar sharing for a single user. Refer to
    https://developers.google.com/google-apps/calendar/v3/reference/acl/insert.
    Uses Advanced Calendar Service, which must be enabled via Developer's Dashboard.

    ###Parameters:###
    <table class="params">
    <thead>
    <tr>
    <th>Name</th>
    <th>Type</th>
    <th class="last">Description</th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td class="name"><code>calId</code></td>
    <td class="type">
    string
    </td>
    <td class="description last">Calendar ID</td>
    </tr>
    <tr>
    <td class="name"><code>user</code></td>
    <td class="type">
    string
    </td>
    <td class="description last">Email address to share with</td>
    </tr>
    <tr>
    <td class="name"><code>role</code></td>
    <td class="type">
    Date
    </td>
    <td class="description last">Optional permissions, default = "reader":
    * "none, "freeBusyReader", "reader", "writer", "owner"</td>
    </tr>
    </tbody>
    </table>


    ###Returns:###
    See https://developers.google.com/google-apps/calendar/v3/reference/acl#resource.
    <dl>
    <dt>
    Type
    </dt>
    <dd>
    <span class="param-type">aclResource</span>
    </dd>
    </dl>
    64 changes: 64 additions & 0 deletions gCalUtils.js
    Original file line number Diff line number Diff line change
    @@ -60,3 +60,67 @@ function updateBlockReservation() {

    debugger; // Pause if running in debugger
    }

    /**
    * Get the calendar ID for a named calendar.
    * Throws an error if the calendar name is not accessible by the user.
    *
    * @param {string} calName Calendar Name
    *
    * @returns {string} Calendar ID
    */
    function getCalId( calName ) {
    var cal = CalendarApp.getCalendarsByName(calName);
    if (cal) {
    return cal[0].getId();
    }
    else {
    throw new Error( "Calendar not found: " + calName );
    }
    }

    /**
    * Set up calendar sharing for a single user. Refer to
    * https://developers.google.com/google-apps/calendar/v3/reference/acl/insert.
    * Uses Advanced Calendar Service, which must be enabled via Developer's Dashboard.
    *
    * @param {string} calId Calendar ID
    * @param {string} user Email address to share with
    * @param {string} role Optional permissions, default = "reader":
    * "none, "freeBusyReader", "reader", "writer", "owner"
    *
    * @returns {aclResource} See https://developers.google.com/google-apps/calendar/v3/reference/acl#resource
    */
    function shareCalendar( calId, user, role ) {
    role = role || "reader";

    var acl = null;

    // Check whether there is already a rule for this user
    try {
    var acl = Calendar.Acl.get(calId, "user:"+user);
    }
    catch (e) {
    // no existing acl record for this user - as expected. Carry on.
    }
    debugger;

    if (!acl) {
    // No existing rule - insert one.
    acl = {
    "scope": {
    "type": "user",
    "value": user
    },
    "role": role
    };
    var newRule = Calendar.Acl.insert(acl, calId);
    }
    else {
    // There was a rule for this user - update it.
    acl.role = role;
    newRule = Calendar.Acl.update(acl, calId, acl.id)
    }

    return newRule;
    }
  2. @mogsdad mogsdad revised this gist Nov 21, 2013. 2 changed files with 40 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion GCalUtils.md
    Original file line number Diff line number Diff line change
    @@ -66,4 +66,9 @@ the matching events
    <dd>
    <span class="param-type">CalendarEvent[]</span>
    </dd>
    </dl>
    </dl>

    ## updateBlockReservation ##

    Create or update a block reservation for a conference room,
    starting 'blockFrom' days from today.
    34 changes: 34 additions & 0 deletions gCalUtils.js
    Original file line number Diff line number Diff line change
    @@ -26,3 +26,37 @@ function getEventsWithGuest(calen,start,end,guestEmail) {
    }
    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
    }
  3. @mogsdad mogsdad revised this gist Sep 7, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions GCalUtils.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Google Calendar Utilities #

    ## getEventsWithGuest ##

    Gets all events that occur within a given time range,
  4. @mogsdad mogsdad renamed this gist Sep 7, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @mogsdad mogsdad created this gist May 9, 2013.
    28 changes: 28 additions & 0 deletions GCalUtils.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    /** @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;
    }
    67 changes: 67 additions & 0 deletions GCalUtils.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    ## getEventsWithGuest ##

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

    ###Parameters:###
    <table class="params">
    <thead>
    <tr>
    <th>Name</th>
    <th>Type</th>
    <th class="last">Description</th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td class="name"><code>calen</code></td>
    <td class="type">
    Calendar
    </td>
    <td class="description last">Calendar to search</td>
    </tr>
    <tr>
    <td class="name"><code>start</code></td>
    <td class="type">
    Date
    </td>
    <td class="description last">the start of the time range</td>
    </tr>
    <tr>
    <td class="name"><code>end</code></td>
    <td class="type">
    Date
    </td>
    <td class="description last">the end of the time range, non-inclusive</td>
    </tr>
    <tr>
    <td class="name"><code>guestEmail</code></td>
    <td class="type">
    String
    </td>
    <td class="description last">Guest email address to search for</td>
    </tr>
    </tbody>
    </table>

    <dl class="details">
    <dt class="tag-see">See:</dt>
    <dd class="tag-see">
    <ul>
    <li><a href="https://developers.google.com/apps-script/reference/calendar/calendar-app?hl=en#getEvents(Date,Date)">CalendarApp.getEvents(Date,Date)</a></li>
    <li><a href="https://developers.google.com/apps-script/reference/calendar/calendar-event">Class CalendarEvent</a></li>
    </ul>
    </dd>
    </dl>

    ###Returns:###
    the matching events
    <dl>
    <dt>
    Type
    </dt>
    <dd>
    <span class="param-type">CalendarEvent[]</span>
    </dd>
    </dl>