Home > Mobile >  google calendar api v3 get id of last modified event
google calendar api v3 get id of last modified event

Time:11-01

I want to script behavior when people insert event in my calendar. (e.g. when they add something into my "focus time"). I was able to connect an appscript "trigger" to call onEventUpdate. Sadly AppScript does not give you the event id for the event that was modified... (can you confirm the API does not offer this?).

So I tried to fetch the "last updated" events instead:

function getOptions() {
  var now = new Date();
  var yesterday = new Date();
  yesterday.setDate(now.getDate() - 1);
  
  console.log(yesterday.toISOString())

  return {
    updateMin: yesterday.toISOString(),
    maxResults: 2,
    orderBy: 'updated',
    singleEvents: true,
    showDeleted: false
  }
}

function onEventUpdate() {
  var options = getOptions();
  var calendarId = Session.getActiveUser().getEmail();
  // var calendar = CalendarApp.getCalendarById(calendarId);
  // console.log(calendar.getName());

  var events = Calendar.Events.list(calendarId, options);
  if(!events.items) return
  for (var i = 0; i < events.items.length; i  ) {
    var event = events.items[i];
    console.log(event.summary   " @ "   event.start['dateTime']);
  }
}

Yet, I have just modified an event, but instead I am getting events from the past (i.e. August, 2mo ago...):

5:11:21 PM  Notice  Execution started
5:11:21 PM  Info    2022-10-29T00:11:21.826Z
5:11:22 PM  Info    Old Meeting @ 2022-08-08T17:00:00-07:00
5:11:22 PM  Info    Old Meeting @ 2022-08-03T14:00:00-07:00
5:11:22 PM  Notice  Execution completed

Thoughts?

CodePudding user response:

I believe your goal is as follows.

  • You want to retrieve the last updated event in a Google Calendar using Google Apps Script.

In the current stage, orderBy of "Events: list" can be used with only "ascending". When I saw your script, I thought that the reason for your current issue might be due to this. If "descending" was used with orderBy, your script might be able to be used. But, in the current stage, it seems that this cannot be used.

So, in the current stage, in order to retrieve the last updated event, I thought that it is required to retrieve all events with your getOptions(), and the last element is required to be retrieved. When this is reflected in your script, how about the following modification?

Modified script:

function getOptions() {
  var now = new Date();
  var yesterday = new Date();
  yesterday.setDate(now.getDate() - 1);

  console.log(yesterday.toISOString())

  return {
    updatedMin: yesterday.toISOString(),
    maxResults: 2500, // Modified
    orderBy: 'updated',
    singleEvents: true,
    showDeleted: false
  }
}

function onEventUpdate() {
  var options = getOptions();
  var calendarId = Session.getActiveUser().getEmail();
  
  // I modified the below script.
  var eventList = [];
  var pageToken = null;
  do {
    options.pageToken = pageToken;
    var events = Calendar.Events.list(calendarId, options);
    if (events.items.length > 0) {
      eventList = [...eventList, ...events.items];
    }
    pageToken = events.nextPageToken;
  } while (pageToken);
  var lastUpdatedEvent = eventList.pop(); // You can retrieve the last updated event.
  var lastUpdatedEventId = lastUpdatedEvent.id; // You can retrieve the event ID of the last updated event.
}

Note:

  • About I was able to connect an appscript "trigger" to call onEventUpdate. Sadly AppScript does not give you the event id for the event that was modified, how about reporting this to the Google issue tracker as a future request?

Reference:

CodePudding user response:

Alright, I achieved what I wanted to (thanks for spotting my typo). I take some risk, as I assume there is no more than 50 edited events in the last hour (although risk could be reduced by making this delta smaller).

This is what the overly-convoluted way appscript needs (i.e. they could have just given me a calendar-event object as an argument to the trigger? oh well)

This is just a simple example, as now I can programmatically edit my calendar at will :)

function getOptions() {
  var now = new Date();
  TIME_DIFF = 60 * 60 * 1000;
  var earlier = new Date(now.getTime() - TIME_DIFF)
  return {
    updatedMin: earlier.toISOString(),
    maxResults: 50,
    orderBy: 'updated',
    singleEvents: true,
    showDeleted: false
  }
}

function getLastEditedEvent() {
  // returns https://developers.google.com/apps-script/reference/calendar/calendar-event
  var options = getOptions();
  var calendarId = Session.getActiveUser().getEmail();
  var events = Calendar.Events.list(calendarId, options);
  if(!events.items) return undefined;
  var _event = events.items[events.items.length-1];
  return CalendarApp.getEventById(_event.id);
}

function onEventUpdate() {
  // sadly event update contains no information
  var event = getLastEditedEvent()
  if(event == undefined) return;
  console.log('Modifying: '   event.getTitle()   ' @ '   event.getStartTime());
  event.setColor(CalendarApp.EventColor.PALE_BLUE);
}
  • Related