谷歌日历中的重复事件是如何工作的


How recurring events in google calendar work

我尝试实现两种方式将应用程序与谷歌日历同步。我找不到合理的描述复发事件是如何工作的。它们在主事件中是否存在物理重复(有自己的ID)?Google日历API(listEvents)只返回带有重复(字符串)的主事件。当复发没有自己的ID时,如何删除它们?当我从API(listEvents)数据中的系列(在谷歌日历中)中删除一个递归事件时,并没有提到丢失的递归事件。

Recurring事件是一系列单个事件(实例)。您可以通过以下链接阅读有关实例的信息:https://developers.google.com/google-apps/calendar/v3/reference/events/instances

如果你想删除重复发生的事件(所有实例),你需要使用以下代码:

$rec_event = $this->calendar->events->get('primary', $google_event_id);
if ($rec_event && $rec_event->getStatus() != "cancelled") { 
    $this->calendar->events->delete('primary', $google_event_id); // $google_event_id is id of main event with recurrences
}

如果你想从某个日期删除以下所有实例,你需要获得该日期之后的所有实例,然后在周期中删除它们:

    $opt_params = array('timeMin' => $isoDate); // date of DATE_RFC3339 format,  "Y-m-d'TH:i:sP"
    $instances = $this->calendar->events->instances($this->calendar_id, $google_event_id, $opt_params);
    if ($instances && count($instances->getItems())) {
      foreach ($instances->getItems() as $instance) {
        $this->calendar->events->delete('primary', $instance->getId());
      }
    }

如果你想添加异常(只删除一个重复事件的实例),需要使用几乎相同的代码,但使用另一个过滤器:

    $opt_params = array('originalStart' => $isoDate); // exception date
    $instances = $this->calendar->events->instances($this->calendar_id, $google_event_id, $opt_params);
    if ($instances && count($instances->getItems())) {
      foreach ($instances->getItems() as $instance) {
        $this->calendar->events->delete('primary', $instance->getId());
      }
    }