日历 API 仅使用 Google Apps 脚本修改以下事件

Calendar API to modify the following events only using Google Apps Script

我有从 2021 年 2 月 8 日开始每周一重复的活动。我想修改即将到来的 2021 年 2 月 28 日,以便所有未来的活动将在每周三重复。请注意,事件必须更新所有后续事件(保留 eventID)

var resource = Calendar.Events.get(calendar.getId(), eventId);
            if (resource) {
                resource.start.date = Utilities.formatDate(new Date("28-02-2021"), Session.getScriptTimeZone(), "yyyy-MM-dd");
                resource.end.date = Utilities.formatDate(new Date("28-02-2021"), Session.getScriptTimeZone(), "yyyy-MM-dd");
                var result = Calendar.Events.patch(resource, calendar.getId(), eventId)
            }

此代码将更新所有重复发生的事件,包括过去的事件。但我只需要更新以下事件。

根据 Recurring Events 的官方文档,保留 eventId 是不可能的,因为您需要根据您的首选修改创建一个新事件并更新旧的重复事件并更改其 UNTIL结束系列的参数。

Modifying all following instances

为了在给定(目标)实例上或之后更改重复事件的所有实例,您必须发出两个单独的 API 请求。这些请求将原始重复事件分为两部分:原始事件保留没有更改的实例,而新重复事件具有应用更改的实例:

  1. 调用events.update()到trim待更新实例的原始循环事件。通过将 RRULE 的 UNTIL 组件设置为指向第一个目标实例的开始时间之前来执行此操作。或者,您可以设置 COUNT 组件而不是 UNTIL.

  2. 调用 events.insert() 创建一个新的周期性事件,除了您尝试进行的更改外,所有数据都与原始数据相同。新的重复事件必须具有目标实例的开始时间。


(更新)

示例代码:

function updateRecurr() {
  var calendarId = 'c_9cdqeqqluk7vsessartfxxxxx';
  var eventId = '17a97q639vmtkoghcxxxxx';

  //Update eventseries, change "until" parameter in rrule to Feb. 27, 2020
  var event = {
    recurrence:[
      "RRULE:FREQ=WEEKLY;WKST=SU;UNTIL=20210227;BYDAY=MO"
    ]
  }
  updatedEvent = Calendar.Events.patch(event,calendarId,eventId);
  Logger.log(updatedEvent);

  //Create new event having the same important info with different start time and rrule
  var newEvent = {
    summary: updatedEvent.summary,
    description: updatedEvent.description,
    start:{
      date: Utilities.formatDate(new Date("March 3, 2021"), Session.getScriptTimeZone(), "yyyy-MM-dd")
    },
    end:{
      date: Utilities.formatDate(new Date("March 3, 2021"), Session.getScriptTimeZone(), "yyyy-MM-dd")
    },
    recurrence:[
      "RRULE:FREQ=WEEKLY;WKST=SU;UNTIL=20210331;BYDAY=WE"
    ]
  }

  var newEventSeries = Calendar.Events.insert(newEvent,calendarId);
  Logger.log(newEventSeries);
  
}

输出:

之前:

之后:


注:

在新创建的事件系列中,您会注意到我将开始日期设置为“2021 年 3 月 3 日”,这是因为如果我将其设置为“2021 年 2 月 28 日”,那么事件也会在该特定日期创建。但在现实生活中,当我使用 Google 日历创建一个事件并将开始日期设置为“2021 年 2 月 28 日”,事件系列每周三发生。活动中设置的实际开始日期为“2021年3月3日”。

只需复制您之前的活动系列中的所有其他重要活动详细信息(如果有的话)。在提供的示例中,我只是配置了事件标题和描述。