【问题标题】:Google Apps Script, get event attachments errorGoogle Apps 脚本,获取事件附件错误
【发布时间】:2018-05-18 20:24:15
【问题描述】:

我正在尝试将所有事件从一个日历复制到另一个日历,同时保留事件颜色和附件。 以下是为了测试目的。完美运行,直到我添加了一些找到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());
}
}

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

    • 感谢伙伴的帮助。同样的错误"Not Found (line 22, file "Code")Dismiss"......
    • 也许事件 id 是错误的,因为这是使用 gapps 内置方法检索的?
    • 好的,发现问题了:GAS 内置 getId() 将返回带有“@google.com”后缀的 eventid,而 API event['id'] 将返回不带后缀的事件 id。后者有效,但需要Calendar.Events.list 才能工作
    • 好收获。我现在记得几年前在一个项目中遇到了日历 ID 问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 2014-11-16
    相关资源
    最近更新 更多