Google Apps Script: How to check if a calendar event has been marked as deleted manually

风格不统一 提交于 2021-01-27 21:18:19

问题


I've written a Google Apps Script which adds events kept in a spreadsheet to a google calendar. Each event stores the calendar event's ID so that it can update it if the event in the spreadsheet gets modified. The event can still be deleted manually in the calendar so I would like the script to detect this and recreate the event. Unfortunately, I cannot simply check for null when calling getEventById(...) on a calendar object because even when deleted, this function seems to be returning a valid calendar object.

The CalendarEvent API documentation doesn't seem to point to a way to check if the event has been deleted via the UI/manually. Is there a way of accessing this property through the Google Apps API?

Even permanently deleting trashed events does not fix the problem, although it then prevents modifications to the returned event object.

What can I use to check if the event (which still exists) has been "deleted"? The only thing I can think might be of use is the get/setTag() methods for the CalendarEvent object.

Alternatively, this question looks similar, but it feels like there should be a better way than searching through a list of trashed events, and I'm not sure this would get around the issue of having ID strings return valid events that have been "permanently" deleted.

Example

var calendarId = "abc123@group.calendar.google.com"
var calendar = CalendarApp.getCalendarById(calendarId);

var event = calendar.createEvent(...);

// Event is then deleted manually in calendar but 
// ID has been saved in spreadsheet (or elsewhere)

if (calendar.getEventById("the-deleted-event-id") == null) {
    // Create the event (This is never reached)
} else {
    // Update the event
}

回答1:


I have spent quite a time solving this and I cant udnerstand why there is no better way how to solve it, directly by CalendarApp. What you need is this:

var eventId = "5qif5n15v46c0jt5gq20a39dqu@google.com".split("@")[0];
if(Calendar.Events.get("yourCalendarId", eventId).status === "cancelled")
        CODE://what to do if event was deleted (manually)

Notes:

  1. As API v3.0 uses as eventid only the part before @, so you need to split (strip rest)
  2. You need Calendar API v3.0 first activate.

You will find more info about in this question: Checking a Google Calendar Event's Existence




回答2:


I found that the best way to do this is to check whether the event in question is included when asking the calendar for events during the time period of event in question:

var event = calendar.getEventById(eventId);
var events = calendar.getEvents(event.getStartTime(),event.getEndTime());
var isEventDeleted = events.some((event) => event.getId() == eventId);

Methods used:

https://developers.google.com/apps-script/reference/calendar/calendar#getEventById(String) https://developers.google.com/apps-script/reference/calendar/calendar#geteventsstarttime,-endtime https://developers.google.com/apps-script/reference/calendar/calendar-event#getstarttime https://developers.google.com/apps-script/reference/calendar/calendar-event#getendtime https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some



来源:https://stackoverflow.com/questions/48122824/google-apps-script-how-to-check-if-a-calendar-event-has-been-marked-as-deleted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!