ลองนึกถึงแอปที่ช่วยให้ผู้ใช้ค้นหาเส้นทางเดินป่าที่ดีที่สุด เมื่อเพิ่มแผนการเดินป่าเป็นกิจกรรมในปฏิทิน ผู้ใช้จะได้รับความช่วยเหลือมากมายในการจัดระเบียบโดยอัตโนมัติ Google ปฏิทินจะช่วยนักเรียนในการแชร์แผนการและเตือน แผนการเพื่อให้เตรียมตัวได้โดยไม่รู้สึกเครียด นอกจากนี้ การผสานรวมผลิตภัณฑ์ของ Google อย่างราบรื่นทำให้ Google Now ส่งคำสั่ง ping ถึงพวกเขาเกี่ยวกับเวลาออกเดินทาง และ Google Maps จะพาลูกค้าไปยังจุดนัดพบตรงเวลา
บทความนี้อธิบายวิธีสร้างกิจกรรมในปฏิทินและเพิ่มลงในปฏิทินของผู้ใช้
เพิ่มกิจกรรม
หากต้องการสร้างเหตุการณ์ ให้เรียกใช้เมธอด events.insert()
ที่มีพารามิเตอร์ต่อไปนี้เป็นอย่างน้อย
calendarId
คือตัวระบุปฏิทินและอาจเป็นที่อยู่อีเมลของปฏิทินที่ใช้สร้างกิจกรรม หรือคีย์เวิร์ดพิเศษ'primary'
ที่จะใช้ปฏิทินหลักของผู้ใช้ที่เข้าสู่ระบบ หากไม่ทราบอีเมลของปฏิทินที่ต้องการใช้ คุณสามารถดูได้ในการตั้งค่าปฏิทินใน UI ทางเว็บของ Google ปฏิทิน (ในส่วน "ที่อยู่ปฏิทิน") หรือจะค้นหาจากผลจากการเรียกใช้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
// Refer to the PHP quickstart on how to setup the environment:
// https://developers.google.com/calendar/quickstart/php
// Change the scope to Google_Service_Calendar::CALENDAR and delete any stored
// credentials.
$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 ส่วน ดังนี้
- รับ URL ของไฟล์
alternateLink
,title
และmimeType
จาก ทรัพยากร Drive API Files ซึ่งโดยปกติจะใช้เมธอดfiles.get()
- สร้างหรืออัปเดตเหตุการณ์ที่มีการตั้งค่าช่อง
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
ดังต่อไปนี้
- Hangouts สำหรับผู้ใช้ทั่วไป (
eventHangout
) - Hangouts แบบคลาสสิกสำหรับ Google Workspace ผู้ใช้
(เลิกใช้งานแล้ว;
eventNamedHangout
) - 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
ทั้งหมดจากกิจกรรมหนึ่งไปยังอีกกิจกรรมหนึ่งได้
การคัดลอกจะมีประโยชน์ในบางสถานการณ์ ตัวอย่างเช่น สมมติว่าคุณกำลังพัฒนาใบสมัครสรรหาบุคลากรที่จัดกิจกรรมสำหรับผู้สมัครและผู้สัมภาษณ์แยกกัน คุณต้องการปกป้องตัวตนของผู้สัมภาษณ์ แต่ก็ต้องการให้ผู้เข้าร่วมทุกคนเข้าร่วมการประชุมสายเดียวกันด้วย