W tym dokumencie opisujemy, jak pracować ze zdarzeniami cyklicznymi i ich instancjami.
Tworzenie wydarzeń cyklicznych
Tworzenie wydarzeń cyklicznych przypomina tworzenie zwykłego (pojedynczego) wydarzenia za pomocą zestawu pól recurrence
zasobu event
.
Protokół
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
Dostęp do instancji
Aby zobaczyć wszystkie instancje danego zdarzenia cyklicznego, użyj żądania events.instances().
Żądanie events.list()
domyślnie zwraca tylko pojedyncze zdarzenia, wydarzenia cykliczne i wyjątki. Instancje, które nie są wyjątkami, nie są zwracane.
Jeśli parametr singleEvents
ma wartość true
, w wynikach pojawią się wszystkie poszczególne wystąpienia, ale nieistniejące zdarzenia cykliczne. Gdy użytkownik z uprawnieniami Wolny/Zajęty wysyła zapytanie events.list()
, działa tak, jakby singleEvent
miał wartość true
. Więcej informacji o regułach list kontroli dostępu znajdziesz w artykule Acl.
Poszczególne instancje są podobne do pojedynczych zdarzeń. W przeciwieństwie do nadrzędnych wydarzeń cyklicznych instancje nie mają ustawionego pola recurrence
.
Te pola zdarzeń są specyficzne dla instancji:
recurringEventId
– identyfikator nadrzędnego wydarzenia cyklicznego, do którego należy ta instancja;originalStartTime
– czas rozpoczęcia tej instancji zgodny z danymi powtarzania w nadrzędnym zdarzeniu cyklicznego. Ta wartość może się różnić od rzeczywistego czasustart
w przypadku zmiany harmonogramu instancji. Określa on jednoznacznie instancję w serii wydarzeń cyklicznych, nawet jeśli została przeniesiona.
Modyfikowanie i usuwanie instancji
Aby zmodyfikować pojedynczą instancję (tworząc wyjątek), aplikacje klienckie muszą najpierw pobrać instancję, a potem ją zaktualizować, wysyłając autoryzowane żądanie PUT na adres URL edycji instancji ze zaktualizowanymi danymi w treści. Adres URL ma postać:
https://www.googleapis.com/calendar/v3/calendars/calendarId/events/instanceId
Zamiast calendarId i instanceId użyj odpowiednich wartości.
Po pomyślnym zakończeniu serwer w odpowiedzi wysyła kod stanu HTTP 200 OK ze zaktualizowaną instancją. Z przykładu poniżej dowiesz się, jak anulować wystąpienie wydarzenia cyklicznego.
Protokół
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
Zmodyfikuj wszystkie następujące instancje
Aby zmienić wszystkie wystąpienia zdarzenia cyklicznego w danej instancji (docelowej) lub po niej, musisz wysłać 2 osobne żądania do interfejsu API. Żądania te dzielą pierwotne zdarzenie cykliczne na 2 : pierwotne, które zachowuje wystąpienia bez zmiany, oraz nowe wydarzenie cykliczne, w którym występuje zmiana:- Wywołaj
events.update()
, aby przyciąć pierwotne zdarzenie cykliczne w instancjach do zaktualizowania. Aby to zrobić, ustaw komponentUNTIL
obiektuRRULE
tak, aby wskazywał czas rozpoczęcia pierwszej instancji docelowej. Możesz też ustawić komponentCOUNT
zamiastUNTIL
. - Wywołaj
events.insert()
, aby utworzyć nowe wydarzenie cykliczne z tymi samymi danymi co pierwotne, z wyjątkiem zmiany, którą chcesz wprowadzić. Nowe zdarzenie cykliczne musi mieć godzinę rozpoczęcia instancji docelowej.
Ten przykład pokazuje, jak zmienić lokalizację na „Gdzieś w innym miejscu”, zaczynając od trzeciego wystąpienia zdarzenia cyklicznego z poprzednich przykładów.
Protokół
# 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... }, # ... ], }