액션 독려
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
Action
객체를 사용하면 Google Workspace 부가기능에 대화형 동작을 빌드할 수 있습니다. 이러한 핸들러는 사용자가 부가기능 UI에서 위젯 (예: 버튼)과 상호작용할 때 발생하는 작업을 정의합니다.
작업은 위젯 핸들러 함수를 사용하여 지정된 위젯에 연결되며, 이 함수는 작업을 트리거하는 조건도 정의합니다. 트리거되면 작업이 지정된 콜백 함수를 실행합니다.
콜백 함수에는 사용자의 클라이언트 측 상호작용에 관한 정보를 전달하는 이벤트 객체가 전달됩니다. 콜백 함수를 구현하고 특정 응답 객체를 반환하도록 해야 합니다.
예를 들어 클릭하면 새 카드를 빌드하고 표시하는 버튼을 원한다고 가정해 보겠습니다. 이를 위해 새 버튼 위젯을 만들고 버튼 위젯 핸들러 함수 setOnClickAction(action)
를 사용하여 카드 빌드 Action
를 설정해야 합니다. 정의한 Action
은 버튼을 클릭할 때 실행되는 Apps Script 콜백 함수를 지정합니다. 이 경우 콜백 함수를 구현하여 원하는 카드를 빌드하고 ActionResponse
객체를 반환합니다. 응답 객체는 콜백 함수가 빌드한 카드를 표시하도록 부가기능에 지시합니다.
이 페이지에서는 부가기능에 포함할 수 있는 Drive 전용 위젯 작업을 설명합니다.
상호작용 유도
Drive를 확장하는 Google Workspace 부가기능에는 추가 Drive 전용 위젯 작업이 포함될 수 있습니다. 이 작업을 수행하려면 연결된 작업 콜백 함수가 특수 응답 객체를 반환해야 합니다.
이러한 위젯 작업과 응답 객체를 사용하려면 다음이 모두 충족되어야 합니다.
- 사용자가 하나 이상의 Drive 항목을 선택한 상태에서 작업이 트리거됩니다.
- 부가기능의 매니페스트에
https://www.googleapis.com/auth/drive.file
드라이브 범위가 포함되어 있습니다.
선택한 파일에 대한 파일 액세스 권한 요청
다음 예시에서는 사용자가 하나 이상의 Drive 항목을 선택할 때 트리거되는 Google Drive의 컨텍스트 인터페이스를 빌드하는 방법을 보여줍니다. 이 예에서는 각 항목을 테스트하여 부가기능에 액세스 권한이 부여되었는지 확인합니다. 부여되지 않은 경우 DriveItemsSelectedActionResponse
객체를 사용하여 사용자에게 권한을 요청합니다. 항목에 권한이 부여되면 부가기능에 해당 항목의 Drive 할당량 사용량이 표시됩니다.
/**
* Build a simple card that checks selected items' quota usage. Checking
* quota usage requires user-permissions, so this add-on provides a button
* to request `drive.file` scope for items the add-on doesn't yet have
* permission to access.
*
* @param e The event object passed containing contextual information about
* the Drive items selected.
* @return {Card}
*/
function onDriveItemsSelected(e) {
var builder = CardService.newCardBuilder();
// For each item the user has selected in Drive, display either its
// quota information or a button that allows the user to provide
// permission to access that file to retrieve its quota details.
e['drive']['selectedItems'].forEach(
function(item){
var cardSection = CardService.newCardSection()
.setHeader(item['title']);
// This add-on uses the recommended, limited-permission `drive.file`
// scope to get granular per-file access permissions.
// See: https://developers.google.com/drive/api/v2/about-auth
if (item['addonHasFileScopePermission']) {
// If the add-on has access permission, read and display its
// quota.
cardSection.addWidget(
CardService.newTextParagraph().setText(
"This file takes up: " + getQuotaBytesUsed(item['id'])));
} else {
// If the add-on does not have access permission, add a button
// that allows the user to provide that permission on a per-file
// basis.
cardSection.addWidget(
CardService.newTextParagraph().setText(
"The add-on needs permission to access this file's quota."));
var buttonAction = CardService.newAction()
.setFunctionName("onRequestFileScopeButtonClicked")
.setParameters({id: item.id});
var button = CardService.newTextButton()
.setText("Request permission")
.setOnClickAction(buttonAction);
cardSection.addWidget(button);
}
builder.addSection(cardSection);
});
return builder.build();
}
/**
* Callback function for a button action. Instructs Drive to display a
* permissions dialog to the user, requesting `drive.file` scope for a
* specific item on behalf of this add-on.
*
* @param {Object} e The parameters object that contains the item's
* Drive ID.
* @return {DriveItemsSelectedActionResponse}
*/
function onRequestFileScopeButtonClicked (e) {
var idToRequest = e.parameters.id;
return CardService.newDriveItemsSelectedActionResponseBuilder()
.requestFileScope(idToRequest).build();
}
/**
* Use the Advanced Drive Service
* (See https://developers.google.com/apps-script/advanced/drive),
* with `drive.file` scope permissions to request the quota usage of a
* specific Drive item.
*
* @param {string} itemId The ID of the item to check.
* @return {string} A description of the item's quota usage, in bytes.
*/
function getQuotaBytesUsed(itemId) {
try {
return Drive.Files.get(itemId,{fields: "quotaBytesUsed"})
.quotaBytesUsed + " bytes";
} catch (e) {
return "Error fetching how much quota this item uses. Error: " + e;
}
}
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-08-01(UTC)
[null,null,["최종 업데이트: 2025-08-01(UTC)"],[[["\u003cp\u003eActions enable interactive behavior in Google Workspace add-ons, defining responses to user interactions with widgets.\u003c/p\u003e\n"],["\u003cp\u003eDrive add-ons use a specialized DriveItemsSelectedActionResponse to manage file access permissions.\u003c/p\u003e\n"],["\u003cp\u003eAdd-ons need the \u003ccode\u003ehttps://www.googleapis.com/auth/drive.file\u003c/code\u003e scope to request and utilize Drive file access.\u003c/p\u003e\n"],["\u003cp\u003eThe provided example demonstrates building a contextual Drive interface, requesting file access, and displaying quota usage upon permission grant.\u003c/p\u003e\n"]]],["Actions in Google Workspace add-ons define interactive behaviors triggered by widget interactions. Widgets use handler functions to attach actions, executing callback functions upon user interaction. These callbacks receive an event object containing interaction details and must return a specific response. Drive-specific actions allow requesting file access for selected files. The callback, triggered when a user selects a file in drive, checks if access is granted, and, if not, prompts the user for permission using `DriveItemsSelectedActionResponse`. The add-on then displays file quota information if access is granted.\n"],null,["# Drive actions\n\n[`Action`](/workspace/add-ons/concepts/actions) objects let you build interactive\nbehavior into Google Workspace add-ons. They define\nwhat happens when a user interacts with a widget (for example, a button) in\nthe add-on UI.\n\nAn action is attached to a given widget using a\n[widget handler function](/workspace/add-ons/concepts/actions#widget_handler_functions),\nwhich also defines the condition that triggers the action. When triggered, the\naction executes a designated\n[callback function](/workspace/add-ons/concepts/actions#callback_functions).\nThe callback function is passed an\n[event object](/workspace/add-ons/concepts/event-objects) that carries\ninformation about the user's client-side interactions. You must implement the\ncallback function and have it return a specific response object.\n\nFor example, say you want a button that builds and displays a new card when\nclicked. For this, you must create a new button widget and use the button widget\nhandler function\n[`setOnClickAction(action)`](/apps-script/reference/card-service/text-button#setOnClickAction(Action))\nto set a card-building [`Action`](/workspace/add-ons/concepts/actions). The\n[`Action`](/workspace/add-ons/concepts/actions) you define specifies an Apps Script\ncallback function that executes when the button is clicked. In this case, you\nimplement the callback function to build the card you want and return an\n[`ActionResponse`](/apps-script/reference/card-service/action-response)\nobject. The response object tells the add-on to display the card the callback\nfunction built.\n\nThis page describes Drive-specific widget actions you can include in your\nadd-on.\n\nDrive interactions\n------------------\n\nGoogle Workspace add-ons that extend Drive can include\nan additional Drive-specific widget action. This action requires the associated\naction [callback function](/workspace/add-ons/concepts/actions#callback_functions)\nto return a specialized response object:\n\n| Action attempted | Callback function should return |\n|-----------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|\n| [Request file access for selected files](#request_file_access_for_selected_files) | [`DriveItemsSelectedActionResponse`](/apps-script/reference/card-service/drive-items-selected-action-response) |\n\nTo make use of these widget actions and response objects, all of the following\nmust be true:\n\n- The action is triggered while the user has one or more Drive items selected.\n- The add-on includes the `https://www.googleapis.com/auth/drive.file` [Drive scope](/workspace/add-ons/concepts/workspace-scopes#drive_scopes) in its manifest.\n\n### Request file access for selected files\n\nThe following example shows how to build a contextual interface for Google\nDrive that is triggered when the user selects one or more Drive items. The\nexample tests each item to see if the add-on has been granted access permission;\nif not, it uses a [`DriveItemsSelectedActionResponse`](/apps-script/reference/card-service/drive-items-selected-action-response)\nobject to request that permission from the user. Once permission is granted for\nan item, the add-on displays the Drive quota usage of that item. \n\n /**\n * Build a simple card that checks selected items' quota usage. Checking\n * quota usage requires user-permissions, so this add-on provides a button\n * to request `drive.file` scope for items the add-on doesn't yet have\n * permission to access.\n *\n * @param e The event object passed containing contextual information about\n * the Drive items selected.\n * @return {Card}\n */\n function onDriveItemsSelected(e) {\n var builder = CardService.newCardBuilder();\n\n // For each item the user has selected in Drive, display either its\n // quota information or a button that allows the user to provide\n // permission to access that file to retrieve its quota details.\n e['drive']['selectedItems'].forEach(\n function(item){\n var cardSection = CardService.newCardSection()\n .setHeader(item['title']);\n\n // This add-on uses the recommended, limited-permission `drive.file`\n // scope to get granular per-file access permissions.\n // See: https://developers.google.com/drive/api/v2/about-auth\n if (item['addonHasFileScopePermission']) {\n // If the add-on has access permission, read and display its\n // quota.\n cardSection.addWidget(\n CardService.newTextParagraph().setText(\n \"This file takes up: \" + getQuotaBytesUsed(item['id'])));\n } else {\n // If the add-on does not have access permission, add a button\n // that allows the user to provide that permission on a per-file\n // basis.\n cardSection.addWidget(\n CardService.newTextParagraph().setText(\n \"The add-on needs permission to access this file's quota.\"));\n\n var buttonAction = CardService.newAction()\n .setFunctionName(\"onRequestFileScopeButtonClicked\")\n .setParameters({id: item.id});\n\n var button = CardService.newTextButton()\n .setText(\"Request permission\")\n .setOnClickAction(buttonAction);\n\n cardSection.addWidget(button);\n }\n\n builder.addSection(cardSection);\n });\n\n return builder.build();\n }\n\n /**\n * Callback function for a button action. Instructs Drive to display a\n * permissions dialog to the user, requesting `drive.file` scope for a\n * specific item on behalf of this add-on.\n *\n * @param {Object} e The parameters object that contains the item's\n * Drive ID.\n * @return {DriveItemsSelectedActionResponse}\n */\n function onRequestFileScopeButtonClicked (e) {\n var idToRequest = e.parameters.id;\n return CardService.newDriveItemsSelectedActionResponseBuilder()\n .requestFileScope(idToRequest).build();\n }\n\n /**\n * Use the Advanced Drive Service\n * (See https://developers.google.com/apps-script/advanced/drive),\n * with `drive.file` scope permissions to request the quota usage of a\n * specific Drive item.\n *\n * @param {string} itemId The ID of the item to check.\n * @return {string} A description of the item's quota usage, in bytes.\n */\n function getQuotaBytesUsed(itemId) {\n try {\n return Drive.Files.get(itemId,{fields: \"quotaBytesUsed\"})\n .quotaBytesUsed + \" bytes\";\n } catch (e) {\n return \"Error fetching how much quota this item uses. Error: \" + e;\n }\n }"]]