ส่งข้อมูลระหว่างขั้นตอนด้วยตัวแปรเอาต์พุต

คู่มือนี้อธิบายวิธีสร้างตัวแปรเอาต์พุต

ขั้นตอนต่างๆ จะส่งคืนตัวแปรเอาต์พุตและส่งไปยังขั้นตอนอื่นได้ เช่น ส่งอีเมลไปยังขั้นตอนอื่นซึ่งใช้อีเมลนั้นเพื่อระบุ ผู้รับอีเมล

กำหนดตัวแปรเอาต์พุตใน 2 ที่ ได้แก่ ไฟล์ Manifest ของส่วนเสริม และในโค้ดที่มีฟังก์ชันที่ส่งคืนตัวแปรเอาต์พุต

ตัวอย่างต่อไปนี้แสดงผลลัพธ์ทางคณิตศาสตร์ที่คำนวณจากตัวแปรอินพุต 3 รายการ ได้แก่ ตัวเลข 2 รายการและการดำเนินการทางคณิตศาสตร์

กำหนดตัวแปรเอาต์พุตในไฟล์ Manifest

ในไฟล์ Manifest ของ Apps Script ให้ระบุ outputs[] array และ onExecuteFunction()

แต่ละรายการในอาร์เรย์ outputs[] มีพร็อพเพอร์ตี้ต่อไปนี้

  • id: ตัวระบุที่ไม่ซ้ำกันสำหรับตัวแปรเอาต์พุต
  • description: คำอธิบายตัวแปรเอาต์พุตที่จะแสดงต่อผู้ใช้ปลายทาง
  • cardinality: จำนวนค่าที่อนุญาต ค่าที่เป็นไปได้มีดังนี้
    • "SINGLE": อนุญาตให้มีเพียงค่าเดียว
  • dataType: ประเภทของค่าที่ยอมรับ dataType มีพร็อพเพอร์ตี้ basicType ซึ่งกำหนดประเภทของข้อมูล ค่าที่ใช้ได้มีดังนี้
    • "STRING": สตริงที่เป็นตัวเลขและตัวอักษร
    • "INTEGER": ตัวเลข
    • "TIMESTAMP": การประทับเวลาในรูปแบบ ISO 8601 เช่น ใน ISO 8601 วันที่ 15 มีนาคม 2025 จะแสดงเป็น 2025-03-15
    • "BOOLEAN": จริงหรือเท็จ
    • "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 ที่กำหนดไว้ในไฟล์ Manifest โดยจะดำเนินการทางคณิตศาสตร์กับค่า 2 ค่าที่ผู้ใช้ป้อน และแสดงผลลัพธ์เป็นตัวแปรเอาต์พุตที่มีฟังก์ชันชื่อ outputVariables()

หากต้องการแสดงตัวแปรเอาต์พุต ให้แสดง JSON ที่มีข้อกำหนดต่อไปนี้

  • variableId ของตัวแปรเอาต์พุตแต่ละรายการต้องตรงกับ id ของตัวแปรเอาต์พุตที่เกี่ยวข้องในไฟล์ Manifest
  • variableData ของตัวแปรเอาต์พุตต้องตรงกับ dataType และ cardinality ของตัวแปรเอาต์พุตที่สอดคล้องกันในไฟล์ Manifest

ตัวอย่างต่อไปนี้จะแสดงตัวแปรเอาต์พุตซึ่งเป็นค่าทางคณิตศาสตร์ ของตัวเลขอินพุต 2 ตัว

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