尝试更新事件时出现“参数数量无效”错误

时间:2020-09-03 15:54:39

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

我正在尝试通过Google Apps脚本更新日历事件。我有日历ID,事件ID和要尝试作为变量更新的对象:

   var eventinfo = {
   "calendarId": calID
      "eventId": eventID,
      "resource": {
        "description": "1234"
      }
   };

 //update the event description and location

   var updater;
   try {
    updater = Calendar.Events.update(eventinfo);
    Logger.log('Successfully updated event: ' + i);
   } catch (e) {
    Logger.log('Fetch threw an exception: ' + e);
    } 

我收到此错误:

Fetch引发了一个异常:异常:提供的参数数量无效。预计只有3-5个

以前,我曾尝试以这种方式.update(calID, eventID, eventinfo)调用更新方法,其中事件信息只是一个带有描述的对象,但是返回的错误提示是错误的呼叫。

我认为我的对象参数中缺少某些内容。

1 个答案:

答案 0 :(得分:2)

问题:

  • 首先,您忘记了eventinfo定义中的逗号 在第一行和第二行之间。

  • 但是,我认为您的方法无效,因为您没有 在event object函数中传递Calendar.Events.update()。结构应该是这样的:

    Calendar.Events.update(
       event,
       calendarId,
       event.id
     ); 
    

解决方案/示例:

  • 下面的示例在将来更新第一个事件。在 特别是,它会更新标题(摘要),说明和位置,但 随意修改,如果需要的话:

    function updateNextEvent() {
      const calendarId = 'primary';
      const now = new Date();
      const events = Calendar.Events.list(calendarId, {
        timeMin: now.toISOString(),
        singleEvents: true,
        orderBy: 'startTime',
        maxResults: 1
      });
    
     var event = events.items[0]; //use your own event object here if you want
    
     event.location = 'The Coffee Shop';
     event.description = '1234';
     event.summary = 'New event';
     event = Calendar.Events.update(
          event,
          calendarId,
          event.id
        ); 
    }
    

当然,不要忘记从资源 => 高级Google服务打开Calendar API。

参考文献: