本指南說明如何建立輸出變數。
步驟會傳回輸出變數,並可傳送至另一個步驟。舉例來說,您可以將電子郵件地址傳遞至另一個步驟,並使用該地址指定電子郵件的收件者。
在兩個位置定義輸出變數:外掛程式的資訊清單檔案,以及使用會傳回輸出變數的函式。
以下範例會從三個輸入變數 (兩個數字和一個算術運算) 傳回計算出的數學結果。
在資訊清單檔案中定義輸出變數
在 Apps Script 資訊清單檔案中,指定 outputs[] 陣列和 onExecuteFunction()。
outputs[] 陣列中的每個項目都具有下列屬性:
id:輸出變數的專屬 ID。description:向使用者顯示的輸出變數說明。cardinality:允許的值數量。可能的值包括:"SINGLE":只能輸入一個值。
dataType:可接受的值類型。dataType具有basicType屬性,可定義資料類型。有效值包括:"STRING":英數字元字串。"INTEGER":數字。"TIMESTAMP":ISO 8601 格式的時間戳記。舉例來說,在 ISO 8601 中,2025 年 3 月 15 日會表示為 2025-03-15。"BOOLEAN":true 或 false。"EMAIL_ADDRESS":格式為dana@example.com的電子郵件地址。
以下範例會定義計算機步驟的輸出變數。 輸出變數為整數。
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"
}
}
]
}
}
}
在程式碼中定義輸出變數
步驟的程式碼包含名為 onExecuteCalculate() 的函式,也就是資訊清單中定義的 onExecuteFunction。這個函式會對使用者輸入的兩個值執行算術運算,並以名為 outputVariables() 的函式傳回結果做為輸出變數。
如要傳回輸出變數,請傳回符合下列規定的 JSON:
- 每個輸出變數的
variableId都必須與資訊清單檔案中對應輸出變數的id相符。 - 輸出變數的
variableData必須與資訊清單檔案中對應輸出變數的dataType和cardinality相符。
以下範例會傳回輸出變數,也就是兩個輸入數字的算術值:
Apps Script
/**
* 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 onExecuteCalculateFunction(event) {
console.log("output: " + JSON.stringify(event));
var calculatedValue = 0;
var value1 = event.workflow.actionInvocation.inputs["value1"];
var value2 = event.workflow.actionInvocation.inputs["value2"];
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;
}
var renderAction = {
"hostAppAction" : {
"workflowAction" : {
"returnOutputVariablesAction" : {
"variableValues" : [
{
"variableId": "result",
"integerValues": [
calculatedValue
]
}
]
}
}
}
};
console.log("renderAction: " + JSON.stringify(renderAction));
return renderAction;
}