Class Switch
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Switch
Một phần tử giao diện người dùng hỗ trợ bật hoặc tắt. Bạn chỉ có thể sử dụng phần tử này trong tiện ích DecoratedText
.
Có sẵn cho các tiện ích bổ sung của Google Workspace và ứng dụng 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'),
),
);
Tài liệu chi tiết
setControlType(controlType)
Đặt loại điều khiển của nút chuyển. Giá trị mặc định là SWITCH
.
Thông số
Cầu thủ trả bóng
Switch
– Đối tượng này, để tạo chuỗi.
setFieldName(fieldName)
Đặt khoá xác định nút chuyển này trong đối tượng sự kiện được tạo khi có hoạt động tương tác với giao diện người dùng. Người dùng không nhìn thấy. Bắt buộc.
Không giống như các trường biểu mẫu khác, tên trường này không cần phải là tên duy nhất. Các giá trị nhập của biểu mẫu cho các nút chuyển sử dụng cùng một tên trường sẽ được trả về dưới dạng một mảng. Mảng này bao gồm các giá trị cho tất cả các nút chuyển đã bật có tên trường đó.
Thông số
Tên | Loại | Mô tả |
fieldName | String | Khoá dùng để xác định nút chuyển này. |
Cầu thủ trả bóng
Switch
– Đối tượng này, để tạo chuỗi.
setOnChangeAction(action)
Đặt hành động cần thực hiện khi bật/tắt nút chuyển.
Thông số
Tên | Loại | Mô tả |
action | Action | Hành động cần thực hiện khi bật/tắt nút chuyển. |
Cầu thủ trả bóng
Switch
– Đối tượng này, để tạo chuỗi.
setSelected(selected)
Đặt trạng thái ban đầu của nút chuyển này là đã chọn hay chưa chọn.
Thông số
Tên | Loại | Mô tả |
selected | Boolean | Chế độ cài đặt trạng thái nút chuyển bắt đầu. |
Cầu thủ trả bóng
Switch
– Đối tượng này, để tạo chuỗi.
setValue(value)
Đặt giá trị được gửi dưới dạng dữ liệu đầu vào của biểu mẫu khi nút chuyển này được bật.
Thông số
Tên | Loại | Mô tả |
value | String | Giá trị được liên kết với tên khi nút chuyển bật. Khi được gửi đến lệnh gọi lại biểu mẫu, giá trị này luôn được biểu thị dưới dạng một chuỗi. |
Cầu thủ trả bóng
Switch
– Đối tượng này, để tạo chuỗi.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 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."]]