建構步驟

本指南說明如何建立步驟,供使用者新增至 Google Workspace Flows 的流程。

步驟是流程中一系列工作中的單一步驟。步驟無法啟動流程。

舉例來說,假設某個步驟會執行算術運算。這個函式會要求使用者提供兩個值和一項數學運算。然後對這些值執行數學運算,並輸出結果。

使用者在流程中設定計算機步驟。

圖 1:使用者在流程中設定計算機步驟。

如要建構步驟,請在外掛程式的資訊清單檔案中設定步驟,在 Google Workspace 外掛程式的程式碼中編寫應用程式邏輯,然後部署及測試步驟。在 Alpha 版期間,請勿發布擴充 Flows 的外掛程式。

定義步驟

如要設定步驟,請在資訊清單檔案中定義步驟,並在程式碼中編寫應用程式邏輯。

在資訊清單檔案中定義步驟

在資訊清單檔案中,appsscript.json

  1. onConfigFunctiononExecuteFunction 設為外掛程式碼中對應函式的名稱。在本範例中,函式稱為 onConfigCalculate()onExecuteCalculate()
    • onConfigFunction 會設定及設定步驟。如有必要,請從使用者收集執行步驟所需的資料,例如傳送電子郵件的地址。在本指南的範例中,我們會要求兩個值和一項數學運算。
    • onExecuteFunction 執行步驟。如果資料是從使用者收集而來,該資料會傳遞至這個函式。如果適用,則會傳回輸出內容。在本指南的範例中,輸出數學計算結果。
  2. 設定必要輸入和輸出內容,讓步驟收集資料並傳送至後續步驟。在本範例中,請向使用者索取兩個值,以及 inputs[] 中定義的數學運算。根據 outputs[] 輸出計算結果。

    如要進一步瞭解輸入和輸出內容,請參閱輸入變數輸出變數。如要確保流程順利執行,請驗證輸入變數

以下是「計算機」步驟的資訊清單檔案:

JSON

{
  "timeZone": "America/Los_Angeles",
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "addOns": {
    "common": {
      "name": "Calculator",
      "logoUrl": "https://www.gstatic.com/images/branding/productlogos/calculator_search/v1/web-24dp/logo_calculator_search_color_1x_web_24dp.png",
      "useLocaleFromApp": true
    },
    "flows": {
      "workflowElements": [
        {
          "id": "actionElement",
          "state": "ACTIVE",
          "name": "Calculate",
          "description": "Asks the user for two values and a math operation, then performs the math operation on the values and outputs the result.",
          "workflowAction": {
            "inputs": [
              {
                "id": "value1",
                "description": "value1",
                "cardinality": "SINGLE",
                "dataType": {
                  "basicType": "INTEGER"
                }
              },
              {
                "id": "value2",
                "description": "value2",
                "cardinality": "SINGLE",
                "dataType": {
                  "basicType": "INTEGER"
                }
              },
              {
                "id": "operation",
                "description": "operation",
                "cardinality": "SINGLE",
                "dataType": {
                  "basicType": "STRING"
                }
              }
            ],
            "outputs": [
              {
                "id": "result",
                "description": "Calculated result",
                "cardinality": "SINGLE",
                "dataType": {
                  "basicType": "INTEGER"
                }
              }
            ],
            "onConfigFunction": "onConfigCalculate",
            "onExecuteFunction": "onExecuteCalculate"
          }
        }
      ]
    }
  }
}

接著,請在程式碼中定義步驟,編寫支援程式碼。

在程式碼中定義步驟

在應用程式程式碼中,請執行下列操作:

  1. 撰寫 onConfigFunction,在本例中稱為 onConfigCalculate()。 使用者將步驟新增至流程後,即可在流程建構工具中設定步驟的各個層面。如要向使用者收集必要資訊,onConfigFunction 會定義設定卡片。

    資訊卡是外掛程式使用者介面的建構元素。資訊卡支援定義的版面配置、按鈕等互動式 UI 元素,以及圖片等多媒體。您可以使用資訊卡從使用者取得步驟執行所需的資料,例如傳送電子郵件時的電子郵件地址。

    OnConfigFunction 會傳回包含資訊卡的 RenderActions 物件。這張卡片定義了使用者設定步驟資料的 UI。在這個範例中,onConfigFunction 會建構一張卡片,要求使用者提供兩個值和數學運算。

  2. 在本例中,請編寫名為 onExecuteFunctiononExecuteCalculate()。 當步驟在流程中執行時,系統會執行 OnExecuteFunction。使用者在設定期間設定的任何輸入值 (如 onConfigurationFunction 中所定義),都會傳遞至 OnExecuteFunction

    編寫 OnExecuteFunction(),以便使用提供的輸入內容同步執行工作。請注意,OnExecuteFunction() 必須傳回流程資訊清單中定義的所有輸出內容,否則會發生錯誤。

這個程式碼範例包含支援函式 outputVariables(),可建構並傳送提供的變數做為步驟的輸出內容。

您的步驟已可供測試。

以下是「計算機」步驟的程式碼:

Apps Script

/**
 * Generates and displays a configuration card for the sample calculation step.
 *
 * This function creates a card with input fields for two values and a drop-down
 * for selecting an arithmetic operation. The card also includes a "Save"
 * button to save the step configuration for the workflow.
 *
 * The input fields are configured to let the user select outputs from previous
 * workflow steps as input values using the `hostAppDataSource` property.
 */
function onConfigCalculate() {
  var card = {
    "sections": [
      {
        "header": "Step example: Calculate",
        "widgets": [
          {
            "textInput": {
              "name": "value1",
              "label": "First value",
              "hostAppDataSource" : {
                "workflowDataSource" : {
                  "includeVariables" : true
                }
              }
            }
          },
          {
            "selectionInput": {
              "name": "operation",
              "label": "Operation",
              "type": "DROPDOWN",
              "items": [
                {
                  "text": "+",
                  "value": "+",
                },
                {
                  "text": "-",
                  "value": "-",
                },
                {
                  "text": "x",
                  "value": "x",
                },
                {
                  "text": "/",
                  "value": "/",
                }
              ]
            }
          },
          {
            "textInput": {
              "name": "value2",
              "label": "Second value",
              "hostAppDataSource" : {
                "workflowDataSource" : {
                  "includeVariables" : true
                }
              }
            }
          }
        ]
      }
    ]
  };
  return {
    "action": {
      "navigations": [{
        "push_card": card
      }]
    }
  };
}

/**
 * Returns output variables from a step.
 *
 * This function constructs an object that, when returned, sends the
 * provided variable values as output from the current step.
 * The variable values are logged to the console for debugging purposes.
 */
function outputVariables(variableValues) {
  var renderAction =  {
    "hostAppAction" : {
      "workflowAction" : {
        "returnOutputVariablesAction" : {
          "variables" : variableValues
        }
      }
    }
  };
  console.log("renderAction: " + JSON.stringify(renderAction));
  return renderAction;
}

/**
 * Executes the calculation step based on the inputs from a flow event.
 *
 * This function retrieves input values and the operation from the flow event,
 * performs the calculation, and returns the result as an output variable.
 * The function logs the event for debugging purposes.
 */
function onExecuteCalculate(event) {
  console.log("output: " + JSON.stringify(event));
  var calculatedValue = 0;
  var value1 = event.workflow.actionInvocation.inputs["value1"].integerValues[0];
  var value2 = event.workflow.actionInvocation.inputs["value2"].integerValues[0];
  var operation = event.workflow.actionInvocation.inputs["operation"].stringValues[0];

  if (operation == "+") {
    calculatedValue = value1 + value2;
  } else if (operation == "-") {
    calculatedValue = value1 - value2;
  } else if (operation == "x") {
    calculatedValue = value1 * value2;
  } else if (operation == "/") {
    calculatedValue = value1 / value2;
  }
  return outputVariables([{
    "variableId": "result",
    "variableData": {
        "integerValues": [
          calculatedValue
        ]
      },
  }]);
}

測試步驟

如要測試步驟,請為外掛程式設定測試部署作業,將步驟新增至流程,然後執行流程。

  1. 為外掛程式設定測試部署作業:

    1. 在 Apps Script 編輯器中開啟指令碼專案。
    2. 依序點選「部署」>「測試部署作業」
    3. 按一下 [安裝]
    4. 按一下底部的「完成」

    您可以與其他使用者的帳戶共用 Apps Script 專案 (必須具備編輯權限),讓他們測試外掛程式。然後提示使用者按照先前的步驟操作。

    安裝完畢後,外掛程式會立即顯示在 Flows 中。您可能需要重新整理 Flows,外掛程式才會顯示。您也必須先授權外掛程式,才能使用。

    如要進一步瞭解測試部署作業,請參閱「安裝未發布的增益集」。

  2. 開啟「流程」。

  3. 建立包含步驟的流程:

    1. 按一下「新增流程」
    2. 選取流程的啟動方式。測試步驟時,建議選擇可自行觸發的啟動器,例如傳送電子郵件給自己。如果步驟需要輸入變數,請將輸入變數設定為啟動器的輸出內容。
    3. 按一下「新增步驟」。選取您建立或更新的步驟,稱為「計算」
    4. 設定步驟。在計算步驟中,選擇兩個值和數學運算。系統會自動儲存步驟。
    5. 如要測試步驟的輸出內容,請新增另一個步驟。舉例來說,如要在電子郵件中新增輸出內容,可以新增 Gmail 的「傳送郵件」步驟。在「訊息」中,按一下「變數」,然後選取步驟的輸出內容。在計算步驟中,依序選取「變數」 >「步驟 2:計算結果」 >「計算結果」。變數會以方塊形式顯示在「訊息」欄位中。
    6. 按一下「開啟」。流程已可執行。
  4. 如要執行流程,請啟動流程的啟動條件。舉例來說,如果流程會在收到電子郵件時啟動,請傳送電子郵件給自己。

  5. 確認流程是否正常運作。前往流程建構工具的「活動」分頁,即可查看記錄。如要瞭解如何在「活動」分頁中建立自訂記錄,請參閱活動記錄