サイレント モード、不在、勤務場所の予定を管理する

このページでは、Google Calendar API を使用して、表示するイベントを作成する方法について説明します。 Google カレンダー ユーザーのステータスも管理できます。ステータス イベントは、ユーザーの現在地または たとえば、サイレント モード中、不在中、 特定の場所での勤務などです

Google カレンダーでは、サイレント モード、不在、勤務時間を設定できる カスタムのステータスと現在地を示すことができます。これらの機能は メイン カレンダーと、一部の Google カレンダー ユーザーのみが使用できます。

詳しくは、Google でサイレント モードを使用する カレンダー および できます

カレンダーのステータス イベントの読み取りと一覧表示

次の場所でカレンダーのステータスのイベントを読み、一覧表示できます。 次の Events リソース: Calendar API。

ステータス イベントを読み取るには、 events.get メソッドを使用し、 イベントの eventId

ステータス イベントを一覧表示するには、 events.list メソッド(いずれか 1 つを指定する) 1 つ以上 eventTypes フィールド:

  • 'focusTime'
  • 'outOfOffice'
  • 'workingLocation'

次に、返された Event オブジェクトで、 eventType フィールドには 値を確認できます。詳細は、対応するフィールドを参照してください。 Google カレンダーでユーザーが作成したステータス:

ステータス イベントの変更を受け取る

ステータス イベントの変更は、 次の Events リソース: Calendar API。

こちらの events.watch メソッドを使用し、 calendarId の 登録するカレンダーと、1 つ以上の eventTypes フィールド:

  • 'focusTime'
  • 'outOfOffice'
  • 'workingLocation'

カレンダーのステータスの予定を作成、更新する

ステータス イベントを作成するには、 Events リソースに events.insert メソッドを使用し、 必須項目です。

「新規顧客の獲得」を使用してステータス イベントを更新すると、 events.update メソッド: イベント 必須フィールドを維持する必要があります

サイレント モードにする

サイレント モードの予定を作成するには:

  • eventType の設定 'focusTime'
  • 次を含める: focusTimeProperties 表示されます。
  • transparency を設定します。 フィールドを 'opaque' に設定します。
  • イベントの start と 時間指定されたイベントとして end 個のフィールドを設定します (開始時間と終了時間を指定する)。
    サイレント モードを終日の予定にすることはできません。

機能について詳しくは、Google でサイレント モードを使用する カレンダー

「不在」を作成

不在の予定を作成するには:

  • eventType の設定 'outOfOffice'
  • 次を含める: outOfOfficeProperties 表示されます。
  • transparency を設定します。 フィールドを 'opaque' に設定します。
  • イベントの start と 時間指定されたイベントとして end 個のフィールドを設定します (開始時間と終了時間を指定する)。
    不在の予定を終日の予定にすることはできません。

機能の詳細については、不在時に表示するをご覧ください。 オフィス

勤務場所の作成

勤務場所の予定を作成するには:

  • eventType の設定 'workingLocation'
  • 次を含める: workingLocationProperties 表示されます。
  • visibility フィールドを設定する 宛先: 'public'
  • transparency を設定します。 フィールドを 'transparent' に設定します。
  • イベントの startend フィールドは、次のいずれかにする必要があります。

    • 時間指定されたイベント(開始時間と終了時間を指定する)。
    • 次の期間にまたがる終日の予定(開始日と終了日を指定) あります。

    終日の勤務場所の予定は、複数の日にまたがって表示することはできませんが、時間制限付きの予定 できます。

次のフィールドは省略可能ですが、最適なユーザー エクスペリエンスのために指定することをおすすめします。 メッセージに officeLocation:

バッチ エンドポイントを使用して勤務場所の予定を作成、更新することはできません サポートされません。

機能の詳細については、業務時間と勤務時間を設定する 場所 および ユーザー

重複する勤務場所の予定を表示する方法

ユーザーのカレンダーには、勤務場所の予定が重複して追加されることがあります。 重複する時間があります。つまり、特定の時間に複数の動作が 表示されます。表示できるビジネス情報が 1 つだけの場合 複数の場所で一貫してその場所が表示されるよう 説明します。その際は、次のガイドラインを参考にして イベントが表示されます。

  • 時間指定イベントは 終日の予定よりも優先されます。
  • 単一の予定は定期的な予定よりも優先され、その予定は 例外があります。
  • 後で開始するイベントは、それより前に開始するイベントよりも優先されます。
  • 期間が短いイベントは、長いイベントよりも優先されます あります。
  • 最近作成された予定は、作成された予定よりも優先されます 挙げられます
  • 部分的に重なっているイベントは、それぞれ 2 つの異なるイベントとして表示されます。 それぞれが独自の勤務場所を使っています

Google Apps Script でステータス イベントを作成する

Google Apps Script は、JavaScript ベースのクラウド 統合するビジネス アプリケーションを構築できるスクリプト言語を Google Workspace ですスクリプトはブラウザベースのコードエディタで開発され、 Google のサーバーに保存され、実行されます。Google Apps Script もご覧ください。 クイックスタートをご覧ください。 Google Calendar API にリクエストを送信する Apps Script。

次の手順では、 Google Calendar API(Google の高度なサービス) Google Apps Script。Google Calendar API のリソースとメソッドの完全なリストについては、 リファレンス ドキュメントをご覧ください。

スクリプトを作成して設定する

  1. スクリプトを作成するには script.google.com/create.
  2. 左側のペインで [サービス] の横にある [サービスを追加] をクリックします。
  3. [Google Calendar API] を選択し、[追加] をクリックします。
  4. 有効にすると、API が左側のペインに表示されます。利用可能なメソッドと API のクラスをリストするには、エディタで Calendar キーワードを使用します。

(省略可)Google Cloud プロジェクトを更新する

各 Google Apps Script プロジェクトには Google Cloud プロジェクトが関連付けられています。お客様の Google Apps Script がデフォルト プロジェクトを使用して 作成します。カスタム Google Cloud プロジェクトを使用する場合は、次の手順を行います。 ] をクリックして、スクリプトに関連付けられているプロジェクトを更新します。

  1. エディタの左側で、[プロジェクトの設定] をクリックします。
  2. [Google Cloud Platform(GCP)プロジェクト] で、[プロジェクトを変更] をクリックします。
  3. デベロッパー アカウントに登録されている Google Cloud プロジェクトのプロジェクト番号を入力します。 [プレビュー プログラム] をクリックし、[プロジェクトを設定] をクリックします。
  4. 左側にあるエディタ アイコン を選択すると、 コードエディタに戻ります

スクリプトにコードを追加する

次のコードサンプルは、ステータス イベントの作成、読み取り、一覧表示を行う方法を示しています。 メイン カレンダーに表示されます。

  1. 次のコードをコードエディタに貼り付けます。

    <ph type="x-smartling-placeholder">
    /** Creates a focus time event. */
    function createFocusTime() {
      const event = {
        start: { dateTime: '2023-11-14T10:00:00+01:00' },
        end: { dateTime: '2023-11-14T12:00:00+01:00' },
        eventType: 'focusTime',
        focusTimeProperties: {
          chatStatus: 'doNotDisturb',
          autoDeclineMode: 'declineOnlyNewConflictingInvitations',
          declineMessage: 'Declined because I am in focus time.',
        }
      }
      createEvent(event);
    }
    
    /** Creates an out of office event. */
    function createOutOfOffice() {
      const event = {
        start: { dateTime: '2023-11-15T10:00:00+01:00' },
        end: { dateTime: '2023-11-15T18:00:00+01:00' },
        eventType: 'outOfOffice',
        outOfOfficeProperties: {
          autoDeclineMode: 'declineOnlyNewConflictingInvitations',
          declineMessage: 'Declined because I am on vacation.',
        }
      }
      createEvent(event);
    }
    
    /** Creates a working location event. */
    function createWorkingLocation() {
      const event = {
        start: { date: "2023-06-01" },
        end: { date: "2023-06-02" },
        eventType: "workingLocation",
        visibility: "public",
        transparency: "transparent",
        workingLocationProperties: {
          type: 'customLocation',
          customLocation: { label: "a custom location" },
        }
      }
      createEvent(event);
    }
    
    /**
      * Creates a Calendar event.
      * See https://developers.google.com/calendar/api/v3/reference/events/insert
      */
    function createEvent(event) {
      const calendarId = 'primary';
    
      try {
        var response = Calendar.Events.insert(event, calendarId);
        var event = (response.eventType === 'workingLocation') ? parseWorkingLocation(response) : response;
        console.log(event);
      } catch (exception) {
        console.log(exception.message);
      }
    }
    
    /**
      * Reads the event with the given eventId.
      * See https://developers.google.com/calendar/api/v3/reference/events/get
      */
    function readEvent() {
      const calendarId = 'primary';
    
      // Replace with a valid eventId.
      const eventId = "sample-event-id";
    
      try {
        var response = Calendar.Events.get(calendarId, eventId);
        var event = (response.eventType === 'workingLocation') ? parseWorkingLocation(response) : response;
        console.log(event);
      } catch (exception) {
        console.log(exception.message);
      }
    }
    
    /** Lists focus time events. */
    function listFocusTimes() {
      listEvents('focusTime');
    }
    
    /** Lists out of office events. */
    function listOutOfOffices() {
      listEvents('outOfOffice');
    }
    
    /** Lists working location events. */
    function listWorkingLocations() {
      listEvents('workingLocation');
    }
    
    /**
      * Lists events with the given event type.
      * See https://developers.google.com/calendar/api/v3/reference/events/list
      */
    function listEvents(eventType = 'default') {
      const calendarId = 'primary'
    
      // Query parameters for the list request.
      const optionalArgs = {
        eventTypes: [eventType],
        showDeleted: false,
        singleEvents: true,
        timeMax: '2023-04-01T00:00:00+01:00',
        timeMin: '2023-03-27T00:00:00+01:00',
      }
      try {
        var response = Calendar.Events.list(calendarId, optionalArgs);
        response.items.forEach(event =>
          console.log(eventType === 'workingLocation' ? parseWorkingLocation(event) : event));
      } catch (exception) {
        console.log(exception.message);
      }
    }
    
    /**
      * Parses working location properties of an event into a string.
      * See https://developers.google.com/calendar/api/v3/reference/events#resource
      */
    function parseWorkingLocation(event) {
      if (event.eventType != "workingLocation") {
        throw new Error("'" + event.summary + "' is not a working location event.");
      }
    
      var location = 'No Location';
      const workingLocation = event.workingLocationProperties;
      if (workingLocation) {
        if (workingLocation.type === 'homeOffice') {
          location = 'Home';
        }
        if (workingLocation.type === 'officeLocation') {
          location = workingLocation.officeLocation.label;
        }
        if (workingLocation.type === 'customLocation') {
          location = workingLocation.customLocation.label;
        }
      }
      return `${event.start.date}: ${location}`;
    }
    

サンプルコードの実行

  1. コードエディタの上にあるプルダウン メニューから、実行する関数を選択します。 [実行] をクリックします。
  2. 最初の実行で、アクセスの承認を求められます。確認して許可 Apps Script を使用してカレンダーにアクセスできます。
  3. スクリプトの実行結果は、 実行ログ: ウィンドウの下部に表示されます。