Class Action

動作

這項動作可啟用 UI 元素內的互動功能。這項動作不會直接在用戶端上發生,而是會叫用 Apps Script 回呼函式,並提供選用參數。

適用於 Google Workspace 外掛程式和 Google Chat 應用程式。

const image = CardService.newImage().setOnClickAction(
    CardService.newAction().setFunctionName('handleImageClick').setParameters({
      imageSrc: 'carImage'
    }),
);

方法

方法傳回類型簡短說明
addRequiredWidget(requiredWidget)Action新增這項動作需要的小工具名稱,才能有效提交。
setAllWidgetsAreRequired(allWidgetsAreRequired)Action指出這項動作是否需要所有小工具的輸入內容。
setFunctionName(functionName)Action設定要呼叫的回呼函式名稱。
setInteraction(interaction)Action設定與使用者的互動,只有在開啟對話方塊時才需要。
setLoadIndicator(loadIndicator)Action設定動作進行時顯示的載入指標。
setParameters(parameters)Action允許將自訂參數傳遞至回呼函式。
setPersistValues(persistValues)Action指出表單值是由用戶端值還是伺服器值決定 (在動作回應更新表單的 Card 後)。

內容詳盡的說明文件

addRequiredWidget(requiredWidget)

新增這項動作需要的微件名稱,才能有效提交。如果這個清單中的小工具在叫用此動作時沒有值,系統會中止表單提交作業。

適用於 Google Workspace 外掛程式和 Google Chat 應用程式。

const textInput = CardService.newTextInput()
                      .setFieldName('text_input_1')
                      .setTitle('Text input title');

// Creates a footer button that requires an input from the above TextInput
// Widget.
const action = CardService.newAction()
                   .setFunctionName('notificationCallback')
                   .addRequiredWidget('text_input_1');
const fixedFooter = CardService.newFixedFooter().setPrimaryButton(
    CardService.newTextButton().setText('help').setOnClickAction(action),
);

參數

名稱類型說明
requiredWidgetString這個動作所需的小工具名稱。

回攻員

Action - 這個物件,用於鏈結。


setAllWidgetsAreRequired(allWidgetsAreRequired)

指出這項動作是否需要所有小工具的輸入內容。

適用於 Google Workspace 外掛程式和 Google Chat 應用程式。

// Creates a button with an action that requires inputs from all widgets.
const button = CardService.newTextButton()
                   .setText('Create notification')
                   .setOnClickAction(
                       CardService.newAction().setAllWidgetsAreRequired(true));

參數

名稱類型說明
allWidgetsAreRequiredBoolean動作是否需要所有小工具的輸入內容。預設值為 false

回攻員

Action - 這個物件,用於鏈結。


setFunctionName(functionName)

設定要呼叫的回呼函式名稱。必填。

參數

名稱類型說明
functionNameString函式名稱。您可以使用隨附程式庫中的函式,例如 Library.libFunction1

回攻員

Action - 這個物件,用於鏈結。


setInteraction(interaction)

設定與使用者的互動,只有在開啟對話方塊時才需要。如未指定,應用程式會執行 Action (例如開啟連結或執行函式) 來回應,與正常情況相同。

僅適用於 Google Chat 擴充應用程式。不適用於 Google Workspace 外掛程式。

const action = CardService.newAction()
                   .setFunctionName('handleDialog')
                   .setInteraction(CardService.Interaction.OPEN_DIALOG);

參數

名稱類型說明
interactionInteraction要指定的互動。

回攻員

Action - 這個物件,用於鏈結。


setLoadIndicator(loadIndicator)

設定動作進行時顯示的載入指標。

參數

名稱類型說明
loadIndicatorLoadIndicator要顯示的指標。

回攻員

Action - 這個物件,用於鏈結。


setParameters(parameters)

可將自訂參數傳遞至回呼函式。選填。

參數

名稱類型說明
parametersObject鍵和值都必須是字串。

回攻員

Action - 這個物件,用於鏈結。


setPersistValues(persistValues)

指出表單值是由用戶端值還是伺服器值決定 (在動作回應更新表單的 Card 後)。設為 true 時,用戶端的值會在伺服器回應後保留。設為 false 時,伺服器的值會覆寫表單值。預設值為 false

保留用戶端值有助於避免使用者編輯後,表單意外變更的情況。舉例來說,使用者在提交表單後,但在伺服器回應前編輯 TextInput,如果值會保留,伺服器回應更新 Card 後,使用者所做的編輯內容仍會保留;否則表單值會還原為使用者原本提交的值。

保存用戶端值可能會干擾指令碼清除表單欄位或覆寫表單值的能力,因此請避免為這類功能啟用保存功能。如果沒有持續性,建議您使用事件的 LoadIndicator.SPINNER,因為這會鎖定 UI,並防止使用者在伺服器回應前編輯內容。或者,您也可以使用 LoadIndicator.NONE,並確保表單中的每個元素都有 onChange 動作。

// Creates a button with an action that persists the client's values as the
// on-click action.
const button =
    CardService.newTextButton()
        .setText('Create notification')
        .setOnClickAction(
            CardService.newAction().setPersistValues(true).setFunctionName(
                'functionName'),
        );

參數

名稱類型說明
persistValuesBoolean是否要保留值。預設值為 false

回攻員

Action - 這個物件,用於鏈結。

已淘汰的方法