Google Apps脚本,导致事件附件错误

时间:2018-05-17 16:59:17

标签: google-apps-script google-calendar-api

我正在尝试将所有事件从一个日历复制到另一个日历,保留事件颜色和附件。 以下是用于测试目的。完全有效,直到我添加了一些代码here来获取事件附件。 Google API显然已正确激活。

function myFunction() {
  var calendarSource = CalendarApp.getCalendarById("somecalendarIDhere@group.calendar.google.com");
  var calendarDestination = CalendarApp.getCalendarById("primary");
  var eventToCopy = calendarSource.getEvents(new Date("July 21, 2009 EST"), new Date("July 22, 2018 EST"));

  for (var i in eventToCopy){
      // line below throws error
      var res = Calendar.Events.get(calendarSource, eventToCopy[i].getId(), {fields: "attachments/fileId"}); // THROWS "not found" error in debugger
      var fileIds = res.attachments.map(function(e){return e.fileId});
      var options = {description:eventToCopy[i].getDescription(), location: eventToCopy[i].getLocation(), attachments:fileIds}; 
      var newEvent = calendarDestination.createEvent(eventToCopy[i].getTitle(), eventToCopy[i].getStartTime(),eventToCopy[i].getEndTime(), options );
      newEvent.setColor(eventToCopy[i].getColor());
}
}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

calendarSource是一个日历对象。 Calendar.Events.get正在寻找calendarID。

尝试:

var res = Calendar.Events.get(calendarSource.getId().split('@')[0], eventToCopy[i].getId(), {fields: "attachments/fileId"}); // THROWS "not found" error in debugger