Google API Apps脚本日历创建日历事件成功,但删除日历事件失败

时间:2018-12-16 18:43:57

标签: google-apps-script google-api

这些功能在我们测试的Google Corporate Calendars上有效,但在我们生产的Google Corporate Calendars上无效。在生产中,createCalendarEvent函数可以运行,但是deleteCalendarEvent函数失败,并且不返回任何错误。我正在使用具有相同用户和相同应用程序脚本的OpenID connect。用户的安全访问权限已在所有日历上验证为相同。功能如下:

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();
}

1 个答案:

答案 0 :(得分:0)

您可以执行类似的操作以确保您具有所有参数。

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

我将您的创建函数更改为:

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();
}

同样,您可能想用逻辑包装它们以确保拥有所有参数。