Class Ui

界面

一种 Google 应用的界面环境实例,允许脚本添加菜单、对话框和边栏等功能。脚本只能与已打开编辑器的当前实例的界面进行交互,且仅当脚本绑定到容器时才会与编辑器交互。

// 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.
var ui = SpreadsheetApp.getUi();
var response = 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());
} else if (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.');
}

属性

属性类型说明
ButtonButton表示由提醒PromptResponse.getSelectedButton() 返回的预定本地化对话框对话框的枚举,用于指示用户点击了对话框中的哪个按钮。
ButtonSetButtonSet表示一个或多个可添加到提醒提示的对话框按钮的已预先确定本地化的枚举。

方法

方法返回类型简介
alert(prompt)Button使用给定消息和一个“OK”按钮,在用户的编辑器中打开一个对话框。
alert(prompt, buttons)Button使用给定的消息和一组按钮在用户的编辑器中打开一个对话框。
alert(title, prompt, buttons)Button使用指定的标题、消息和一组按钮,在用户的编辑器中打开一个对话框。
createAddonMenu()Menu创建构建器,可用于将子菜单插入编辑器的插件菜单中。
createMenu(caption)Menu创建可用于向编辑器界面添加菜单的构建器。
prompt(prompt)PromptResponse使用给定消息和一个“确定”按钮,在用户的编辑器中打开一个输入对话框。
prompt(prompt, buttons)PromptResponse使用给定消息和一组按钮在用户的编辑器中打开一个输入对话框。
prompt(title, prompt, buttons)PromptResponse使用指定的标题、消息和一组按钮在用户的编辑器中打开一个输入对话框。
showModalDialog(userInterface, title)void在用户编辑器中打开包含自定义客户端内容的模态对话框。
showModelessDialog(userInterface, title)void使用自定义客户端内容在用户的编辑器中打开一个无模式对话框。
showSidebar(userInterface)void打开包含自定义客户端内容的编辑器的边栏。

详细文档

alert(prompt)

使用给定消息和一个“OK”按钮,在用户的编辑器中打开一个对话框。在对话框打开时,此方法会暂停服务器端脚本。用户关闭对话框后,脚本会继续运行,但 Jdbc 连接和 LockService 锁定在帐号暂停期间不会持续显示。如需了解详情,请参阅对话框和边栏指南

// 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');

参数

名称类型说明
promptString要在对话框中显示的消息。

返回

Button - 用户点击的按钮。


alert(prompt, buttons)

使用给定的消息和一组按钮在用户的编辑器中打开一个对话框。在对话框打开时,此方法会暂停服务器端脚本。用户关闭对话框后,脚本会继续运行,但 Jdbc 连接和 LockService 锁定在帐号暂停期间不会持续显示。如需了解详情,请参阅对话框和边栏指南

// 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.
var ui = SpreadsheetApp.getUi();
var response = 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.');
}

参数

名称类型说明
promptString要在对话框中显示的消息。
buttonsButtonSet设置为显示在对话框中的按钮。

返回

Button - 用户点击的按钮。


alert(title, prompt, buttons)

使用指定的标题、消息和一组按钮,在用户的编辑器中打开一个对话框。该方法会在对话框打开时挂起服务器端脚本。用户关闭对话框后,脚本会继续运行,但 Jdbc 连接和 LockService 锁定在暂停期间不会持续显示。如需了解详情,请参阅对话框和边栏指南

// 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.
var ui = SpreadsheetApp.getUi();
var response = 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.');
}

参数

名称类型说明
titleString对话框上方显示的标题。
promptString要在对话框中显示的消息。
buttonsButtonSet设置为显示在对话框中的按钮。

返回

Button - 用户点击的按钮。


createAddonMenu()

创建构建器,可用于将子菜单插入编辑器的插件菜单中。在调用 Menu.addToUi() 之前,菜单实际上不会更新。如果脚本作为插件运行,则子菜单名称与网上应用店中的插件名称匹配;如果脚本直接绑定到文档,则子菜单名称与脚本的名称匹配。如需了解详情,请参阅菜单指南

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

返回

Menu - 新的菜单构建器。


createMenu(caption)

创建可用于向编辑器界面添加菜单的构建器。实际上,在调用 Menu.addToUi() 之前才会添加菜单。如需了解详情,请参阅菜单指南。顶级菜单的标签应采用首字母大写形式(所有主要字词均采用首字母大写形式),但子菜单的标签应采用句首字母大写形式(只有第一个单词采用首字母大写形式)。如果脚本以插件的形式发布,则系统会忽略 caption 参数,并将菜单添加为插件菜单的子菜单(等同于 createAddonMenu())。

// Add a custom menu to the active document, including a separator and a sub-menu.
function onOpen(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();
}

参数

名称类型说明
captionString菜单的标签,所有顶级单词均大写以用于顶级菜单,或仅将第一个单词的首字母大写。

返回

Menu - 新的菜单构建器。


prompt(prompt)

使用给定消息和一个“确定”按钮,在用户的编辑器中打开一个输入对话框。该方法会在对话框打开时挂起服务器端脚本。用户关闭对话框后,脚本会继续运行,但 Jdbc 连接和 LockService 锁定在暂停期间不会持续显示。如需了解详情,请参阅对话框和边栏指南

// 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.
var ui = SpreadsheetApp.getUi();
var response = 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.');
}

参数

名称类型说明
promptString要在对话框中显示的消息。

返回

PromptResponse - 表示用户的响应。


prompt(prompt, buttons)

使用给定消息和一组按钮在用户的编辑器中打开一个输入对话框。该方法会在对话框打开时挂起服务器端脚本。用户关闭对话框后,脚本会继续运行,但 Jdbc 连接和 LockService 锁定在暂停期间不会持续显示。如需了解详情,请参阅对话框和边栏指南

// 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.
var ui = SpreadsheetApp.getUi();
var response = 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());
} else if (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.');
}

参数

名称类型说明
promptString要在对话框中显示的消息。
buttonsButtonSet设置为显示在对话框中的按钮。

返回

PromptResponse - 表示用户的响应。


prompt(title, prompt, buttons)

使用指定的标题、消息和一组按钮在用户的编辑器中打开一个输入对话框。在对话框打开时,此方法会挂起服务器端脚本。用户关闭对话框后,脚本会继续运行,但 Jdbc 连接和 LockService 锁定在挂起状态下不会保留。如需了解详情,请参阅对话框和边栏指南

// 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.
var ui = SpreadsheetApp.getUi();
var response = 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());
} else if (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.');
}

参数

名称类型说明
titleString对话框上方显示的标题。
promptString要在对话框中显示的消息。
buttonsButtonSet设置为显示在对话框中的按钮。

返回

PromptResponse - 表示用户的响应。


showModalDialog(userInterface, title)

在用户编辑器中打开包含自定义客户端内容的模态对话框。在对话框打开时,此方法不会暂停服务器端脚本。为了与服务器端脚本进行通信,客户端组件必须使用 google.script API 对 HtmlService 进行异步回调。如需以编程方式关闭对话框,请在 HtmlService Web 应用的客户端上调用 google.script.host.close()。如需了解详情,请参阅对话框和边栏指南

模态对话框会阻止用户与该对话框以外的任何内容互动。相比之下,非模式对话框边栏可让用户与编辑器进行交互。几乎在所有情况下,模态对话框或边栏都比无模式对话框更好。

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

参数

名称类型说明
userInterfaceObject表示要显示的接口的 HtmlOutput
titleString对话框的标题;通过对 userInterface 对象调用 setTitle() 替换设置的任何标题。

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

showModelessDialog(userInterface, title)

使用自定义客户端内容在用户的编辑器中打开一个无模式对话框。对话框打开时,不会暂停服务器端脚本。为了与服务器端脚本通信,客户端组件必须使用 google.script API 对 HtmlService 进行异步回调。如需以编程方式关闭对话框,请在 HtmlService Web 应用的客户端上调用 google.script.host.close()。如需了解详情,请参阅对话框和边栏指南

无模式对话框可让用户与对话框后面的编辑器互动。相反,模态对话框则不会。几乎在所有情况下,模态对话框或边栏都是比无模式对话框更好的选择。

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

参数

名称类型说明
userInterfaceObject表示要显示的接口的 HtmlOutput
titleString对话框的标题;通过对 userInterface 对象调用 setTitle() 替换设置的任何标题。

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

showSidebar(userInterface)

打开包含自定义客户端内容的编辑器的边栏。在边栏打开时,此方法不会暂停服务器端脚本。为了与服务器端脚本进行通信,客户端组件必须使用 google.script API 对 HtmlService 进行异步回调。如需以编程方式关闭边栏,请在 HtmlService Web 应用的客户端上调用 google.script.host.close()。如需了解详情,请参阅对话框和边栏指南

如果用户的环境使用从左到右的语言,则在编辑器的左侧;对于从右到左的语言,边栏会显示在编辑器的左侧。脚本显示的所有边栏宽度都是 300 像素。

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

参数

名称类型说明
userInterfaceObject表示要显示的接口的 HtmlOutput

授权

使用此方法的脚本需要通过以下一个或多个范围进行授权:

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

已废弃的方法