กิจกรรมที่เกิดซ้ํา

เอกสารนี้จะอธิบายวิธีทำงานกับกิจกรรมที่เกิดซ้ำและอินสแตนซ์ของกิจกรรม

สร้างกิจกรรมที่เกิดซ้ำ

การสร้างกิจกรรมที่เกิดซ้ำจะคล้ายกับการสร้างกิจกรรมปกติ (รายการเดียว) ที่มีการตั้งค่าช่อง recurrence ของทรัพยากร event

โปรโตคอล

POST /calendar/v3/calendars/primary/events
...

{
  "summary": "Appointment",
  "location": "Somewhere",
  "start": {
    "dateTime": "2011-06-03T10:00:00.000-07:00",
    "timeZone": "America/Los_Angeles"
  },
  "end": {
    "dateTime": "2011-06-03T10:25:00.000-07:00",
    "timeZone": "America/Los_Angeles"
  },
  "recurrence": [
    "RRULE:FREQ=WEEKLY;UNTIL=20110701T170000Z",
  ],
  "attendees": [
    {
      "email": "attendeeEmail",
      # Other attendee's data...
    },
    # ...
  ],
}

Java

Event event = new Event();

event.setSummary("Appointment");
event.setLocation("Somewhere");

ArrayList<EventAttendee> attendees = new ArrayList<EventAttendee>();
attendees.add(new EventAttendee().setEmail("attendeeEmail"));
// ...
event.setAttendees(attendees);

DateTime start = DateTime.parseRfc3339("2011-06-03T10:00:00.000-07:00");
DateTime end = DateTime.parseRfc3339("2011-06-03T10:25:00.000-07:00");
event.setStart(new EventDateTime().setDateTime(start).setTimeZone("America/Los_Angeles"));
event.setEnd(new EventDateTime().setDateTime(end).setTimeZone("America/Los_Angeles"));
event.setRecurrence(Arrays.asList("RRULE:FREQ=WEEKLY;UNTIL=20110701T170000Z"));

Event recurringEvent = service.events().insert("primary", event).execute();

System.out.println(createdEvent.getId());

.NET

Event event = new Event()
    {
      Summary = "Appointment",
      Location = "Somewhere",
      Start = new EventDateTime() {
          DateTime = new DateTime("2011-06-03T10:00:00.000:-07:00")
          TimeZone = "America/Los_Angeles"
      },
      End = new EventDateTime() {
          DateTime = new DateTime("2011-06-03T10:25:00.000:-07:00")
          TimeZone = "America/Los_Angeles"
      },
      Recurrence = new String[] {
          "RRULE:FREQ=WEEKLY;UNTIL=20110701T170000Z"
      },
      Attendees = new List<EventAttendee>()
          {
            new EventAttendee() { Email: "attendeeEmail" },
            // ...
          }
    };

Event recurringEvent = service.Events.Insert(event, "primary").Fetch();

Console.WriteLine(recurringEvent.Id);

Python

event = {
  'summary': 'Appointment',
  'location': 'Somewhere',
  'start': {
    'dateTime': '2011-06-03T10:00:00.000-07:00',
    'timeZone': 'America/Los_Angeles'
  },
  'end': {
    'dateTime': '2011-06-03T10:25:00.000-07:00',
    'timeZone': 'America/Los_Angeles'
  },
  'recurrence': [
    'RRULE:FREQ=WEEKLY;UNTIL=20110701T170000Z',
  ],
  'attendees': [
    {
      'email': 'attendeeEmail',
      # Other attendee's data...
    },
    # ...
  ],
}

recurring_event = service.events().insert(calendarId='primary', body=event).execute()

print recurring_event['id']

PHP

$event = new Google_Service_Calendar_Event();
$event->setSummary('Appointment');
$event->setLocation('Somewhere');
$start = new Google_Service_Calendar_EventDateTime();
$start->setDateTime('2011-06-03T10:00:00.000-07:00');
$start->setTimeZone('America/Los_Angeles');
$event->setStart($start);
$end = new Google_Service_Calendar_EventDateTime();
$end->setDateTime('2011-06-03T10:25:00.000-07:00');
$end->setTimeZone('America/Los_Angeles');
$event->setEnd($end);
$event->setRecurrence(array('RRULE:FREQ=WEEKLY;UNTIL=20110701T170000Z'));
$attendee1 = new Google_Service_Calendar_EventAttendee();
$attendee1->setEmail('attendeeEmail');
// ...
$attendees = array($attendee1,
                   // ...
                   );
$event->attendees = $attendees;
$recurringEvent = $service->events->insert('primary', $event);

echo $recurringEvent->getId();

Ruby

event = Google::Apis::CalendarV3::Event.new(
  summary: 'Appointment',
  location: 'Somewhere',
  start: {
    date_time: '2011-06-03T10:00:00.000-07:00',
    time_zone:  'America/Los_Angeles'
  },
  end: {
    date_time: '2011-06-03T10:25:00.000-07:00',
    time_zone: 'America/Los_Angeles'
  },
  recurrence: ['RRULE:FREQ=WEEKLY;UNTIL=20110701T170000Z']
  attendees: [
    {
      email: 'attendeeEmail'
    },
    #...
  ]
)
response = client.insert_event('primary', event)
print response.id

เข้าถึงอินสแตนซ์

หากต้องการดูอินสแตนซ์ทั้งหมดของกิจกรรมที่เกิดซ้ำ คุณจะใช้คำขอ events.instances() ได้

โดยค่าเริ่มต้น คำขอ events.list() จะแสดงผลเฉพาะกิจกรรมรายการเดียว กิจกรรมที่เกิดซ้ำ และข้อยกเว้นเท่านั้น โดยจะแสดงอินสแตนซ์ที่ไม่ใช่ข้อยกเว้น หากตั้งค่าพารามิเตอร์ singleEvents เป็น true อินสแตนซ์ทั้งหมดจะปรากฏในผลลัพธ์ แต่เหตุการณ์ที่เกิดซ้ำที่เกี่ยวข้องจะไม่แสดง เมื่อผู้ใช้ที่มีสิทธิ์ว่าง/ไม่ว่างค้นหา events.list() ระบบจะทำงานเหมือนกับ singleEvent คือ true ดูข้อมูลเพิ่มเติมเกี่ยวกับกฎรายการควบคุมการเข้าถึงได้ที่ Acl

อินสแตนซ์แต่ละรายการจะคล้ายกับเหตุการณ์เดียว อินสแตนซ์ไม่ได้ตั้งค่าช่อง recurrence ไว้ ซึ่งต่างจากกิจกรรมที่เกิดซ้ำระดับบนสุด

ช่องเหตุการณ์ต่อไปนี้ใช้สำหรับอินสแตนซ์เท่านั้น

  • recurringEventId — รหัสของกิจกรรมที่เกิดซ้ำระดับบนสุดที่เป็นเจ้าของอินสแตนซ์นี้
  • originalStartTime — เวลาที่อินสแตนซ์นี้จะเริ่มต้นตามข้อมูลการเกิดซ้ำในกิจกรรมที่เกิดซ้ำระดับบนสุด ซึ่งอาจแตกต่างจากเวลาจริงของ start หากมีกำหนดเวลาอินสแตนซ์ใหม่ โดยอินสแตนซ์ดังกล่าวจะระบุอินสแตนซ์ภายในชุดกิจกรรมที่เกิดซ้ำ แม้ว่าอินสแตนซ์จะถูกย้ายแล้วก็ตาม

แก้ไขหรือลบอินสแตนซ์

หากต้องการแก้ไขอินสแตนซ์เดียว (สร้างข้อยกเว้น) แอปพลิเคชันไคลเอ็นต์จะต้องเรียกข้อมูลอินสแตนซ์ก่อน จากนั้นอัปเดตอินสแตนซ์โดยส่งคำขอ PUT ที่ได้รับอนุญาตไปยัง URL แก้ไขอินสแตนซ์พร้อมข้อมูลอัปเดตในเนื้อหา URL จะมีรูปแบบดังนี้

https://www.googleapis.com/calendar/v3/calendars/calendarId/events/instanceId

ใช้ค่าที่เหมาะสมแทน calendarId และ instanceId

หมายเหตุ: ค่า calendarId พิเศษ primary สามารถใช้เพื่ออ้างถึงปฏิทินหลักของผู้ใช้ที่ตรวจสอบสิทธิ์แล้ว

เมื่อทำสำเร็จ เซิร์ฟเวอร์จะตอบสนองด้วยรหัสสถานะ HTTP 200 OK ด้วยอินสแตนซ์ที่อัปเดต ตัวอย่างต่อไปนี้แสดงวิธียกเลิกอินสแตนซ์ของกิจกรรมที่เกิดซ้ำ

โปรโตคอล

PUT /calendar/v3/calendars/primary/events/instanceId
...

{
  "kind": "calendar#event",
  "id": "instanceId",
  "etag": "instanceEtag",
  "status": "cancelled",
  "htmlLink": "https://www.google.com/calendar/event?eid=instanceEid",
  "created": "2011-05-23T22:27:01.000Z",
  "updated": "2011-05-23T22:27:01.000Z",
  "summary": "Recurring event",
  "location": "Somewhere",
  "creator": {
    "email": "userEmail"
  },
  "recurringEventId": "recurringEventId",
  "originalStartTime": "2011-06-03T10:00:00.000-07:00",
  "organizer": {
    "email": "userEmail",
    "displayName": "userDisplayName"
  },
  "start": {
    "dateTime": "2011-06-03T10:00:00.000-07:00",
    "timeZone": "America/Los_Angeles"
  },
  "end": {
    "dateTime": "2011-06-03T10:25:00.000-07:00",
    "timeZone": "America/Los_Angeles"
  },
  "iCalUID": "eventUID",
  "sequence": 0,
  "attendees": [
    {
      "email": "attendeeEmail",
      "displayName": "attendeeDisplayName",
      "responseStatus": "needsAction"
    },
    # ...
    {
      "email": "userEmail",
      "displayName": "userDisplayName",
      "responseStatus": "accepted",
      "organizer": true,
      "self": true
    }
  ],
  "guestsCanInviteOthers": false,
  "guestsCanSeeOtherGuests": false,
  "reminders": {
    "useDefault": true
  }
}

Java

// First retrieve the instances from the API.
Events instances = service.events().instances("primary", "recurringEventId").execute();

// Select the instance to cancel.
Event instance = instances.getItems().get(0);
instance.setStatus("cancelled");

Event updatedInstance = service.events().update("primary", instance.getId(), instance).execute();

// Print the updated date.
System.out.println(updatedInstance.getUpdated());

.NET

// First retrieve the instances from the API.
Events instances = service.Events.Instances("primary", "recurringEventId").Fetch();

// Select the instance to cancel.
Event instance = instances.Items[0];
instance.Status = "cancelled";

Event updatedInstance = service.Events.Update(instance, "primary", instance.Id).Fetch();

// Print the updated date.
Console.WriteLine(updatedInstance.Updated);

Python

# First retrieve the instances from the API.
instances = service.events().instances(calendarId='primary', eventId='recurringEventId').execute()

# Select the instance to cancel.
instance = instances['items'][0]
instance['status'] = 'cancelled'

updated_instance = service.events().update(calendarId='primary', eventId=instance['id'], body=instance).execute()

# Print the updated date.
print updated_instance['updated']

PHP

$events = $service->events->instances("primary", "eventId");

// Select the instance to cancel.
$instance = $events->getItems()[0];
$instance->setStatus('cancelled');

$updatedInstance = $service->events->update('primary', $instance->getId(), $instance);

// Print the updated date.
echo $updatedInstance->getUpdated();

Ruby

# First retrieve the instances from the API.
instances = client.list_event_instances('primary', 'recurringEventId')

# Select the instance to cancel.
instance = instances.items[0]
instance.status = 'cancelled'

response = client.update_event('primary', instance.id, instance)
print response.updated

แก้ไขอินสแตนซ์ต่อไปนี้ทั้งหมด

หากต้องการเปลี่ยนอินสแตนซ์ทั้งหมดของกิจกรรมที่เกิดซ้ำในหรือหลังจากอินสแตนซ์ (เป้าหมาย) ที่ระบุ คุณต้องส่งคำขอ API แยกกัน 2 รายการ คำขอเหล่านี้แบ่งกิจกรรมที่เกิดซ้ำเดิมออกเป็น 2 กิจกรรม กล่าวคือ กิจกรรมเดิมจะคงอินสแตนซ์ไว้โดยไม่มีการเปลี่ยนแปลง และเหตุการณ์ที่เกิดซ้ำใหม่มีอินสแตนซ์ที่ใช้การเปลี่ยนแปลง ดังนี้
  1. เรียกใช้ events.update() เพื่อตัดกิจกรรมที่เกิดซ้ำเดิมของอินสแตนซ์ที่จะอัปเดต โดยการตั้งค่าคอมโพเนนต์ UNTIL ของ RRULE ให้ชี้ไปที่เวลาเริ่มต้นของอินสแตนซ์เป้าหมายแรก หรือคุณอาจตั้งค่าคอมโพเนนต์ COUNT แทน UNTIL
  2. เรียกใช้ events.insert() เพื่อสร้างกิจกรรมที่เกิดซ้ำใหม่ด้วยข้อมูลเดิมทั้งหมด ยกเว้นการเปลี่ยนแปลงที่คุณพยายามจะทำ กิจกรรมที่เกิดซ้ำใหม่ต้องมีเวลาเริ่มต้นของอินสแตนซ์เป้าหมาย

ตัวอย่างนี้แสดงวิธีเปลี่ยนตำแหน่งเป็น "ที่อื่น" โดยเริ่มจากอินสแตนซ์ที่ 3 ของกิจกรรมที่เกิดซ้ำจากตัวอย่างก่อนหน้านี้

โปรโตคอล

# Updating the original recurring event to trim the instance list:

PUT /calendar/v3/calendars/primary/events/recurringEventId
...

{
  "summary": "Appointment",
  "location": "Somewhere",
  "start": {
    "dateTime": "2011-06-03T10:00:00.000-07:00",
    "timeZone": "America/Los_Angeles"
  },
  "end": {
    "dateTime": "2011-06-03T10:25:00.000-07:00",
    "timeZone": "America/Los_Angeles"
  },
  "recurrence": [
    "RRULE:FREQ=WEEKLY;UNTIL=20110617T065959Z",
  ],
  "attendees": [
    {
      "email": "attendeeEmail",
      # Other attendee's data...
    },
    # ...
  ],
}


# Creating a new recurring event with the change applied:

POST /calendar/v3/calendars/primary/events
...

{
  "summary": "Appointment",
  "location": "Somewhere else",
  "start": {
    "dateTime": "2011-06-17T10:00:00.000-07:00",
    "timeZone": "America/Los_Angeles"
  },
  "end": {
    "dateTime": "2011-06-17T10:25:00.000-07:00",
    "timeZone": "America/Los_Angeles"
  },
  "recurrence": [
    "RRULE:FREQ=WEEKLY;UNTIL=20110617T065959Z",
  ],
  "attendees": [
    {
      "email": "attendeeEmail",
      # Other attendee's data...
    },
    # ...
  ],
}