イベントを作成する

ユーザーが最適なハイキング コースを見つけるのに役立つアプリを想像してみてください。ハイキング プランをカレンダーの予定として追加することで、ユーザーは自動的に整理された状態を維持できます。Google カレンダーでは、プランを共有したり、リマインダーを設定したりできるため、ストレスなく準備できます。また、Google サービスのシームレスな統合により、Google Now から出発時刻が通知され、Google マップで待ち合わせ場所までの経路が案内されます。

この記事では、カレンダーの予定を作成してユーザーのカレンダーに追加する方法について説明します。

イベントの追加

予定を作成するには、 events.insert() メソッドを少なくとも次のパラメータを指定して呼び出します。

  • calendarId はカレンダーの識別子です。予定を作成するカレンダーのメールアドレス 、またはログインしているユーザーのメイン カレンダーを使用する特別なキーワード 'primary' のいずれかを指定できます。使用するカレンダーのメールアドレスがわからない場合は、Google カレンダー ウェブ UI のカレンダーの設定([カレンダー アドレス] セクション)で確認するか、calendarList.list() 呼び出しの結果で確認できます。
  • event は、開始時刻や終了時刻など、必要な詳細情報を含む作成する予定です。必須フィールドは startend の 2 つだけです。予定 フィールドの完全なセットについては、 eventリファレンスをご覧ください。

予定を正常に作成するには、次の操作を行う必要があります。

  • OAuth スコープを https://www.googleapis.com/auth/calendar に設定して、ユーザーのカレンダーへの編集アクセス権を取得します。
  • 認証されたユーザーが、指定した calendarIdを持つカレンダーに対する書き込みアクセス権を持っていることを確認します(たとえば、 calendarList.get()に対して calendarIdを呼び出してaccessRoleを確認します)。

イベント メタデータを追加する

カレンダーの予定を作成するときに、必要に応じてイベント メタデータを追加できます。作成時にメタデータを追加しない場合は、 events.update() を使用して多くのフィールドを更新できます。ただし、イベント ID などの一部のフィールドは、 events.insert() オペレーションでのみ設定できます。

場所
場所フィールドに住所を追加すると、「出発時間」などの機能が有効になり、経路を示す地図が表示されます。
イベント ID
予定を作成するときに、形式要件に準拠した独自のイベント ID を生成できます。これにより、ローカル データベース内のエンティティを Google カレンダーの予定と同期させることができます。また、カレンダー バックエンドでオペレーションが正常に実行された後、どこかの時点で失敗した場合に、予定が重複して作成されるのを防ぐことができます。イベント ID が指定されていない場合、サーバーによって ID が生成されます。詳細については、イベント ID リファレンスをご覧ください。
参加者
作成した予定は、同じイベント ID を持つ参加者のすべてのメインの Google カレンダーに表示されます。挿入リクエストで sendUpdates"all" または "externalOnly" に設定すると、 対応する参加者に予定のメール通知が届きます。詳細については、 複数の参加者がいる予定をご覧ください。

次の例では、予定を作成してメタデータを設定する方法を示します。

Go

// Refer to the Go quickstart on how to setup the environment:
// https://developers.google.com/workspace/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/workspace/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/workspace/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/workspace/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/workspace/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 ドライブのファイルを予定に添付する手順は次のとおりです。

  1. 通常は files.get() メソッドを使用して、 Drive API ファイル リソースからファイルの alternateLink URL、titlemimeType を取得します。
  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()

予定にビデオ会議と電話会議を追加する

予定を HangoutsGoogle Meetの会議に関連付けることで、 ユーザーは電話やビデオ通話でリモートで会議に参加できます。

conferenceData フィールドを使用すると、既存の会議の詳細情報の読み取り、コピー、クリアを行えます。また、新しい会議の生成をリクエストすることもできます。会議の詳細情報の作成と変更を許可するには、conferenceDataVersion リクエスト パラメータを 1 に設定します。

現在サポートされている conferenceData には、 conferenceData.conferenceSolution.key.type で示されるように、次の 3 種類があります。

  1. 一般ユーザー向けハングアウト(eventHangout
  2. Google Workspace ユーザー向けの従来のハングアウト(非推奨、eventNamedHangout
  3. Google Meet(hangoutsMeet

ユーザーの特定のカレンダーでサポートされている会議タイプを確認するには、conferenceProperties.allowedConferenceSolutionTypescalendarscalendarList コレクションを確認します。また、新しく作成したすべての予定に対してハングアウトを作成するかどうかをユーザーが選択しているかどうかを、autoAddHangouts 設定を settings コレクションで確認することで知ることもできます。

conferenceSolution には、type の他に、会議ソリューションを表すために使用できる 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);

予定の新しい会議を作成するには、新しく生成された requestId を含む createRequest を指定します。requestId には任意の string を指定できます。会議は非同期で作成されますが、リクエストのステータスをいつでも確認して、ユーザーに状況を知らせることができます。

たとえば、既存の予定の会議の生成をリクエストするには、次のようにします。

JavaScript

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

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

この呼び出しに対する即時のレスポンスには、完全に設定された conferenceDataが含まれていない場合があります。これは、pendingstatus フィールドのステータス コードで示されます。会議情報が設定されると、ステータス コードが success に変わります。entryPoints フィールドには、ユーザーがダイヤルインできるビデオ URI と電話 URI に関する情報が含まれています。

同じ会議の詳細情報を使用して複数のカレンダーの予定をスケジュールする場合は、ある予定から別の予定に conferenceData 全体をコピーできます。

コピーは特定の状況で役立ちます。たとえば、候補者と面接官の予定を別々に設定する採用アプリを開発しているとします。面接官の身元を保護しながら、すべての参加者が同じグループ通話に参加できるようにする必要があります。