สร้างเหตุการณ์

ลองนึกภาพแอปที่ช่วยให้ผู้ใช้พบเส้นทางเดินป่าที่ดีที่สุด โดยเพิ่ม เป็นกิจกรรมในปฏิทิน ทำให้ผู้ใช้ได้รับความช่วยเหลือมากมายในการ ได้รับการจัดระเบียบโดยอัตโนมัติ และ Google ปฏิทินจะช่วยแชร์แผน จะเตือนบุตรหลานเกี่ยวกับเรื่องนี้ ให้พวกเขาได้เตรียมตัวโดยปราศจากความเครียด และขอขอบคุณ การผสานรวมผลิตภัณฑ์ Google อย่างราบรื่น Google Now จะส่งข้อความให้พวกเขาเกี่ยวกับเวลา ออกและ Google Maps จะพาพวกเขาไปยังจุดนัดหมายตรงเวลา

บทความนี้จะอธิบายวิธีสร้างกิจกรรมในปฏิทินและเพิ่มกิจกรรมในปฏิทินของผู้ใช้ ปฏิทิน

เพิ่มกิจกรรม

หากต้องการสร้างเหตุการณ์ ให้เรียกเมธอด events.insert() ให้เมธอดที่ พารามิเตอร์เหล่านี้น้อยที่สุด

  • calendarId คือตัวระบุปฏิทิน ซึ่งอาจเป็นอีเมลก็ได้ ในปฏิทินที่จะสร้างกิจกรรมหรือคีย์เวิร์ดพิเศษ 'primary' ซึ่งจะใช้ปฏิทินหลักของผู้ใช้ที่เข้าสู่ระบบ ถ้า คุณไม่ทราบที่อยู่อีเมลของปฏิทินที่คุณต้องการใช้ สามารถตรวจสอบได้ในการตั้งค่าปฏิทินของ Google ปฏิทินบนเว็บ UI (ในส่วน "ที่อยู่ของปฏิทิน") หรือคุณสามารถค้นหา ในผลลัพธ์ของ calendarList.list() โทร
  • event คือกิจกรรมที่จะสร้างโดยมีรายละเอียดที่จำเป็นทั้งหมด เช่น เริ่มต้น และสิ้นสุด ช่องที่ต้องกรอกเพียง 2 ช่องคือ start และ end โปรดดู ข้อมูลอ้างอิง event สำหรับเหตุการณ์ทั้งชุด ด้วย

เพื่อสร้างเหตุการณ์ให้สำเร็จ คุณต้องทำดังนี้

  • กำหนดขอบเขต OAuth เป็น https://www.googleapis.com/auth/calendar เพื่อให้ คุณมีสิทธิ์แก้ไขปฏิทินของผู้ใช้
  • ตรวจสอบว่าผู้ใช้ที่ตรวจสอบสิทธิ์แล้วมีสิทธิ์เขียนปฏิทินด้วย calendarId ที่คุณระบุ (เช่น การโทร calendarList.get() สำหรับ calendarId และตรวจสอบaccessRole)

เพิ่มข้อมูลเมตาของเหตุการณ์

คุณสามารถเลือกเพิ่มข้อมูลเมตาของกิจกรรมเมื่อสร้างกิจกรรมในปฏิทินได้ หากคุณ เลือกที่จะไม่เพิ่มข้อมูลเมตาระหว่างการสร้าง คุณสามารถอัปเดตหลายฟิลด์ได้โดยใช้ events.update(); แต่บางช่อง เช่น รหัสเหตุการณ์ จะสามารถตั้งค่าได้ในช่วง events.insert() การดำเนินการ

ตำแหน่ง

การเพิ่มที่อยู่ลงในช่องสถานที่ตั้งจะทำให้สามารถใช้ฟีเจอร์ต่างๆ เช่น

"ถึงเวลาออกเดินทาง" หรือแสดงแผนที่พร้อมเส้นทาง

รหัสเหตุการณ์

เมื่อสร้างกิจกรรม คุณจะเลือกสร้างรหัสเหตุการณ์ของคุณเองได้

ที่สอดคล้องกับข้อกำหนดด้านรูปแบบของเรา วิธีนี้จะช่วยให้คุณเก็บเอนทิตีไว้ ในฐานข้อมูลของเครื่องให้ตรงกับกิจกรรมใน Google ปฏิทิน และ ป้องกันการสร้างกิจกรรมที่ซ้ำกันหากการดำเนินการล้มเหลวในบางเวลาหลังจากนั้น ดำเนินการสำเร็จแล้วในแบ็กเอนด์ของปฏิทิน หากไม่ใช่ ระบุรหัสเหตุการณ์แล้ว เซิร์ฟเวอร์จะสร้างรหัสให้คุณ ดูรหัสเหตุการณ์ ข้อมูลอ้างอิงสำหรับข้อมูลเพิ่มเติม

ผู้เข้าร่วม

กิจกรรมที่คุณสร้างจะปรากฏใน Google ปฏิทินหลักทั้งหมดของ

ผู้เข้าร่วมที่คุณรวมไว้ด้วยรหัสกิจกรรมเดียวกัน หากตั้งค่าไว้ sendNotifications ถึง true ในคำขอแทรกของคุณ ผู้เข้าร่วมจะ จะได้รับการแจ้งเตือนทางอีเมลเกี่ยวกับกิจกรรมของคุณ ดูเหตุการณ์ที่มี คำแนะนำสำหรับผู้เข้าร่วมหลายคน ข้อมูลเพิ่มเติม

ตัวอย่างต่อไปนี้แสดงการสร้างเหตุการณ์และการตั้งค่าข้อมูลเมตา

Go

// Refer to the Go quickstart on how to setup the environment:
// https://developers.google.com/calendar/quickstart/go
// Change the scope to calendar.CalendarScope and delete any stored credentials.

event := &calendar.Event{
  Summary: "Google I/O 2015",
  Location: "800 Howard St., San Francisco, CA 94103",
  Description: "A chance to hear more about Google's developer products.",
  Start: &calendar.EventDateTime{
    DateTime: "2015-05-28T09:00:00-07:00",
    TimeZone: "America/Los_Angeles",
  },
  End: &calendar.EventDateTime{
    DateTime: "2015-05-28T17:00:00-07:00",
    TimeZone: "America/Los_Angeles",
  },
  Recurrence: []string{"RRULE:FREQ=DAILY;COUNT=2"},
  Attendees: []*calendar.EventAttendee{
    &calendar.EventAttendee{Email:"lpage@example.com"},
    &calendar.EventAttendee{Email:"sbrin@example.com"},
  },
}

calendarId := "primary"
event, err = srv.Events.Insert(calendarId, event).Do()
if err != nil {
  log.Fatalf("Unable to create event. %v\n", err)
}
fmt.Printf("Event created: %s\n", event.HtmlLink)

Java

// Refer to the Java quickstart on how to setup the environment:
// https://developers.google.com/calendar/quickstart/java
// Change the scope to CalendarScopes.CALENDAR and delete any stored
// credentials.

Event event = new Event()
    .setSummary("Google I/O 2015")
    .setLocation("800 Howard St., San Francisco, CA 94103")
    .setDescription("A chance to hear more about Google's developer products.");

DateTime startDateTime = new DateTime("2015-05-28T09:00:00-07:00");
EventDateTime start = new EventDateTime()
    .setDateTime(startDateTime)
    .setTimeZone("America/Los_Angeles");
event.setStart(start);

DateTime endDateTime = new DateTime("2015-05-28T17:00:00-07:00");
EventDateTime end = new EventDateTime()
    .setDateTime(endDateTime)
    .setTimeZone("America/Los_Angeles");
event.setEnd(end);

String[] recurrence = new String[] {"RRULE:FREQ=DAILY;COUNT=2"};
event.setRecurrence(Arrays.asList(recurrence));

EventAttendee[] attendees = new EventAttendee[] {
    new EventAttendee().setEmail("lpage@example.com"),
    new EventAttendee().setEmail("sbrin@example.com"),
};
event.setAttendees(Arrays.asList(attendees));

EventReminder[] reminderOverrides = new EventReminder[] {
    new EventReminder().setMethod("email").setMinutes(24 * 60),
    new EventReminder().setMethod("popup").setMinutes(10),
};
Event.Reminders reminders = new Event.Reminders()
    .setUseDefault(false)
    .setOverrides(Arrays.asList(reminderOverrides));
event.setReminders(reminders);

String calendarId = "primary";
event = service.events().insert(calendarId, event).execute();
System.out.printf("Event created: %s\n", event.getHtmlLink());

JavaScript

// Refer to the JavaScript quickstart on how to setup the environment:
// https://developers.google.com/calendar/quickstart/js
// Change the scope to 'https://www.googleapis.com/auth/calendar' and delete any
// stored credentials.

const event = {
  'summary': 'Google I/O 2015',
  'location': '800 Howard St., San Francisco, CA 94103',
  'description': 'A chance to hear more about Google\'s developer products.',
  'start': {
    'dateTime': '2015-05-28T09:00:00-07:00',
    'timeZone': 'America/Los_Angeles'
  },
  'end': {
    'dateTime': '2015-05-28T17:00:00-07:00',
    'timeZone': 'America/Los_Angeles'
  },
  'recurrence': [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  'attendees': [
    {'email': 'lpage@example.com'},
    {'email': 'sbrin@example.com'}
  ],
  'reminders': {
    'useDefault': false,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10}
    ]
  }
};

const request = gapi.client.calendar.events.insert({
  'calendarId': 'primary',
  'resource': event
});

request.execute(function(event) {
  appendPre('Event created: ' + event.htmlLink);
});

Node.js

// Refer to the Node.js quickstart on how to setup the environment:
// https://developers.google.com/calendar/quickstart/node
// Change the scope to 'https://www.googleapis.com/auth/calendar' and delete any
// stored credentials.

const event = {
  'summary': 'Google I/O 2015',
  'location': '800 Howard St., San Francisco, CA 94103',
  'description': 'A chance to hear more about Google\'s developer products.',
  'start': {
    'dateTime': '2015-05-28T09:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'end': {
    'dateTime': '2015-05-28T17:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'recurrence': [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  'attendees': [
    {'email': 'lpage@example.com'},
    {'email': 'sbrin@example.com'},
  ],
  'reminders': {
    'useDefault': false,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10},
    ],
  },
};

calendar.events.insert({
  auth: auth,
  calendarId: 'primary',
  resource: event,
}, function(err, event) {
  if (err) {
    console.log('There was an error contacting the Calendar service: ' + err);
    return;
  }
  console.log('Event created: %s', event.htmlLink);
});

PHP

$event = new Google_Service_Calendar_Event(array(
  'summary' => 'Google I/O 2015',
  'location' => '800 Howard St., San Francisco, CA 94103',
  'description' => 'A chance to hear more about Google\'s developer products.',
  'start' => array(
    'dateTime' => '2015-05-28T09:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'end' => array(
    'dateTime' => '2015-05-28T17:00:00-07:00',
    'timeZone' => 'America/Los_Angeles',
  ),
  'recurrence' => array(
    'RRULE:FREQ=DAILY;COUNT=2'
  ),
  'attendees' => array(
    array('email' => 'lpage@example.com'),
    array('email' => 'sbrin@example.com'),
  ),
  'reminders' => array(
    'useDefault' => FALSE,
    'overrides' => array(
      array('method' => 'email', 'minutes' => 24 * 60),
      array('method' => 'popup', 'minutes' => 10),
    ),
  ),
));

$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %s\n', $event->htmlLink);

Python

# Refer to the Python quickstart on how to setup the environment:
# https://developers.google.com/calendar/quickstart/python
# Change the scope to 'https://www.googleapis.com/auth/calendar' and delete any
# stored credentials.

event = {
  'summary': 'Google I/O 2015',
  'location': '800 Howard St., San Francisco, CA 94103',
  'description': 'A chance to hear more about Google\'s developer products.',
  'start': {
    'dateTime': '2015-05-28T09:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'end': {
    'dateTime': '2015-05-28T17:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'recurrence': [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  'attendees': [
    {'email': 'lpage@example.com'},
    {'email': 'sbrin@example.com'},
  ],
  'reminders': {
    'useDefault': False,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10},
    ],
  },
}

event = service.events().insert(calendarId='primary', body=event).execute()
print 'Event created: %s' % (event.get('htmlLink'))

Ruby

event = Google::Apis::CalendarV3::Event.new(
  summary: 'Google I/O 2015',
  location: '800 Howard St., San Francisco, CA 94103',
  description: 'A chance to hear more about Google\'s developer products.',
  start: Google::Apis::CalendarV3::EventDateTime.new(
    date_time: '2015-05-28T09:00:00-07:00',
    time_zone: 'America/Los_Angeles'
  ),
  end: Google::Apis::CalendarV3::EventDateTime.new(
    date_time: '2015-05-28T17:00:00-07:00',
    time_zone: 'America/Los_Angeles'
  ),
  recurrence: [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  attendees: [
    Google::Apis::CalendarV3::EventAttendee.new(
      email: 'lpage@example.com'
    ),
    Google::Apis::CalendarV3::EventAttendee.new(
      email: 'sbrin@example.com'
    )
  ],
  reminders: Google::Apis::CalendarV3::Event::Reminders.new(
    use_default: false,
    overrides: [
      Google::Apis::CalendarV3::EventReminder.new(
        reminder_method: 'email',
        minutes: 24 * 60
      ),
      Google::Apis::CalendarV3::EventReminder.new(
        reminder_method: 'popup',
        minutes: 10
      )
    ]
  )
)

result = client.insert_event('primary', event)
puts "Event created: #{result.html_link}"

เพิ่มไฟล์แนบของไดรฟ์ในกิจกรรม

คุณแนบ Google ไดรฟ์ได้ เช่น บันทึกการประชุมในเอกสาร ชีต งานนำเสนอในสไลด์ หรืออื่นๆ ไฟล์ Google ไดรฟ์ที่เกี่ยวข้องกับกิจกรรมในปฏิทินของคุณ คุณสามารถเพิ่ม เมื่อคุณสร้างกิจกรรมด้วย events.insert() ขึ้นไปซึ่งเป็นส่วนหนึ่งของ อัปเดต เช่น ด้วย events.patch()

การแนบไฟล์ Google ไดรฟ์กับกิจกรรมประกอบด้วย 2 ส่วน ดังนี้

  1. รับ URL ของไฟล์ alternateLink, title และ mimeType จาก ทรัพยากรของ Drive API Files โดยปกติ โดยใช้เมธอด files.get()
  2. สร้างหรืออัปเดตกิจกรรมด้วยการตั้งค่าช่อง attachments ในคำขอ ตั้งค่าเนื้อหา และพารามิเตอร์ supportsAttachments เป็น true

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีอัปเดตเหตุการณ์ที่มีอยู่เพื่อเพิ่ม ไฟล์แนบ:

Java

public static void addAttachment(Calendar calendarService, Drive driveService, String calendarId,
    String eventId, String fileId) throws IOException {
  File file = driveService.files().get(fileId).execute();
  Event event = calendarService.events().get(calendarId, eventId).execute();

  List<EventAttachment> attachments = event.getAttachments();
  if (attachments == null) {
    attachments = new ArrayList<EventAttachment>();
  }
  attachments.add(new EventAttachment()
      .setFileUrl(file.getAlternateLink())
      .setMimeType(file.getMimeType())
      .setTitle(file.getTitle()));

  Event changes = new Event()
      .setAttachments(attachments);
  calendarService.events().patch(calendarId, eventId, changes)
      .setSupportsAttachments(true)
      .execute();
}

PHP

function addAttachment($calendarService, $driveService, $calendarId, $eventId, $fileId) {
  $file = $driveService->files->get($fileId);
  $event = $calendarService->events->get($calendarId, $eventId);
  $attachments = $event->attachments;

  $attachments[] = array(
    'fileUrl' => $file->alternateLink,
    'mimeType' => $file->mimeType,
    'title' => $file->title
  );
  $changes = new Google_Service_Calendar_Event(array(
    'attachments' => $attachments
  ));

  $calendarService->events->patch($calendarId, $eventId, $changes, array(
    'supportsAttachments' => TRUE
  ));
}

Python

def add_attachment(calendarService, driveService, calendarId, eventId, fileId):
    file = driveService.files().get(fileId=fileId).execute()
    event = calendarService.events().get(calendarId=calendarId,
                                         eventId=eventId).execute()

    attachments = event.get('attachments', [])
    attachments.append({
        'fileUrl': file['alternateLink'],
        'mimeType': file['mimeType'],
        'title': file['title']
    })

    changes = {
        'attachments': attachments
    }
    calendarService.events().patch(calendarId=calendarId, eventId=eventId,
                                   body=changes,
                                   supportsAttachments=True).execute()

เพิ่มการประชุมทางวิดีโอและโทรศัพท์ลงในกิจกรรม

คุณสามารถเชื่อมโยงกิจกรรมกับ Hangouts และ การประชุม Google Meet ไปยัง อนุญาตให้ผู้ใช้ประชุมจากระยะไกลผ่านการโทรหรือวิดีโอคอลได้

ช่อง conferenceData สามารถ ใช้ในการอ่าน คัดลอก และล้างรายละเอียดการประชุมที่มีอยู่ หรืออาจจะเป็น ที่ใช้ในการขอให้สร้างการประชุมใหม่ เพื่อให้สามารถสร้างและ การแก้ไขรายละเอียดการประชุม ตั้งค่าคำขอ conferenceDataVersion 1

มี conferenceData ที่รองรับอยู่ในปัจจุบัน 3 ประเภท ตามที่แสดงโดย conferenceData.conferenceSolution.key.type:

  1. Hangouts สำหรับผู้ใช้ทั่วไป (eventHangout)
  2. Hangouts แบบคลาสสิกสำหรับ Google Workspace ผู้ใช้ (เลิกใช้งานแล้ว eventNamedHangout)
  3. Google Meet (hangoutsMeet)

คุณสามารถเรียนรู้ได้ว่าการประชุมประเภทใดมีการสนับสนุนสำหรับปฏิทินใดๆ ของ โดยดูที่ conferenceProperties.allowedConferenceSolutionTypes ใน calendars และ calendarList คอลเล็กชัน นอกจากนี้คุณยัง เรียนรู้ว่าผู้ใช้ต้องการสร้าง Hangouts สำหรับ สร้างกิจกรรมโดยตรวจสอบการตั้งค่า autoAddHangouts ใน settings คอลเล็กชัน

นอกเหนือจาก type แล้ว conferenceSolution ยังมี name และ iconUri ช่องที่ใช้แสดงโซลูชันการประชุมได้ ตามที่แสดง ด้านล่าง

JavaScript

const solution = event.conferenceData.conferenceSolution;

const content = document.getElementById("content");
const text = document.createTextNode("Join " + solution.name);
const icon = document.createElement("img");
icon.src = solution.iconUri;

content.appendChild(icon);
content.appendChild(text);

คุณสามารถสร้างการประชุมใหม่สำหรับกิจกรรมได้โดยการระบุ createRequest ด้วย requestId ที่สร้างขึ้นใหม่ซึ่งอาจเป็น string แบบสุ่ม การประชุมคือ ไม่พร้อมกัน แต่คุณสามารถตรวจสอบสถานะคำขอเพื่อ บอกให้ผู้ใช้รู้ว่าเกิดอะไรขึ้น

เช่น หากต้องการขอให้สร้างการประชุมสำหรับกิจกรรมที่มีอยู่ ให้ทำดังนี้

JavaScript

const eventPatch = {
  conferenceData: {
    createRequest: {requestId: "7qxalsvy0e"}
  }
};

gapi.client.calendar.events.patch({
  calendarId: "primary",
  eventId: "7cbh8rpc10lrc0ckih9tafss99",
  resource: eventPatch,
  sendNotifications: true,
  conferenceDataVersion: 1
}).execute(function(event) {
  console.log("Conference created for event: %s", event.htmlLink);
});

การตอบกลับทันทีต่อการเรียกนี้อาจยังไม่มีฟิลด์ conferenceData; จะระบุด้วยรหัสสถานะ pending ใน สถานะ ด้วย รหัสสถานะจะเปลี่ยนเป็น success หลังจากข้อมูลการประชุมคือ ป้อนข้อมูลแล้ว ฟิลด์ entryPoints จะมีข้อมูลเกี่ยวกับวิดีโอและ URI ของโทรศัพท์เพื่อให้ผู้ใช้โทรเข้าได้

ถ้าต้องการวางกำหนดการกิจกรรมหลายรายการในปฏิทิน รายละเอียดการประชุม คุณสามารถคัดลอก conferenceData ทั้งกิจกรรมจากกิจกรรมเดียวไปยัง อีกรายการ

การคัดลอกมีประโยชน์ในบางสถานการณ์ ตัวอย่างเช่น สมมติว่าคุณกำลังพัฒนา ใบสมัครสรรหาบุคลากรที่จัดตั้งกิจกรรมแยกต่างหากสำหรับผู้สมัครและ ผู้สัมภาษณ์ต้องการปกป้องตัวตนของผู้สัมภาษณ์ ต้องการให้ผู้เข้าร่วมทุกคนเข้าร่วมการประชุมสายเดียวกัน