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

プロパティ

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

メソッド

メソッド戻り値の型概要
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.
const ui = SpreadsheetApp.getUi();
const 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.
const ui = SpreadsheetApp.getUi();
const 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.
const ui = SpreadsheetApp.getUi();
const 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.
const ui = SpreadsheetApp.getUi();
const 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.
const ui = SpreadsheetApp.getUi();
const 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)

カスタムのクライアントサイド コンテンツを含むモーダル ダイアログ ボックスをユーザーのエディタで開きます。このメソッドは、ダイアログが開いている間、サーバーサイド スクリプトを一時停止しません。サーバーサイド スクリプトと通信するには、クライアントサイド コンポーネントが HtmlServicegoogle.script API を使用して非同期コールバックを行う必要があります。ダイアログをプログラムで閉じるには、HtmlService ウェブアプリのクライアント サイドで google.script.host.close() を呼び出します。詳しくは、ダイアログとサイドバーのガイドをご覧ください。

モーダル ダイアログが表示されている場合、ユーザーはダイアログ以外のものを操作できません。一方、モードレス ダイアログサイドバーでは、ユーザーはエディタを操作できます。ほとんどの場合、モードレス ダイアログよりもモーダル ダイアログまたはサイドバーの方が適しています。

// Display a modal dialog box with custom HtmlService content.
const 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)

カスタムのクライアントサイド コンテンツを含むモードレス ダイアログ ボックスをユーザーのエディタで開きます。このメソッドは、ダイアログが開いている間、サーバーサイド スクリプトを一時停止しません。サーバーサイド スクリプトと通信するには、クライアントサイド コンポーネントが HtmlServicegoogle.script API を使用して非同期コールバックを行う必要があります。ダイアログをプログラムで閉じるには、HtmlService ウェブアプリのクライアント サイドで google.script.host.close() を呼び出します。詳しくは、ダイアログとサイドバーのガイドをご覧ください。

モードレス ダイアログを使用すると、ユーザーはダイアログの背後にあるエディタを操作できます。一方、モーダル ダイアログはそうではありません。ほとんどの場合、モードレス ダイアログよりもモーダル ダイアログまたはサイドバーの方が適しています。

// Display a modeless dialog box with custom HtmlService content.
const 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)

ユーザーのエディタに、カスタムのクライアントサイド コンテンツを含むサイドバーを開きます。このメソッドは、サイドバーが開いている間、サーバーサイド スクリプトを一時停止しません。サーバーサイド スクリプトと通信するには、クライアントサイド コンポーネントが HtmlServicegoogle.script API を使用して非同期コールバックを行う必要があります。サイドバーをプログラムで閉じるには、HtmlService ウェブアプリのクライアント サイドで google.script.host.close() を呼び出します。詳しくは、ダイアログとサイドバーのガイドをご覧ください。

左から右に向かう言語を使用する環境のユーザーには、エディタの右側にサイドバーが表示されます。右から左に向かう言語を使用する環境のユーザーには、エディタの左側にサイドバーが表示されます。スクリプトで表示されるサイドバーはすべて幅 300 ピクセルです。

// Display a sidebar with custom HtmlService content.
const 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

サポート終了のメソッド