简体   繁体   中英

Responding to Google Calendar API's push notifications with Google Apps Script

I'd like to use Google Apps Script to update a Google spreadsheet and send emails with the Gmail API whenever changes are made to a range of calendars that I own.

Can the Google Calendar push notifications be used with Google App Script or is another platform of some sort required?

I'm willing to learn whatever is necessary. I understand that a domain name is required receive the notifications.

I appreciate the help!

Unfortunately this is not possible to achieve using Google App Script (at least not as of this writing).

You could try to create a Google Apps Script(GAS) web app and use its url as an endpoint for the Calendar API's push notifications. However, the data payload of a push notification sent by the Calendar API resides in custom HTTP headers which are not currently accessible from a GAS Web App.

There are alternative solutions but they will take you well outside of GSuite's ecosystem and they are unlikely to be free. But if you're up to the task in terms of programming skill and you can afford to invest monetarily in a platform then you should check out Google Cloud Functions. Its a pay-to-play cloud-based mirco-service but if you stay under the monthly free usage quota its practically free.

This is quite a simple task if you're familiar with GAS (Google Apps Script), the code below is only a part of what's required (event.getLastUpdated is the trick) but it gives an idea of how to accomplish what you require. I also have this requirement & it works fine for me...

Set up a spreadsheet with the calendar details, & post your calendar info to it ...

  function getCalendars()
  {
   var cal = CalendarApp.getCalendarsByName(calName)[0]; // loop here with all your Calenders
   var events = cal.getEvents( date1, date2 ); // start date & end date
   for (var x in events ) 
   { 
      var event = events[x];
      var title = event.getTitle();
      var created = event.getDateCreated();
      var lastMod = event.getLastUpdated();
      var lastRead = getLastModifiedFromSpreadSheet();
      var diff= daysDiff(lastMod, read);
      if (diff > 0) {putNewDateInSpreadsheet(); doYourStuff;)
    }
  }

  function daysDiff(a, b) { // change to hours or minutes if required
  var oneDay = 1000 * 60 * 60 * 24;
  return Math.floor(b.getTime() / oneDay) - Math.floor(a.getTime() / oneDay);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM