Running Google Apps Script by Event Notification from Google Calendar

Gists

Overview

This is a simple method running Google Apps Script by the event notification from Google Calendar.

Description

There are several event triggers in Google Apps Script. Ref1, Ref2 There is an event trigger for Google Calendar. The official document says An installable calendar event trigger runs when a user's calendar events are updated—created, edited, or deleted.. Ref3 If I want to use the trigger when the event in the calendar is starts and finished, it is required to use Calendar API by preparing the URL for receiving from Google. Ref4, Ref5 Also, there is the method that the start time are retrieved from all event lists and run the script for each start time. But in this case, it is required to check new events and manage the time-driven triggers for running the script. I thought that those are the higher hurdle for me. So I thought a workaround.

Here, I would like to introduce the simple method for running Google Apps Script by the event notification from Google Calendar.

Flow

In order to use the event notification from Google Calendar as the trigger for running the Google Apps Script, IFTTT and Web Apps are used. The simple flow is as follows.

  1. An event is started at Google Calendar.
  2. Send the notification from Google Calendar to IFTTT.
  3. Request from IFTTT to Web Apps.

By above flow, Google Apps Script can be run when the event of Google Calendar is started.

Usage

Here, I would like to introduce how to set up this workaround.

1. Create new project of Google Apps Script.

You can see how to create it at this official document.

2. Copy and paste script.

Please copy and paste the following script to the script editor of the created GAS project.

function doGet(e) {
  var p = e.parameter;
  var object = {
    title: p.title,
    start: p.start,
    end: p.end,
    eventUrl: p.eventUrl
  };

  // do something
  //
  // Here, please put the script you want to run when the event is started.
  //

  return ContentService.createTextOutput("Done.");
}

3. Deploy Web Apps:

  1. On the script editor, Open a dialog box by “Publish” -> “Deploy as web app”.
  2. Select “Me” for “Execute the app as:".
  3. Select “Anyone, even anonymous” for “Who has access to the app:".
    • This setting is for a test situation.
    • You can also access with the access token by setting “Only myself” instead of “Anyone, even anonymous”.
  4. Click “Deploy” button as new “Project version”.
  5. Automatically open a dialog box of “Authorization required”.
    1. Click “Review Permissions”.
    2. Select own account.
    3. Click “Advanced” at “This app isn’t verified”.
    4. Click “Go to ### project name ###(unsafe)”
    5. Click “Allow” button.
  6. Click “OK”.
  7. Copy the URL of Web Apps. It’s like https://script.google.com/macros/s/###/exec.
    • When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.

4. Set up IFTTT:

Please log in to IFTTT and open “Create your own” with “Create”.

  1. Set This of “If This Then That”.

  2. Search “Google Calendar” at “Choose action service”.

  3. Select “Any event starts” at “Choose trigger”.

  4. Set “Which calendar?” and “Time before event starts”. Here, when “Time before event starts” is set as 0 minutes, you can run the Google Apps Script when the event is started.

  5. Set That of “If This Then That”.

  6. Search “Webhooks” at “Choose action service”.

  7. Select “Make a web request” at “Choose action”.

  8. Set https://script.google.com/macros/s/###/exec?title={{Title}}&start={{Starts}}&end={{Ends}}&eventUrl={{EventUrl}} to “URL” at Set “Complete action fields”. Please replace ### for the URL of your Web Apps. And click “Create action”.

The setup of this workaround is done.

 Share!