Class Switch
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
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
: 연결을 위한 객체입니다.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 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."]]