Action
オブジェクトを使用すると、Google Workspace アドオンにインタラクティブな動作を組み込むことができます。ユーザーがアドオン UI のウィジェット(ボタンなど)を操作したときの動作を定義します。
アクションは、アクションをトリガーする条件も定義するウィジェット ハンドラ関数を使用して、特定のウィジェットに関連付けられます。アクションがトリガーされると、指定されたコールバック関数が実行されます。このコールバック関数には、ユーザーのクライアント側の操作に関する情報を保持するイベント オブジェクトが渡されます。コールバック関数を実装して、特定のレスポンス オブジェクトを返す必要があります。
たとえば、クリックすると新しいカードをビルドして表示するボタンが必要であるとします。そのためには、新しいボタン ウィジェットを作成し、ボタン ウィジェット ハンドラ関数 setOnClickAction(action)
を使用してカード作成 Action
を設定する必要があります。定義した Action
は、ボタンがクリックされたときに実行される Apps Script コールバック関数を指定します。この場合、コールバック関数を実装して必要なカードを作成し、ActionResponse
オブジェクトを返します。レスポンス オブジェクトは、コールバック関数がビルドしたカードを表示するようにアドオンに指示します。
このページでは、アドオンに含めることができるカレンダー固有のウィジェット アクションについて説明します。
カレンダーの操作
カレンダーを拡張する Google Workspace アドオンには、カレンダー固有のウィジェット アクションがいくつか追加されています。これらのアクションでは、特殊なレスポンス オブジェクトを返すために、関連付けられたアクション コールバック関数が必要です。
アクションを試行しました | コールバック関数は |
---|---|
参加者を追加する | CalendarEventActionResponse |
会議データの設定 | CalendarEventActionResponse |
添付ファイルの追加 | CalendarEventActionResponse |
ウィジェットのアクションとレスポンス オブジェクトを利用するには、次の条件をすべて満たす必要があります。
- この操作は、ユーザーがカレンダーの予定を開いているときにトリガーされます。
- アドオンの
addOns.calendar.currentEventAccess
マニフェスト フィールドがWRITE
またはREAD_WRITE
に設定されている。 - このアドオンには
https://www.googleapis.com/auth/calendar.addons.current.event.write
カレンダー スコープが含まれています。
また、アクション コールバック関数で行った変更は、ユーザーがカレンダーの予定を保存するまで保存されません。
コールバック関数を使用して参加者を追加する
次の例は、編集中のカレンダーの予定に特定の参加者を追加するボタンを作成する方法を示しています。
/**
* Build a simple card with a button that sends a notification.
* This function is called as part of the eventOpenTrigger that builds
* a UI when the user opens an event.
*
* @param e The event object passed to eventOpenTrigger function.
* @return {Card}
*/
function buildSimpleCard(e) {
var buttonAction = CardService.newAction()
.setFunctionName('onAddAttendeesButtonClicked');
var button = CardService.newTextButton()
.setText('Add new attendee')
.setOnClickAction(buttonAction);
// Check the event object to determine if the user can add
// attendees and disable the button if not.
if (!e.calendar.capabilities.canAddAttendees) {
button.setDisabled(true);
}
// ...continue creating card sections and widgets, then create a Card
// object to add them to. Return the built Card object.
}
/**
* Callback function for a button action. Adds attendees to the
* Calendar event being edited.
*
* @param {Object} e The action event object.
* @return {CalendarEventActionResponse}
*/
function onAddAttendeesButtonClicked (e) {
return CardService.newCalendarEventActionResponseBuilder()
.addAttendees(["aiko@example.com", "malcom@example.com"])
.build();
}
コールバック関数を使用して会議データを設定する
この操作により、開始イベントの会議データが設定されます。この会議データには、ユーザーが目的のソリューションを選択したときにアクションをトリガーしていないため、会議ソリューション ID を指定する必要があります。
次の例は、編集中のイベントの会議データを設定するボタンを作成する方法を示しています。
/**
* Build a simple card with a button that sends a notification.
* This function is called as part of the eventOpenTrigger that builds
* a UI when the user opens a Calendar event.
*
* @param e The event object passed to eventOpenTrigger function.
* @return {Card}
*/
function buildSimpleCard(e) {
var buttonAction = CardService.newAction()
.setFunctionName('onSaveConferenceOptionsButtonClicked')
.setParameters(
{'phone': "1555123467", 'adminEmail': "joyce@example.com"});
var button = CardService.newTextButton()
.setText('Add new attendee')
.setOnClickAction(buttonAction);
// Check the event object to determine if the user can set
// conference data and disable the button if not.
if (!e.calendar.capabilities.canSetConferenceData) {
button.setDisabled(true);
}
// ...continue creating card sections and widgets, then create a Card
// object to add them to. Return the built Card object.
}
/**
* Callback function for a button action. Sets conference data for the
* Calendar event being edited.
*
* @param {Object} e The action event object.
* @return {CalendarEventActionResponse}
*/
function onSaveConferenceOptionsButtonClicked(e) {
var parameters = e.commonEventObject.parameters;
// Create an entry point and a conference parameter.
var phoneEntryPoint = ConferenceDataService.newEntryPoint()
.setEntryPointType(ConferenceDataService.EntryPointType.PHONE)
.setUri('tel:' + parameters['phone']);
var adminEmailParameter = ConferenceDataService.newConferenceParameter()
.setKey('adminEmail')
.setValue(parameters['adminEmail']);
// Create a conference data object to set to this Calendar event.
var conferenceData = ConferenceDataService.newConferenceDataBuilder()
.addEntryPoint(phoneEntryPoint)
.addConferenceParameter(adminEmailParameter)
.setConferenceSolutionId('myWebScheduledMeeting')
.build();
return CardService.newCalendarEventActionResponseBuilder()
.setConferenceData(conferenceData)
.build();
}
コールバック関数を使用して添付ファイルを追加する
次の例は、編集中のカレンダーの予定に添付ファイルを追加するボタンを作成する方法を示しています。
/**
* Build a simple card with a button that creates a new attachment.
* This function is called as part of the eventAttachmentTrigger that
* builds a UI when the user goes through the add-attachments flow.
*
* @param e The event object passed to eventAttachmentTrigger function.
* @return {Card}
*/
function buildSimpleCard(e) {
var buttonAction = CardService.newAction()
.setFunctionName('onAddAttachmentButtonClicked');
var button = CardService.newTextButton()
.setText('Add a custom attachment')
.setOnClickAction(buttonAction);
// Check the event object to determine if the user can add
// attachments and disable the button if not.
if (!e.calendar.capabilities.canAddAttachments) {
button.setDisabled(true);
}
// ...continue creating card sections and widgets, then create a Card
// object to add them to. Return the built Card object.
}
/**
* Callback function for a button action. Adds attachments to the Calendar
* event being edited.
*
* @param {Object} e The action event object.
* @return {CalendarEventActionResponse}
*/
function onAddAttachmentButtonClicked(e) {
return CardService.newCalendarEventActionResponseBuilder()
.addAttachments([
CardService.newAttachment()
.setResourceUrl("https://example.com/test")
.setTitle("Custom attachment")
.setMimeType("text/html")
.setIconUrl("https://example.com/test.png")
])
.build();
}
添付ファイル アイコンの設定
添付ファイルのアイコンは、Google のインフラストラクチャでホストする必要があります。詳しくは、添付ファイルのアイコンを指定するをご覧ください。