Reputation: 1449
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.
var calendarId = "[email protected]"
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
}
Upvotes: 5
Views: 3652
Reputation: 1
Another solution would be to search only events that have not yet been deleted, which will cause deleted events to not be found.
let result = Calendar.Events.list(<Calendar_ID>, {showDeleted: false});
Upvotes: 0
Reputation: 1
I found the Simpler Way is check if the Return is null or Not. Null = event not found
https://developers.google.com/apps-script/reference/calendar/calendar#geteventbyidicalid
CalendarEvent — the event with the given ID, or null if the event doesn't exist or the user cannot access it.
Upvotes: -1
Reputation: 1337
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
Upvotes: 2
Reputation: 344
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 = "[email protected]".split("@")[0];
if(Calendar.Events.get("yourCalendarId", eventId).status === "cancelled")
CODE://what to do if event was deleted (manually)
Notes:
You will find more info about in this question: Checking a Google Calendar Event's Existence
Upvotes: 2