更新Google Calendar API JavaScript中的活动

Ric*_*ell 2 javascript google-calendar-api

我正在尝试使用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);
            });
Run Code Online (Sandbox Code Playgroud)

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

Ric*_*ell 8

管理自己解决这个问题,如果有人碰巧遇到同样的问题.正确的格式是使用' PATCH '与' 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);
            });
Run Code Online (Sandbox Code Playgroud)