Class Ui

  • The Ui object allows scripts to add UI elements like menus, dialogs, and sidebars to a Google App's user interface.

  • Scripts can only interact with the UI of the currently open editor if they are container-bound to that editor.

  • The Ui object provides methods to create alert boxes, input prompts, custom menus, modal and modeless dialogs, and sidebars.

  • Alert and prompt dialogs suspend the server-side script while open, while modal dialogs, modeless dialogs, and sidebars do not.

  • Buttons and ButtonSet are properties of the Ui object used to define and handle button responses in dialogs.

Ui

An instance of the user-interface environment for a Google App that allows the script to addfeatures like menus, dialogs, and sidebars. A script can only interact with the UI for thecurrent instance of an open editor, and only if the script iscontainer-bound to the editor.

// Display a dialog box with a title, message, input field, and "Yes" and "No"// buttons. The user can also close the dialog by clicking the close button in// its title bar.constui=SpreadsheetApp.getUi();constresponse=ui.prompt('Getting to know you','May I know your name?',ui.ButtonSet.YES_NO,);// Process the user's response.if(response.getSelectedButton()===ui.Button.YES){Logger.log('The user\'s name is %s.',response.getResponseText());}elseif(response.getSelectedButton()===ui.Button.NO){Logger.log('The user didn\'t want to provide a name.');}else{Logger.log('The user clicked the close button in the dialog\'s title bar.');}

Properties

PropertyTypeDescription
ButtonButtonAn enum representing predetermined, localized dialog buttons returned by analert orPromptResponse.getSelectedButton() to indicatewhich button in a dialog the user clicked.
ButtonSetButtonSetAn enum representing predetermined, localized sets of one or more dialog buttons that can beadded to analert or aprompt.

Methods

MethodReturn typeBrief description
alert(prompt)ButtonOpens a dialog box in the user's editor with the given message and an "OK" button.
alert(prompt, buttons)ButtonOpens a dialog box in the user's editor with the given message and set of buttons.
alert(title, prompt, buttons)ButtonOpens a dialog box in the user's editor with the given title, message, and set of buttons.
createAddonMenu()MenuCreates a builder that can be used to insert a sub-menu into the editor's Extensions menu.
createMenu(caption)MenuCreates a builder that can be used to add a menu to the editor's user interface.
prompt(prompt)PromptResponseOpens an input dialog box in the user's editor with the given message and an "OK" button.
prompt(prompt, buttons)PromptResponseOpens an input dialog box in the user's editor with the given message and set of buttons.
prompt(title, prompt, buttons)PromptResponseOpens an input dialog box in the user's editor with the given title, message, and set ofbuttons.
showModalDialog(userInterface, title)voidOpens a modal dialog box in the user's editor with custom client-side content.
showModelessDialog(userInterface, title)voidOpens a modeless dialog box in the user's editor with custom client-side content.
showSidebar(userInterface)voidOpens a sidebar in the user's editor with custom client-side content.

Deprecated methods

MethodReturn typeBrief description
showDialog(userInterface)voidOpens a dialog box in the user's editor with custom client-side content.

Detailed documentation

alert(prompt)

Opens a dialog box in the user's editor with the given message and an "OK" button. This methodsuspends the server-side script while the dialog is open. The script resumes after the userdismisses the dialog, butJdbcconnections andLockService locks don'tpersist across the suspension. For more information, see theguide to dialogs and sidebars.

// Display "Hello, world" in a dialog box with an "OK" button. The user can also// close the dialog by clicking the close button in its title bar.SpreadsheetApp.getUi().alert('Hello, world');

Parameters

NameTypeDescription
promptStringThe message to display in the dialog box.

Return

Button — The button the user clicked.


alert(prompt, buttons)

Opens a dialog box in the user's editor with the given message and set of buttons. This methodsuspends the server-side script while the dialog is open. The script resumes after the userdismisses the dialog, butJdbcconnections andLockService locks don'tpersist across the suspension. For more information, see theguide to dialogs and sidebars.

// Display a dialog box with a message and "Yes" and "No" buttons. The user can// also close the dialog by clicking the close button in its title bar.constui=SpreadsheetApp.getUi();constresponse=ui.alert('Are you sure you want to continue?',ui.ButtonSet.YES_NO,);// Process the user's response.if(response===ui.Button.YES){Logger.log('The user clicked "Yes."');}else{Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.',);}

Parameters

NameTypeDescription
promptStringThe message to display in the dialog box.
buttonsButtonSetThe button set to display in the dialog box.

Return

Button — The button the user clicked.


alert(title, prompt, buttons)

Opens a dialog box in the user's editor with the given title, message, and set of buttons. Thismethod suspends the server-side script while the dialog is open. The script resumes after theuser dismisses the dialog, butJdbcconnections andLockService locks don'tpersist across the suspension. For more information, see theguide to dialogs and sidebars.

// Display a dialog box with a title, message, and "Yes" and "No" buttons. The// user can also close the dialog by clicking the close button in its title bar.constui=SpreadsheetApp.getUi();constresponse=ui.alert('Confirm','Are you sure you want to continue?',ui.ButtonSet.YES_NO,);// Process the user's response.if(response===ui.Button.YES){Logger.log('The user clicked "Yes."');}else{Logger.log('The user clicked "No" or the close button in the dialog\'s title bar.',);}

Parameters

NameTypeDescription
titleStringThe title to display above the dialog box.
promptStringThe message to display in the dialog box.
buttonsButtonSetThe button set to display in the dialog box.

Return

Button — The button the user clicked.


createAddonMenu()

Creates a builder that can be used to insert a sub-menu into the editor's Extensions menu. Themenu isn't actually be updated untilMenu.addToUi() is called. If the script is runningas an add-on, the sub-menu name matches the add-on's name in the web store; if the script isbound to the document directly, the sub-menu namematches the script's name. For more information, see theguide to menus.

// Add an item to the add-on menu, under a sub-menu whose name is set// automatically.functiononOpen(e){SpreadsheetApp.getUi().createAddonMenu().addItem('Show','showSidebar').addToUi();}

Return

Menu — The new menu builder.


createMenu(caption)

Creates a builder that can be used to add a menu to the editor's user interface. The menu isn'tactually be added untilMenu.addToUi() is called. For more information, see theguide to menus. The label for a top-level menu should bein headline case (all major words capitalized), although the label for a sub-menu should be insentence case (only the first word capitalized). If the script is published as anadd-on, thecaption parameter is ignored and themenu is added as a sub-menu of the Extensions menu, equivalent tocreateAddonMenu().

// Add a custom menu to the active document, including a separator and a// sub-menu.functiononOpen(e){SpreadsheetApp.getUi().createMenu('My Menu').addItem('My menu item','myFunction').addSeparator().addSubMenu(SpreadsheetApp.getUi().createMenu('My sub-menu').addItem('One sub-menu item','mySecondFunction').addItem('Another sub-menu item','myThirdFunction'),).addToUi();}

Parameters

NameTypeDescription
captionStringThe label for the menu, with all major words capitalized for a top-level menu, or only the first word capitalized for a sub-menu.

Return

Menu — The new menu builder.


prompt(prompt)

Opens an input dialog box in the user's editor with the given message and an "OK" button. Thismethod suspends the server-side script while the dialog is open. The script resumes after theuser dismisses the dialog, butJdbcconnections andLockService locks don'tpersist across the suspension. For more information, see theguide to dialogs and sidebars.

// Display a dialog box with a message, input field, and an "OK" button. The// user can also close the dialog by clicking the close button in its title bar.constui=SpreadsheetApp.getUi();constresponse=ui.prompt('Enter your name:');// Process the user's response.if(response.getSelectedButton()===ui.Button.OK){Logger.log('The user\'s name is %s.',response.getResponseText());}else{Logger.log('The user clicked the close button in the dialog\'s title bar.');}

Parameters

NameTypeDescription
promptStringThe message to display in the dialog box.

Return

PromptResponse — A representation of the user's response.


prompt(prompt, buttons)

Opens an input dialog box in the user's editor with the given message and set of buttons. Thismethod suspends the server-side script while the dialog is open. The script resumes after theuser dismisses the dialog, butJdbcconnections andLockService locks don'tpersist across the suspension. For more information, see theguide to dialogs and sidebars.

// Display a dialog box with a message, input field, and "Yes" and "No" buttons.// The user can also close the dialog by clicking the close button in its title// bar.constui=SpreadsheetApp.getUi();constresponse=ui.prompt('May I know your name?',ui.ButtonSet.YES_NO);// Process the user's response.if(response.getSelectedButton()===ui.Button.YES){Logger.log('The user\'s name is %s.',response.getResponseText());}elseif(response.getSelectedButton()===ui.Button.NO){Logger.log('The user didn\'t want to provide a name.');}else{Logger.log('The user clicked the close button in the dialog\'s title bar.');}

Parameters

NameTypeDescription
promptStringThe message to display in the dialog box.
buttonsButtonSetThe button set to display in the dialog box.

Return

PromptResponse — A representation of the user's response.


prompt(title, prompt, buttons)

Opens an input dialog box in the user's editor with the given title, message, and set ofbuttons. This method suspends the server-side script while the dialog is open. The scriptresumes after the user dismisses the dialog, butJdbc connections andLockService locks don't persist across thesuspension. For more information, see theguide todialogs and sidebars.

// Display a dialog box with a title, message, input field, and "Yes" and "No"// buttons. The user can also close the dialog by clicking the close button in// its title bar.constui=SpreadsheetApp.getUi();constresponse=ui.prompt('Getting to know you','May I know your name?',ui.ButtonSet.YES_NO,);// Process the user's response.if(response.getSelectedButton()===ui.Button.YES){Logger.log('The user\'s name is %s.',response.getResponseText());}elseif(response.getSelectedButton()===ui.Button.NO){Logger.log('The user didn\'t want to provide a name.');}else{Logger.log('The user clicked the close button in the dialog\'s title bar.');}

Parameters

NameTypeDescription
titleStringThe title to display above the dialog box.
promptStringThe message to display in the dialog box.
buttonsButtonSetThe button set to display in the dialog box.

Return

PromptResponse — A representation of the user's response.


showModalDialog(userInterface, title)

Opens a modal dialog box in the user's editor with custom client-side content. This method doesnot suspend the server-side script while the dialog is open. To communicate with theserver-side script, the client-side component must make asynchronous callbacks using thegoogle.script API forHtmlService. To close the dialogprogrammatically, callgoogle.script.host.close() on the client side of anHtmlService webapp. For more information, see theguide to dialogs andsidebars.

Modal dialogs prevent the user from interacting with anything other than the dialog. Bycontrast,modeless dialogs andsidebars let the user interact with the editor. In almost all cases, amodal dialog or sidebar is a better choice than a modeless dialog.

// Display a modal dialog box with custom HtmlService content.consthtmlOutput=HtmlService.createHtmlOutput('<p>A change of speed, a change of style...</p>',).setWidth(250).setHeight(300);SpreadsheetApp.getUi().showModalDialog(htmlOutput,'My add-on');

Parameters

NameTypeDescription
userInterfaceObjectAnHtmlOutput representing the interface to display.
titleStringThe title of the dialog; overrides any title set by callingsetTitle() on theuserInterface object.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/script.container.ui

showModelessDialog(userInterface, title)

Opens a modeless dialog box in the user's editor with custom client-side content. This methoddoesnot suspend the server-side script while the dialog is open. To communicate withthe server-side script, the client-side component must make asynchronous callbacks using thegoogle.script API forHtmlService. To close the dialogprogrammatically, callgoogle.script.host.close() on the client side of anHtmlService webapp. For more information, see theguide to dialogs andsidebars.

Modeless dialogs let the user interact with the editor behind the dialog. By contrast,modal dialogs do not. In almost all cases, a modaldialog orsidebar is a better choice than a modeless dialog.

// Display a modeless dialog box with custom HtmlService content.consthtmlOutput=HtmlService.createHtmlOutput('<p>A change of speed, a change of style...</p>',).setWidth(250).setHeight(300);SpreadsheetApp.getUi().showModelessDialog(htmlOutput,'My add-on');

Parameters

NameTypeDescription
userInterfaceObjectAnHtmlOutput representing the interface to display.
titleStringThe title of the dialog; overrides any title set by callingsetTitle() on theuserInterface object.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/script.container.ui

showSidebar(userInterface)

Opens a sidebar in the user's editor with custom client-side content. This method doesnot suspend the server-side script while the sidebar is open. To communicate with theserver-side script, the client-side component must make asynchronous callbacks using thegoogle.script API forHtmlService. To close the sidebarprogrammatically, callgoogle.script.host.close() on the client side of anHtmlService webapp. For more information, see theguide to dialogs andsidebars.

The sidebar displays on the right side of the editor for users whose environments use aleft-to-right language and on the left side of the editor for right-to-left languages. Allsidebars shown by scripts are 300 pixels wide.

// Display a sidebar with custom HtmlService content.consthtmlOutput=HtmlService.createHtmlOutput('<p>A change of speed, a change of style...</p>',).setTitle('My add-on');SpreadsheetApp.getUi().showSidebar(htmlOutput);

Parameters

NameTypeDescription
userInterfaceObjectAnHtmlOutput representing the interface to display.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/script.container.ui

Deprecated methods

showDialog(userInterface)

Deprecated. As of March 2014, this method is deprecated. The direct replacement isshowModelessDialog(userInterface, title), butshowModalDialog(userInterface, title) is a better choice in almost all cases.

Opens a dialog box in the user's editor with custom client-side content. This method doesnot suspend the server-side script while the dialog is open. To communicate with theserver-side script, the client-side component must make asynchronous callbacks using thegoogle.script API forHtmlService. To close the dialogprogrammatically, callgoogle.script.host.close() on the client side of anHtmlService webapp. For more information, see theguide to dialogs andsidebars.

// Display a dialog box with custom HtmlService content.consthtmlOutput=HtmlService.createHtmlOutput('<p>A change of speed, a change of style...</p>',).setTitle('My add-on').setWidth(250).setHeight(300);SpreadsheetApp.getUi().showDialog(htmlOutput);

Parameters

NameTypeDescription
userInterfaceObjectAnHtmlOutput representing the interface to display.

Authorization

Scripts that use this method require authorization with one or more of the followingscopes:

  • https://www.googleapis.com/auth/script.container.ui

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.