編輯器外掛程式的對話方塊和側欄
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
對大多數 Editor 外掛程式而言,對話方塊和側邊欄面板是主要的外掛程式使用者介面。
兩者都可使用標準 HTML 和 CSS 完全自訂,而且您可以使用 Apps Script 的用戶端/伺服器通訊模型,在使用者與側欄或對話方塊互動時執行 Apps Script 函式。外掛程式可以定義多個側欄和對話方塊,但一次只能顯示一個。
如要禁止使用者與編輯器互動,直到他們在外掛程式介面中做出選擇為止,請使用對話方塊;否則請使用側欄。
對話方塊
對話方塊是覆蓋主要編輯器內容的視窗面板。Apps Script 對話方塊是強制回應模式,開啟後使用者就無法與編輯器介面的其他元素互動。您可以自訂對話方塊的內容和大小。
您建立外掛程式對話方塊的方式,與 Apps Script 的自訂對話方塊相同;一般建議程序如下:
- 建立指令碼專案檔案,定義對話方塊的 HTML 結構、CSS 和用戶端 JavaScript 行為。定義對話方塊時,請參閱編輯器外掛程式樣式規範。
- 在要開啟對話方塊的伺服器端程式碼中,呼叫
HtmlService.createHtmlOutputFromFile(filename)
,建立代表對話方塊的 HtmlOutput
物件。或者,如果您使用範本化 HTML,可以呼叫 HtmlService.createTemplateFromFile(filename)
生成範本,然後呼叫 HtmlTemplate.evaluate()
將其轉換為 HtmlOutput
物件。
- 呼叫
Ui.showModalDialog(htmlOutput, dialogTitle)
,使用該 HtmlOutput
顯示對話方塊。
對話方塊開啟時,不會暫停伺服器端指令碼。用戶端 JavaScript 可以使用 google.script.run()
和相關聯的處理常式函式,對伺服器端進行非同步呼叫。詳情請參閱「用戶端與伺服器之間的通訊」。
檔案開啟對話方塊
檔案開啟對話方塊是預先建構的對話方塊,可讓使用者從 Google 雲端硬碟選取檔案。您可以在外掛程式中新增檔案開啟對話方塊,不必自行設計,但需要進行一些額外設定。您也必須存取外掛程式的 Cloud Platform 專案,才能啟用 Google Picker API。
詳情請參閱「檔案開啟對話方塊」。
側欄是顯示在編輯器介面右側的面板,也是最常見的附加元件介面類型。與對話方塊不同,側欄開啟時,您可以繼續與編輯器介面的其他元素互動。側欄的寬度固定,但你可以自訂內容。
建構外掛程式側欄的方式與 Apps Script 的
自訂側欄相同;一般建議程序如下:
- 建立指令碼專案檔案,定義側欄的 HTML 結構、CSS 和用戶端 JavaScript 行為。定義側邊欄時,請參閱編輯器外掛程式樣式指南。
在要開啟側欄的伺服器端程式碼中,呼叫 HtmlService.createHtmlOutputFromFile(filename)
,建立代表側欄的 HtmlOutput
物件。或者,如果您使用範本化 HTML,可以呼叫 HtmlService.createTemplateFromFile(filename)
生成範本,然後呼叫 HtmlTemplate.evaluate()
將其轉換為 HtmlOutput
物件。
呼叫 Ui.showSidebar(htmlOutput)
,即可使用該 HtmlOutput
顯示側欄。
側欄開啟時,不會暫停伺服器端指令碼。用戶端 JavaScript 可以使用 google.script.run()
和相關聯的處理常式函式,對伺服器端進行非同步呼叫。詳情請參閱「用戶端與伺服器之間的通訊」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間:2025-08-21 (世界標準時間)。"],[[["\u003cp\u003eEditor add-ons primarily use customizable dialog windows and sidebar panels for user interfaces, built with HTML, CSS, and Apps Script.\u003c/p\u003e\n"],["\u003cp\u003eDialogs are modal windows, overlaying editor content and preventing interaction until a choice is made, while sidebars allow continued editor interaction.\u003c/p\u003e\n"],["\u003cp\u003eBoth dialogs and sidebars are created using HtmlService to define their structure and displayed using Ui methods.\u003c/p\u003e\n"],["\u003cp\u003eFile-open dialogs are pre-built for selecting files from Google Drive but need extra configuration and Cloud Platform project access.\u003c/p\u003e\n"],["\u003cp\u003eAdd-on sidebars have a fixed width of 300 pixels and appear on the right of the editor, enabling user interaction with other editor elements.\u003c/p\u003e\n"]]],["Editor add-ons utilize customizable dialogs and sidebars as user interfaces, built with HTML, CSS, and Apps Script. Dialogs, which overlay the editor, prevent user interaction until closed, created via `HtmlService` and displayed with `Ui.showModalDialog`. Sidebars, residing on the editor's right, allow continued editor interaction and are created and displayed similarly, using `Ui.showSidebar`. Both interfaces facilitate client-server communication through `google.script.run()` for asynchronous interactions. File-open dialogs offer pre-built file selection.\n"],null,["# Dialogs and sidebars for Editor add-on\n\nFor most [Editor add-on](/workspace/add-ons/concepts/types#editor_add-ons),\ndialog windows and sidebar panels are the primary add-on user interfaces.\nBoth are fully customizable using standard HTML and CSS, and you can use\nApps Script's\n[client-server communication model](/apps-script/guides/html/communication)\nto run Apps Script functions when the user interacts with the sidebar or dialog.\nYour add-on can define multiple sidebars and dialogs, but the add-on can display\nonly one at a time.\n\nWhen you want to prevent the user from interacting with the editor until they\nmake some choice in the add-on interface, use a dialog; otherwise use a\nsidebar.\n\nDialogs\n-------\n\n*Dialogs* are window panels that overlay the primary editor content. Apps Script\ndialogs are modal; while they are opened the user can't interact with the\nother elements of the editor interface. You can customize the content and size\nof dialogs.\n\nYou build add-on dialogs the same way as Apps Script\n[custom dialogs](/apps-script/guides/dialogs#custom_dialogs); the general\nrecommended procedure is the following:\n\n1. Create a script project file that defines your dialog's HTML structure, CSS, and client-side JavaScript behavior. When defining the dialog, refer to the [Editor add-on style guidelines](/workspace/add-ons/guides/editor-style#dialogs).\n2. In your server-side code where you want the dialog to open, call [`HtmlService.createHtmlOutputFromFile(filename)`](/apps-script/reference/html/html-service#createhtmloutputfromfilefilename) to create an [`HtmlOutput`](/apps-script/reference/html/html-output) object representing the dialog. Alternatively, if you are using [templated HTML](/apps-script/guides/html/templates) you can call [`HtmlService.createTemplateFromFile(filename)`](/apps-script/reference/html/html-service#createtemplatefromfilefilename) to generate a template and then [`HtmlTemplate.evaluate()`](/apps-script/reference/html/html-template#evaluate()) to convert it to an [`HtmlOutput`](/apps-script/reference/html/html-output) object.\n3. Call [`Ui.showModalDialog(htmlOutput, dialogTitle)`](/apps-script/reference/base/ui#showModalDialog(Object,String)) to display the dialog using that [`HtmlOutput`](/apps-script/reference/html/html-output).\n\nDialogs don't suspend the server-side script while they are open. The\nclient-side JavaScript can make asynchronous calls to the server-side\nusing [`google.script.run()`](/apps-script/guides/html/reference/run) and\nassociated handler functions. For more details, see\n[Client-to-server communication](/apps-script/guides/html/communication).\n\n### File-open dialogs\n\n*File-open dialogs* are pre-built dialogs that let your users select files\nfrom their Google Drive. You can add a file-open dialog to your add-on without\nneeding to design it, but it requires some additional configuration. You also\nrequire access to the add-on's\n[Cloud Platform project](/apps-script/guides/cloud-platform-projects)\nin order to enable the Google Picker API.\n\nFor full details, see [File-open dialogs](/apps-script/guides/dialogs#file-open_dialogs).\n\nSidebars\n--------\n\n*Sidebars* are panels that appear in the right of the editor interface, and\nare the most common type of add-on interface. Unlike dialogs, you can continue\nto interact with the other elements of the editor interface while a sidebar is\nopen. Sidebars have a fixed width, but you can customize their content.\n\nYou build add-on sidebars the same way as Apps Script\n[custom sidebars](/apps-script/guides/dialogs#custom_sidebars); the general\nrecommended procedure is the following:\n\n1. Create a script project file that defines your sidebar's HTML structure, CSS, and client-side JavaScript behavior. When defining the sidebar, refer to the [Editor add-on style guidelines](/workspace/add-ons/guides/editor-style#sidebars).\n2. In your server-side code where you want the sidebar to open, call\n [`HtmlService.createHtmlOutputFromFile(filename)`](/apps-script/reference/html/html-service#createhtmloutputfromfilefilename)\n to create an [`HtmlOutput`](/apps-script/reference/html/html-output)\n object representing the sidebar. Alternatively, if you are using\n [templated HTML](/apps-script/guides/html/templates) you can call\n [`HtmlService.createTemplateFromFile(filename)`](/apps-script/reference/html/html-service#createtemplatefromfilefilename)\n to generate a template and then\n [`HtmlTemplate.evaluate()`](/apps-script/reference/html/html-template#evaluate())\n to convert it to an\n [`HtmlOutput`](/apps-script/reference/html/html-output) object.\n\n | **Note:** Add-on sidebars have a fixed width of 300 pixels that you can't alter by calling [`HtmlOutput.setWidth(width)`](/apps-script/reference/html/html-output#setwidthwidth).\n3. Call [`Ui.showSidebar(htmlOutput)`](/apps-script/reference/base/ui#showSidebar(Object))\n to display the sidebar using that\n [`HtmlOutput`](/apps-script/reference/html/html-output).\n\nSidebars don't suspend the server-side script while they are open. The\nclient-side JavaScript can make asynchronous calls to the server-side\nusing [`google.script.run()`](/apps-script/guides/html/reference/run) and\nassociated handler functions. For more details, see\n[Client-to-server communication](/apps-script/guides/html/communication)."]]