简体   繁体   中英

Google API Apps Script Calendar Create Calendar Event Succeeds but Delete Calendar Event Fails

These functions work on our test Google Corporate Calendars but fail on our production Google Corporate Calendars. In production, the function, createCalendarEvent, works but the function, deleteCalendarEvent, fails and no error is returned. I am using OpenID connect with the same user and same apps script. The security access for the user has been verified to be the same on all calendars. Here are the functions:

function createCalendarEvent(calendarId, startDate, endDate, eventTitle, eventDescription) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var start = new Date(startDate);
  var end = new Date(endDate);
  var options = {
    description: eventDescription,
    etags: {
      "title": eventTitle,
      "start": start,
      "end": end
    }
  }
  var event = cal.createAllDayEvent(eventTitle, start, end, options);
  return event.getId();
}

function deleteCalendarEvent(calendarId, eventId)  {
  var cal = CalendarApp.getCalendarById(calendarId);
  var event = cal.getEventById(eventId);
  event.deleteEvent();
}

You could do something like this to insure that you have all parameters.

function deleteCalendarEvent(calendarId, eventId) {
  if(calendarId && eventId){
    CalendarApp.getCalendarById(calendarId).getEventById(eventId).deleteEvent();
  }else{
    throw('Error: in function deleteCalendarEvent. Invalid parameters.');
  }
}

I'd change your create function to:

function createCalendarEvent(calendarId, startDate, endDate, eventTitle, eventDescription) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var start = new Date(startDate);
  var end = new Date(endDate);
  var options = {"description": eventDescription};
  var event = cal.createAllDayEvent(eventTitle, start, end, options);
  return event.getId();
}

And again you might want to wrap them with logic to insure that you have all parameters.

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