Class Ui

UI

メニュー、ダイアログ、サイドバーなどの機能をスクリプトで追加できるようにする、Google アプリのユーザー インターフェース環境のインスタンス。スクリプトは、開いているエディタの現在のインスタンスの UI とやり取りできます。ただし、スクリプトがエディタにコンテナ バインドされている場合に限ります。

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

Properties

プロパティ種類説明
ButtonButton事前定義済みのローカライズされたダイアログ ボタンを表す列挙型。アラートまたは PromptResponse.getSelectedButton() によって返されます。ユーザーがダイアログでクリックしたボタンを示します。
ButtonSetButtonSet事前定義されたローカライズされた 1 つ以上のダイアログ ボタンのセットを表す列挙型で、アラートまたはプロンプトに追加できます。

Methods

方法戻り値の型概要
alert(prompt)Buttonユーザーのエディタにダイアログ ボックスが開き、指定したメッセージと [OK] ボタンが表示されます。
alert(prompt, buttons)Buttonユーザーのエディタで、指定されたメッセージとボタンのセットを含むダイアログ ボックスを開きます。
alert(title, prompt, buttons)Buttonユーザーのエディタで、指定されたタイトル、メッセージ、ボタンセットを含むダイアログ ボックスを開きます。
createAddonMenu()Menuエディタのアドオン メニューにサブメニューを挿入するために使用できるビルダーを作成します。
createMenu(caption)Menuエディタのユーザー インターフェースにメニューを追加するために使用できるビルダーを作成します。
prompt(prompt)PromptResponseユーザーのエディタで入力ダイアログ ボックスを開き、所定のメッセージと [OK] ボタンを表示します。
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)

ユーザーのエディタで入力ダイアログ ボックスを開き、所定のメッセージと [OK] ボタンを表示します。このメソッドは、ダイアログが開いている間にサーバー側のスクリプトを停止します。ユーザーがダイアログを閉じるとスクリプトが再開されますが、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)

ユーザーのエディタでモーダル ダイアログ ボックスを開き、クライアントサイドのカスタム コンテンツを表示します。この方法では、ダイアログが開いている間はサーバー側スクリプトが停止されません。サーバー側のスクリプトと通信するために、クライアント側コンポーネントは、HtmlService 用の google.script API を使用して非同期コールバックを行う必要があります。プログラムでダイアログを閉じるには、HtmlService ウェブアプリのクライアント側で 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() を呼び出すことで設定されたタイトルをオーバーライドします。

認可

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

showModelessDialog(userInterface, title)

ユーザーのエディタにモードレスのダイアログ ボックスが開き、クライアントサイドのカスタム コンテンツが表示されます。この方法では、ダイアログが開いている間はサーバー側スクリプトが停止されません。サーバー側のスクリプトと通信するために、クライアント側コンポーネントは、HtmlService 用の google.script API を使用して非同期コールバックを行う必要があります。プログラムでダイアログを閉じるには、HtmlService ウェブアプリのクライアント側で 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() を呼び出すことで設定されたタイトルをオーバーライドします。

認可

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

showSidebar(userInterface)

ユーザーのエディタにサイドバーが追加され、クライアントサイドのカスタム コンテンツが表示されます。この方法では、サイドバーが開いている間はサーバー側スクリプトが停止されません。サーバー側のスクリプトと通信するために、クライアント側コンポーネントは、HtmlService 用の google.script API を使用して非同期コールバックを行う必要があります。サイドバーをプログラムで閉じるには、HtmlService ウェブアプリのクライアント側で 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

認可

このメソッドを使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

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

サポート終了のメソッド