使用输出变量在步骤之间传递数据

本指南介绍了如何创建输出变量。

输出变量由步骤返回,可以发送到另一个步骤。 例如,将电子邮件地址传递给另一个步骤,该步骤会使用该地址来指定电子邮件的收件人。

在两个位置定义输出变量:插件的清单文件和代码中(使用返回输出变量的函数)。

以下示例根据三个输入变量(两个数字和一个算术运算)返回计算出的数学结果。

在清单文件中定义输出变量

在 Apps 脚本清单文件中,指定 outputs[] 数组和 onExecuteFunction()

outputs[] 数组中的每个项都具有以下属性:

  • 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 必须与清单文件中相应输出变量的 dataTypecardinality 相匹配。

以下示例返回一个输出变量,该变量是两个输入数字的算术值:

Apps 脚本

/**
 * 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;
}