Class Switch
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เปลี่ยน
องค์ประกอบ UI ที่รองรับการเปิดหรือปิด ใช้ได้เฉพาะภายในวิดเจ็ต DecoratedText
พร้อมใช้งานสำหรับส่วนเสริมของ Google Workspace และแอป Google Chat
const switchDecoratedText =
CardService.newDecoratedText()
.setTopLabel('Switch decorated text widget label')
.setText('This is a decorated text widget with a switch on the right')
.setWrapText(true)
.setSwitchControl(
CardService.newSwitch()
.setFieldName('form_input_switch_key')
.setValue('form_input_switch_value')
.setOnChangeAction(
CardService.newAction().setFunctionName(
'handleSwitchChange'),
),
);
เอกสารประกอบโดยละเอียด
setControlType(controlType)
ตั้งค่าประเภทการควบคุมของสวิตช์ ค่าเริ่มต้นคือ SWITCH
พารามิเตอร์
รีเทิร์น
Switch
— ออบเจ็กต์นี้สําหรับการต่อเชื่อม
setFieldName(fieldName)
ตั้งค่าคีย์ที่ระบุสวิตช์นี้ในออบเจ็กต์เหตุการณ์ซึ่งสร้างขึ้นเมื่อมีการทำงานกับ UI ผู้ใช้จะมองไม่เห็นข้อมูลนี้ ต้องระบุ
ชื่อฟิลด์นี้ไม่จำเป็นต้องไม่ซ้ำกัน ต่างจากฟิลด์อื่นๆ ของแบบฟอร์ม ระบบจะแสดงผลค่าอินพุตของแบบฟอร์มสําหรับสวิตช์ที่ใช้ชื่อช่องเดียวกันเป็นอาร์เรย์ อาร์เรย์ประกอบด้วยค่าของสวิตช์ที่เปิดใช้ทั้งหมดที่มีชื่อช่องนั้น
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
fieldName | String | คีย์ที่ใช้ระบุสวิตช์นี้ |
รีเทิร์น
Switch
— ออบเจ็กต์นี้สําหรับการต่อเชื่อม
setOnChangeAction(action)
ตั้งค่าการดำเนินการที่จะทำเมื่อสลับสวิตช์
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
action | Action | การดำเนินการที่จะทำเมื่อสลับสวิตช์ |
รีเทิร์น
Switch
— ออบเจ็กต์นี้สําหรับการต่อเชื่อม
setSelected(selected)
ตั้งค่าว่าสวิตช์นี้ควรเริ่มต้นเป็นเลือกไว้หรือไม่ได้เลือกไว้
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
selected | Boolean | การตั้งค่าสถานะสวิตช์เริ่มต้น |
รีเทิร์น
Switch
— ออบเจ็กต์นี้สําหรับการต่อเชื่อม
setValue(value)
ตั้งค่าที่จะส่งเป็นอินพุตแบบฟอร์มเมื่อเปิดสวิตช์นี้
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
value | String | ค่าที่เชื่อมโยงกับชื่อเมื่อสวิตช์เปิดอยู่ เมื่อส่งข้อมูลนี้ไปยังการเรียกกลับของแบบฟอร์ม ระบบจะแสดงเป็นสตริงเสมอ |
รีเทิร์น
Switch
— ออบเจ็กต์นี้สําหรับการต่อเชื่อม
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eThe Switch UI element allows users to toggle between on and off states within a DecoratedText widget.\u003c/p\u003e\n"],["\u003cp\u003eIt's used in Google Workspace Add-ons and Chat apps to collect user input via form submissions.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can customize the switch's behavior, including its initial state, the value submitted, and actions triggered upon toggling.\u003c/p\u003e\n"],["\u003cp\u003eEach switch is identified by a field name, and switches with the same field name submit their values as an array.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003esetOnChangeAction\u003c/code\u003e method allows developers to specify a function to execute when the switch's state changes, enabling dynamic interactions.\u003c/p\u003e\n"]]],[],null,["# Class Switch\n\nSwitch\n\nA UI element that supports being toggled on or off. This can only be used within a [DecoratedText](/apps-script/reference/card-service/decorated-text) widget.\n\nAvailable for Google Workspace add-ons and Google Chat apps.\n\n```javascript\nconst switchDecoratedText =\n CardService.newDecoratedText()\n .setTopLabel('Switch decorated text widget label')\n .setText('This is a decorated text widget with a switch on the right')\n .setWrapText(true)\n .setSwitchControl(\n CardService.newSwitch()\n .setFieldName('form_input_switch_key')\n .setValue('form_input_switch_value')\n .setOnChangeAction(\n CardService.newAction().setFunctionName(\n 'handleSwitchChange'),\n ),\n );\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|-------------------------------------------------------------------|-------------|----------------------------------------------------------------------------------------------------------------|\n| [setControlType(controlType)](#setControlType(SwitchControlType)) | [Switch](#) | Sets the control type of the switch. |\n| [setFieldName(fieldName)](#setFieldName(String)) | [Switch](#) | Sets the key that identifies this switch in the event object that is generated when there is a UI interaction. |\n| [setOnChangeAction(action)](#setOnChangeAction(Action)) | [Switch](#) | Sets the action to take when the switch is toggled. |\n| [setSelected(selected)](#setSelected(Boolean)) | [Switch](#) | Sets whether this switch should start as selected or unselected. |\n| [setValue(value)](#setValue(String)) | [Switch](#) | Sets the value that is sent as the form input when this switch is toggled on. |\n\nDetailed documentation\n----------------------\n\n### `set``Control``Type(controlType)`\n\nSets the control type of the switch. Defaults to `SWITCH`.\n\n#### Parameters\n\n| Name | Type | Description |\n|-----------------|------------------------------------------------------------------------------|--------------------------|\n| `control``Type` | [SwitchControlType](/apps-script/reference/card-service/switch-control-type) | The switch control type. |\n\n#### Return\n\n\n[Switch](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n### `set``Field``Name(fieldName)`\n\nSets the key that identifies this switch in the event object that is generated when there is a\nUI interaction. Not visible to the user. Required.\n\nUnlike other form fields, this field name does not need to be unique. The form input values\nfor switches using the same field name are returned as an array. The array consists of the\nvalues for all enabled switches with that field name.\n\n#### Parameters\n\n| Name | Type | Description |\n|---------------|----------|-----------------------------------------------|\n| `field``Name` | `String` | The key that is used to identify this switch. |\n\n#### Return\n\n\n[Switch](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n### `set``On``Change``Action(action)`\n\nSets the action to take when the switch is toggled.\n\n#### Parameters\n\n| Name | Type | Description |\n|----------|------------------------------------------------------|------------------------------------------------|\n| `action` | [Action](/apps-script/reference/card-service/action) | The action to take when the switch is toggled. |\n\n#### Return\n\n\n[Switch](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n### `set``Selected(selected)`\n\nSets whether this switch should start as selected or unselected.\n\n#### Parameters\n\n| Name | Type | Description |\n|------------|-----------|------------------------------------|\n| `selected` | `Boolean` | The starting switch state setting. |\n\n#### Return\n\n\n[Switch](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n### `set``Value(value)`\n\nSets the value that is sent as the form input when this switch is toggled on.\n\n#### Parameters\n\n| Name | Type | Description |\n|---------|----------|-----------------------------------------------------------------------------------------------------------------------------------------|\n| `value` | `String` | The value associated with the name when the switch is on. When this is sent to the form callback, it is always represented as a string. |\n\n#### Return\n\n\n[Switch](#) --- This object, for chaining."]]