Class Switch
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
开关
支持开启或关闭的界面元素。此元素只能在 DecoratedText
widget 中使用。
适用于 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)
在发生界面互动时生成的事件对象中设置用于标识此开关的键。对用户不可见。必填。
与其他表单字段不同,此字段名称不必是唯一的。使用相同字段名称的开关的表单输入值会作为数组返回。该数组包含具有该字段名称的所有已启用开关的值。
参数
名称 | 类型 | 说明 |
fieldName | String | 用于标识此开关的键。 |
返回
Switch
- 此对象,用于链式调用。
setOnChangeAction(action)
设置在切换开关时要执行的操作。
参数
名称 | 类型 | 说明 |
action | Action | 切换开关时要执行的操作。 |
返回
Switch
- 此对象,用于链式调用。
setSelected(selected)
设置此开关在开始时应处于选中还是未选中状态。
参数
名称 | 类型 | 说明 |
selected | Boolean | 开关的起始状态设置。 |
返回
Switch
- 此对象,用于链式调用。
setValue(value)
设置在此开关处于开启状态时作为表单输入发送的值。
参数
名称 | 类型 | 说明 |
value | String | 开启开关时与名称关联的值。当此值发送到表单回调时,它始终表示为字符串。 |
返回
Switch
- 此对象,用于链式调用。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\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."]]