首页 > 解决方案 > 尝试更新事件时出现“无效的参数数量”错误

问题描述

我正在尝试从谷歌应用脚​​本更新日历事件。我有日历 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),其中事件信息是一个只有描述的对象,但返回的错误是错误调用。

我想我在我的对象论点中遗漏了一些东西。

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

解决方案


问题:

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

  • 但是,我认为您的方法行不通,因为您没有event objectCalendar.Events.update()函数中传递 an 。结构应该是这样的:

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

当然,不要忘记从Resources => Advanced Google services打开 Calendar API 。

参考:


推荐阅读