更新 Google 日历 API JavaScript 中的事件

Update Event in Google Calendar API JavaScript

我正在尝试使用 JavaScript 更新 Google 日历中的事件,但似乎无法实现对我指定的特定变量的简单更新。下面的代码是我到目前为止与其他类似组合一起尝试过的代码:

            var event = {};

            // Example showing a change in the location
            event = {"location": "New Address"};

            var request = gapi.client.calendar.events.update({
                'calendarId': 'primary',
                'eventId': booking.eventCalendarId, // Event ID stored in database
                'resource': event
            });

            request.execute(function (event) {
               console.log(event);
            });

不管我尝试遵循的 API Reference 是什么,我都尝试获取事件本身并传递该引用以尝试更新特定变量。但是,使用上面最接近的工作示例代码,我注意到控制台建议我缺少开始和结束日期作为最小参数。我显然可以将它添加到 'event' 对象,但这并不有效,因为我只想更新我指定的字段。唯一的其他选择是简单地删除事件并创建一个新事件 - 必须有更简单的方法,否则我完全错过了一些东西。

如果有人碰巧遇到同样的问题,我自己设法解决了这个问题。正确的格式是使用“PATCH' versus 'UPDATE”,它允许更新特定字段,而不必设置 'UPDATE'.

所需的最小字段范围

这是我发现的解决问题的正确代码,包括通过首先获取事件进行的轻微初始编辑:

var event = gapi.client.calendar.events.get({"calendarId": 'primary', "eventId": booking.eventCalendarId});

// Example showing a change in the location
event.location = "New Address";

var request = gapi.client.calendar.events.patch({
    'calendarId': 'primary',
    'eventId': booking.eventCalendarId,
    'resource': event
});

request.execute(function (event) {
    console.log(event);
});