Class Switch

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.

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'),
                    ),
        );

Methods

MethodReturn typeBrief description
setControlType(controlType)SwitchSets the control type of the switch.
setFieldName(fieldName)SwitchSets the key that identifies this switch in the event object that is generated when there is a UI interaction.
setOnChangeAction(action)SwitchSets the action to take when the switch is toggled.
setSelected(selected)SwitchSets whether this switch should start as selected or unselected.
setValue(value)SwitchSets the value that is sent as the form input when this switch is toggled on.

Detailed documentation

setControlType(controlType)

Sets the control type of the switch. Defaults to SWITCH.

Parameters

NameTypeDescription
controlTypeSwitchControlTypeThe switch control type.

Return

Switch — This object, for chaining.


setFieldName(fieldName)

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.

Parameters

NameTypeDescription
fieldNameStringThe key that is used to identify this switch.

Return

Switch — This object, for chaining.


setOnChangeAction(action)

Sets the action to take when the switch is toggled.

Parameters

NameTypeDescription
actionActionThe action to take when the switch is toggled.

Return

Switch — This object, for chaining.


setSelected(selected)

Sets whether this switch should start as selected or unselected.

Parameters

NameTypeDescription
selectedBooleanThe starting switch state setting.

Return

Switch — This object, for chaining.


setValue(value)

Sets the value that is sent as the form input when this switch is toggled on.

Parameters

NameTypeDescription
valueStringThe 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.

Return

Switch — This object, for chaining.