【问题标题】:Listing Google calendar events using API使用 API 列出 Google 日历事件
【发布时间】:2019-08-26 16:11:36
【问题描述】:

我正在尝试编写一个 Google Apps 脚本来修改日历事件,因此我修改了一个示例以首先列出事件。当我尝试调试它时,它会在“events = Calendar.Events.list(calendarId, options);”这一行报告“未定义日历”的错误。

我已启用高级日历 API,并且我的脚本基于 Google 文档中的一个,因此我假设其中一个有效。我还需要做些什么来访问相关的对象和方法吗?

/*
Adapted from code in https://developers.google.com/apps-script/advanced/calendar
*/
function syncColourCode() {
  var calendarId = CalendarApp.getDefaultCalendar();
  var properties = PropertiesService.getUserProperties();
  var fullSync = properties.getProperty('fullSync');  // sync status is stored in user properties
  var options = {
    maxResults: 100
  };
  var syncToken = properties.getProperty('syncToken'); // pointer token from last sync also stored in user properties
  if (syncToken && !fullSync) {                        // if there is a sync token from last time and sync status has not been set to full sync
    options.syncToken = syncToken; // adds the current sync token to the list of sync options
  } else {
    // Sync events from today onwards.
    options.timeMin = new Date().toISOString(); //change to new Date().toISOString() from getRelativeDate(-1, 0).toISOString()
  }

  // Retrieve events one page at a time.
  var events;
  var pageToken;
  do {
    try {
      options.pageToken = pageToken;
      events = Calendar.Events.list(calendarId, options);
    } catch (e) {

【问题讨论】:

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


【解决方案1】:

不是 google-apps 专家,但通过查看代码,我发现了一个可能的问题。在任何时候我都看不到您的代码检查 getDefaultCalendar() 是否实际上返回了有效的日历 ID。稍后,您的代码会在假设它很好的情况下使用该 ID。你检查了返回的 calendarId 的值吗?

有时您必须更深入地阅读消息,但我总是尝试从信任错误返回开始。在这种情况下,“日历未定义”让我质疑 calendarId 的值。

【讨论】:

    【解决方案2】:

    Google 似乎做了一些更改,因此 AppScript API 中没有日历引用。

    无论如何,你可以使用这个 API 来获取事件: CalendarApp.getEvents(开始时间,结束时间) https://developers.google.com/apps-script/reference/calendar/calendar-app#geteventsstarttime-endtime

    以下是我在 google sheet 中运行的示例函数。

    function listEventsWithinTwoMonth(){
      var calendar = CalendarApp.getDefaultCalendar();  
      var spreadsheet = SpreadsheetApp.getActiveSheet();
    
      var now = new Date();
      var twoMonthFromNow = new Date(now.getTime() + (24 * 60 * 60 * 30 * 4 * 1000));
      var events = calendar.getEvents(now, twoMonthFromNow);
      if (events.length > 0) {
        // Header Rows
        spreadsheet.appendRow(["#่","id","StartTime","EndTime","Title","Description"]);
        for (i = 0; i < events.length; i++) {
          var event = events[i];
          Logger.log("" + (i+1) + event.getId() +" "+ event.getStartTime()+" "+event.getEndTime()+" "+event.getTitle()+" "+event.getDescription())
          spreadsheet.appendRow([(i+1),event.getId(),event.getStartTime(),event.getEndTime(),event.getTitle(),event.getDescription()]);
        }
      } else {
        Logger.log('No upcoming events found.');
      }
    }
    

    希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      CalendarApp.getDefaultCalendar() 检索类 Calendar 的对象,该对象具有多个属性,其中包括一个 Id。

      要获取日历ID,需要定义

      var calendarId = CalendarApp.getDefaultCalendar().getId();

      【讨论】:

        猜你喜欢
        • 2015-11-04
        • 2014-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-12
        • 1970-01-01
        • 2018-12-29
        相关资源
        最近更新 更多