Google日历中的重复活动如何运作

时间:2013-12-13 07:29:15

标签: php google-api google-calendar-api recurring-events

我尝试了两种方式将应用与谷歌日历同步。我没有找到合理的描述重复事件是如何工作的。它们在主要事件中是否物理复制(拥有自己的ID)? Google日历API(listEvents)仅返回带有重复的主事件(字符串)。如果重复没有自己的ID,我该如何删除它们?当我从API(listEvents)的数据中删除系列(在Google日历中)中的一个重复事件时,没有提及丢失的重复事件。

1 个答案:

答案 0 :(得分:1)

重复事件是一系列单个事件(实例)。您可以通过以下链接阅读有关实例的信息: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());
      }
    }
相关问题