편집기 작업
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
작업 객체를 사용하여 Google Workspace 부가기능에 대화형 동작을 빌드합니다.
작업 객체는 사용자가 부가기능 UI에서 위젯(예: 버튼)과 상호작용할 때 발생하는 상황을 정의합니다.
위젯에 작업을 연결하려면 작업을 트리거하는 조건도 정의하는 위젯 핸들러 함수를 사용하세요. 트리거되면 작업은 지정된 콜백 함수를 실행합니다.
콜백 함수에는 사용자의 클라이언트 측 상호작용에 관한 정보를 전달하는 이벤트 객체가 전달됩니다. 콜백 함수를 구현하고 특정 응답 객체를 반환하도록 해야 합니다.
클릭하면 새 카드를 빌드하고 표시하는 버튼을 부가기능에 추가하려면 다음 단계를 따르세요.
- 버튼 위젯을 만듭니다.
- 카드 빌드 작업을 설정하려면 버튼 위젯 핸들러 함수
setOnClickAction(action)
를 추가합니다.
- 실행할 Apps Script 콜백 함수를 만들고 위젯 핸들러 함수 내에서
(action)
로 지정합니다. 이 경우 콜백 함수는 원하는 카드를 빌드하고 ActionResponse
객체를 반환해야 합니다. 응답 객체는 콜백 함수가 빌드한 카드를 표시하도록 부가기능에 지시합니다.
다음 예시에서는 버튼 위젯을 만드는 방법을 보여줍니다. 이 작업은 부가기능을 대신하여 현재 파일의 drive.file
범위를 요청합니다.
/**
* Adds a section to the Card Builder that displays a "REQUEST PERMISSION" button.
* When it's clicked, the callback triggers file scope permission flow. This is used in
* the add-on when the home-page displays basic data.
*/
function addRequestFileScopeButtonToBuilder(cardBuilder) {
var buttonSection = CardService.newCardSection();
// 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.
var buttonAction = CardService.newAction()
.setFunctionName("onRequestFileScopeButtonClickedInEditor");
var button = CardService.newTextButton()
.setText("Request permission")
.setBackgroundColor("#4285f4")
.setTextButtonStyle(CardService.TextButtonStyle.FILLED)
.setOnClickAction(buttonAction);
buttonSection.addWidget(button);
cardBuilder.addSection(buttonSection);
}
/**
* Callback function for a button action. Instructs Docs to display a
* permissions dialog to the user, requesting `drive.file` scope for the
* current file on behalf of this add-on.
*
* @param {Object} e The parameters object that contains the document’s ID
* @return {editorFileScopeActionResponse}
*/
function onRequestFileScopeButtonClickedInEditor(e) {
return CardService.newEditorFileScopeActionResponseBuilder()
.requestFileScopeForActiveDocument().build();
REST API의 파일 액세스 상호작용
편집기를 확장하고 REST API를 사용하는 Google Workspace 부가기능에는 파일 액세스를 요청하는 추가 위젯 작업이 포함될 수 있습니다. 이 작업을 수행하려면 연결된 작업 콜백 함수가 특수 응답 객체를 반환해야 합니다.
이 위젯 작업 및 응답 객체를 사용하려면 다음이 모두 충족되어야 합니다.
- 부가기능은 REST API를 사용합니다.
- 애드온은
CardService.newEditorFileScopeActionResponseBuilder().requestFileScopeForActiveDocument().build();
메서드를 사용하여 요청 파일 범위 대화상자를 표시합니다.
- 부가기능의 매니페스트에
https://www.googleapis.com/auth/drive.file
편집기 범위와 onFileScopeGranted
트리거가 포함되어 있습니다.
현재 문서의 파일 액세스 권한 요청
현재 문서에 대한 파일 액세스를 요청하려면 다음 단계를 따르세요.
- 부가기능에
drive.file
범위가 있는지 확인하는 홈페이지 카드를 빌드합니다.
drive.file
범위가 부여되지 않은 부가기능의 경우 사용자가 현재 문서에 drive.file
범위를 부여하도록 요청하는 방법을 빌드합니다.
예: Google Docs에서 현재 문서 액세스 권한 가져오기
다음 예에서는 현재 문서의 크기를 표시하는 Google Docs 인터페이스를 빌드합니다. 애드온에 drive.file
승인이 없으면 파일 범위 승인을 시작하는 버튼이 표시됩니다.
/**
* 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 information about the
* current document.
* @return {Card}
*/
function onDocsHomepage(e) {
return createAddOnView(e);
}
function onFileScopeGranted(e) {
return createAddOnView(e);
}
/**
* For the current document, display either its quota information or
* a button that allows the user to provide permission to access that
* file to retrieve its quota details.
*
* @param e The event containing information about the current document
* @return {Card}
*/
function createAddOnView(e) {
var docsEventObject = e['docs'];
var builder = CardService.newCardBuilder();
var cardSection = CardService.newCardSection();
if (docsEventObject['addonHasFileScopePermission']) {
cardSection.setHeader(docsEventObject['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 the add-on has access permission, read and display its quota.
cardSection.addWidget(
CardService.newTextParagraph().setText(
"This file takes up: " + getQuotaBytesUsed(docsEventObject['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");
var button = CardService.newTextButton()
.setText("Request permission")
.setOnClickAction(buttonAction);
cardSection.addWidget(button);
}
return builder.addSection(cardSection).build();
}
/**
* Callback function for a button action. Instructs Docs to display a
* permissions dialog to the user, requesting `drive.file` scope for the
* current file on behalf of this add-on.
*
* @param {Object} e The parameters object that contains the document’s ID
* @return {editorFileScopeActionResponse}
*/
function onRequestFileScopeButtonClicked(e) {
return CardService.newEditorFileScopeActionResponseBuilder()
.requestFileScopeForActiveDocument().build();
}
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-07-25(UTC)
[null,null,["최종 업데이트: 2025-07-25(UTC)"],[[["\u003cp\u003eGoogle Workspace add-ons use Action objects to define interactive behavior triggered by user interactions with UI widgets.\u003c/p\u003e\n"],["\u003cp\u003eAn action's callback function executes when triggered and receives an event object containing interaction details, requiring a specific response object to be returned.\u003c/p\u003e\n"],["\u003cp\u003eAdd-ons extending Editors and using REST APIs can request file access using a widget action with a specialized response object returned by its callback function.\u003c/p\u003e\n"],["\u003cp\u003eRequesting file access involves building a homepage card to check for \u003ccode\u003edrive.file\u003c/code\u003e scope and providing a way for users to grant this scope if needed.\u003c/p\u003e\n"]]],["Action objects enable interactive behavior in Google Workspace add-ons by defining responses to user interactions with widgets. To implement actions, you must define a widget handler function that triggers a callback function upon a specific condition. This callback function then receives an event object with client-side interaction data and must return a specific response object. For file access, add-ons use a specialized response object, `EditorFileScopeActionResponse`, and need `drive.file` scope in the manifest.\n"],null,["# Editor actions\n\nUse [Action](/workspace/add-ons/concepts/actions) objects to build interactive\nbehavior into Google Workspace add-ons.\n\nAction objects define what happens when a user interacts with a widget\n(for example, a button) in the add-on UI.\n\nAdd an action to a widget\n-------------------------\n\nTo attach an action to a widget, use a [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 [callback function](/workspace/add-ons/concepts/actions#callback_functions).\nThe callback function is passed an [event object](/workspace/add-ons/concepts/event-objects)\nthat carries information about the user's client-side interactions. You must\nimplement the callback function and have it return a specific response object.\n\n### Example: Display a new card when a button is clicked\n\nIf you want to add a button to your add-on that builds and displays a new card\nwhen clicked, follow the steps below:\n\n1. Create a button [widget](/workspace/add-ons/concepts/widgets#user_interaction_widgets).\n2. To set a card-building action, add the button widget handler function [`setOnClickAction(action)`](/apps-script/reference/card-service/text-button#setOnClickAction(Action)).\n3. Create an Apps Script callback function to execute and specify it as the [`(action)`](/apps-script/reference/card-service/text-button#setOnClickAction(Action)) within the widget handler function. In this case, the callback function should build the card you want and return an [`ActionResponse`](/apps-script/reference/card-service/action-response) object. The response object tells the add-on to display the card the callback function built.\n\nThe following example shows the creation of a button widget. The action requests\nthe `drive.file` scope for the current file on behalf of the add-on. \n\n```gdscript\n/**\n * Adds a section to the Card Builder that displays a \"REQUEST PERMISSION\" button.\n * When it's clicked, the callback triggers file scope permission flow. This is used in\n * the add-on when the home-page displays basic data.\n */\nfunction addRequestFileScopeButtonToBuilder(cardBuilder) {\n var buttonSection = CardService.newCardSection();\n // If the add-on does not have access permission, add a button that\n // allows the user to provide that permission on a per-file basis.\n var buttonAction = CardService.newAction()\n .setFunctionName(\"onRequestFileScopeButtonClickedInEditor\");\n\n var button = CardService.newTextButton()\n .setText(\"Request permission\")\n .setBackgroundColor(\"#4285f4\")\n .setTextButtonStyle(CardService.TextButtonStyle.FILLED)\n .setOnClickAction(buttonAction);\n\n buttonSection.addWidget(button);\n cardBuilder.addSection(buttonSection);\n}\n\n/**\n * Callback function for a button action. Instructs Docs to display a\n * permissions dialog to the user, requesting `drive.file` scope for the \n * current file on behalf of this add-on.\n *\n * @param {Object} e The parameters object that contains the document's ID\n * @return {editorFileScopeActionResponse}\n */\nfunction onRequestFileScopeButtonClickedInEditor(e) {\n return CardService.newEditorFileScopeActionResponseBuilder()\n .requestFileScopeForActiveDocument().build();\n```\n\nFile access interactions for REST APIs\n--------------------------------------\n\nGoogle Workspace add-ons that extend the Editors and use REST APIs can include an\nadditional widget action to request file access. This action requires the\nassociated action callback function to return a specialized response object:\n\n| Action attempted | Callback function should return |\n|---------------------------------------------------------------------------------------|---------------------------------|\n| [Request file access for current_document](#request_file_access_for_current_document) | `EditorFileScopeActionResponse` |\n\nTo make use of this widget action and response object, all of the following must\nbe true:\n\n- The add-on uses REST APIs.\n- The add-on presents the request file scope dialog using the `CardService.newEditorFileScopeActionResponseBuilder().requestFileScopeForActiveDocument().build();` method.\n- The add-on includes the `https://www.googleapis.com/auth/drive.file` editor scope and `onFileScopeGranted` trigger in its manifest.\n\n### Request file access for current document\n\nTo request file access for the current document, follow these steps:\n\n1. Build a homepage card that checks whether the add-on has `drive.file` scope.\n2. For cases where the add-on hasn't been granted `drive.file` scope, build a way to request that users grant `drive.file` scope for the current document.\n\n#### Example: Get current document access in Google Docs\n\nThe following example builds an interface for Google Docs that displays the size\nof the current document. If the add-on doesn't have `drive.file` authorization,\nit displays a button to initiate the file scope authorization. \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 information about the\n * current document.\n * @return {Card}\n */\n function onDocsHomepage(e) {\n return createAddOnView(e);\n }\n\n function onFileScopeGranted(e) {\n return createAddOnView(e);\n }\n\n /**\n * For the current document, display either its quota information or\n * a button that allows the user to provide permission to access that\n * file to retrieve its quota details.\n *\n * @param e The event containing information about the current document\n * @return {Card}\n */\n function createAddOnView(e) {\n var docsEventObject = e['docs'];\n var builder = CardService.newCardBuilder();\n\n var cardSection = CardService.newCardSection();\n if (docsEventObject['addonHasFileScopePermission']) {\n cardSection.setHeader(docsEventObject['title']);\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 the add-on has access permission, read and display its quota.\n cardSection.addWidget(\n CardService.newTextParagraph().setText(\n \"This file takes up: \" + getQuotaBytesUsed(docsEventObject['id'])));\n } else {\n // If the add-on does not have access permission, add a button that\n // allows the user to provide that permission on a per-file 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\n var button = CardService.newTextButton()\n .setText(\"Request permission\")\n .setOnClickAction(buttonAction);\n\n cardSection.addWidget(button);\n }\n return builder.addSection(cardSection).build();\n }\n\n /**\n * Callback function for a button action. Instructs Docs to display a\n * permissions dialog to the user, requesting `drive.file` scope for the\n * current file on behalf of this add-on.\n *\n * @param {Object} e The parameters object that contains the document's ID\n * @return {editorFileScopeActionResponse}\n */\n function onRequestFileScopeButtonClicked(e) {\n return CardService.newEditorFileScopeActionResponseBuilder()\n .requestFileScopeForActiveDocument().build();\n }"]]