รวบรวมข้อมูลด้วยตัวแปรอินพุต

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

ขั้นตอนการเรียกใช้ต้องใช้ข้อมูลบางอย่าง เช่น การส่งอีเมลต้องใช้อีเมล หากต้องการระบุขั้นตอนข้อมูลที่จำเป็นนี้ ให้กำหนดตัวแปรอินพุต เมื่อกำหนดแล้ว โดยปกติผู้ใช้จะเป็นผู้ตั้งค่าตัวแปรอินพุตในการ์ดการกำหนดค่าของขั้นตอนขณะที่ผู้ใช้ตั้งค่าขั้นตอน

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

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

ในไฟล์ Manifest ให้ระบุตัวแปรอินพุตด้วยอาร์เรย์ inputs[] แต่ละรายการในอาร์เรย์ inputs[] มีพร็อพเพอร์ตี้ต่อไปนี้

  • 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

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

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"
          }
        }
      ]
    }
  }
}

กำหนดตัวแปรอินพุตในโค้ด

โค้ดของขั้นตอนมีฟังก์ชันที่ชื่อ onConfigFunction() ซึ่งแสดงผล การ์ดการกำหนดค่าที่กำหนดวิดเจ็ตการ์ดอินพุต 1 รายการสำหรับตัวแปรอินพุตแต่ละรายการ ที่กำหนดไว้ในอาร์เรย์ inputs[] ของไฟล์ Manifest

วิดเจ็ตอินพุตที่กำหนดไว้ในการ์ดการกำหนดค่ามีข้อกำหนดต่อไปนี้

  • name ของวิดเจ็ตอินพุตแต่ละรายการต้องตรงกับ id ของตัวแปรอินพุตที่เกี่ยวข้องในไฟล์ Manifest
  • คาร์ดินาลิตีของวิดเจ็ตอินพุตต้องตรงกับcardinalityของตัวแปรอินพุต ในไฟล์ Manifest
  • ประเภทข้อมูลของวิดเจ็ตอินพุตต้องตรงกับ dataType ของตัวแปรอินพุตในไฟล์ Manifest หากตัวแปรอินพุตมี dataType เป็นจำนวนเต็ม ตัวแปรนั้นจะ เก็บสตริงไม่ได้

หากต้องการความช่วยเหลือในการสร้างอินเทอร์เฟซของการ์ด โปรดดูตัวเลือกใดตัวเลือกหนึ่งต่อไปนี้

ตัวอย่างต่อไปนี้จะแสดงการ์ดการกำหนดค่าสำหรับวิดเจ็ตอินพุตแต่ละรายการที่กำหนดไว้ ในกำหนดตัวแปรอินพุตในไฟล์ Manifest

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 onConfigFunction() {
  var card = {
    "sections": [
      {
        "header": "Step example: Calculate",
        "widgets": [
          {
            "textInput": {
              "name": "value1", // "name" must match an "id" in the manifest file's inputs[] array.
              "label": "First value",
              "hostAppDataSource" : {
                "workflowDataSource" : {
                  "includeVariables" : true
                }
              }
            }
          },
          {
            "selectionInput": {
              "name": "operation", // "name" must match an "id" in the manifest file's inputs[] array.
              "label": "Operation",
              "type": "DROPDOWN",
              "items": [
                {
                  "text": "+",
                  "value": "+",
                },
                {
                  "text": "-",
                  "value": "-",
                },
                {
                  "text": "x",
                  "value": "x",
                },
                {
                  "text": "/",
                  "value": "/",
                }
              ]
            }
          },
          {
            "textInput": {
              "name": "value2", // "name" must match an "id" in the manifest file's inputs[] array.
              "label": "Second value",
              "hostAppDataSource" : {
                "workflowDataSource" : {
                  "includeVariables" : true
                }
              }
            }
          }
        ]
      }
    ]
  };
  return {
    "action": {
      "navigations": [{
        "push_card": card
      }]
    }
  };
}

ตรวจสอบตัวแปรอินพุต

แนวทางปฏิบัติแนะนำคือตรวจสอบว่าผู้ใช้ป้อนค่าที่เหมาะสม ดูตรวจสอบตัวแปรอินพุต