Stay organized with collections
Save and categorize content based on your preferences.
Switch
A UI element that supports being toggled on or off. This can only be used within a DecoratedText widget.
Available for Google Workspace add-ons and Google Chat apps.
constswitchDecoratedText=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'),),);
Sets the key that identifies this switch in the event object that is generated when there is a
UI interaction. Not visible to the user. Required.
Unlike other form fields, this field name does not need to be unique. The form input values
for switches using the same field name are returned as an array. The array consists of the
values for all enabled switches with that field name.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-01-30 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,["Switch\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\nMethods\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`set``Control``Type(controlType)` \nSets the control type of the switch. Defaults to `SWITCH`.\n\nParameters\n\n| Name | Type | Description |\n|-----------------|------------------------------------------------------------------------------|--------------------------|\n| `control``Type` | [SwitchControlType](/apps-script/reference/card-service/switch-control-type) | The switch control type. |\n\nReturn\n\n\n[Switch](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n`set``Field``Name(fieldName)` \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\nParameters\n\n| Name | Type | Description |\n|---------------|----------|-----------------------------------------------|\n| `field``Name` | `String` | The key that is used to identify this switch. |\n\nReturn\n\n\n[Switch](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n`set``On``Change``Action(action)` \nSets the action to take when the switch is toggled.\n\nParameters\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\nReturn\n\n\n[Switch](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n`set``Selected(selected)` \nSets whether this switch should start as selected or unselected.\n\nParameters\n\n| Name | Type | Description |\n|------------|-----------|------------------------------------|\n| `selected` | `Boolean` | The starting switch state setting. |\n\nReturn\n\n\n[Switch](#) --- This object, for chaining.\n\n*** ** * ** ***\n\n`set``Value(value)` \nSets the value that is sent as the form input when this switch is toggled on.\n\nParameters\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\nReturn\n\n\n[Switch](#) --- This object, for chaining."]]