LoginSignup
2
2

More than 5 years have passed since last update.

公式ガイドの「Dialogs and Sidebars in Google Apps」を制覇する!(その1)

Last updated at Posted at 2013-12-02

この記事は、Google Apps Script Advent Calendar 2013の2日目の記事です。

内容は、公式ドキュメントの「Menus and User Interfaces」の中にある、
「Dialogs and Sidebars in Google Apps」の内容を噛み砕いた感じを書いています。
が、いろいろあって、何回かに分けて書かせていただきます。今回は第1弾です。

機能の紹介

Google Docs、Spreadsheet、Formにboundされた(Container-boundな)Scriptには、Dialogと、Sidebarを表示させることができます。Container-boundというのは、Spreadsheetのメニューからツール→スクリプトエディタを選んで作成したApps Scriptのことをいいます。

DialogやSidebarはHTMLServiceやUIServiceを使って、表示内容を制御するようになっています。

AlertDialogを表示する。

Google Document編

コード.gs
// 事前にUiオブジェクトを取得しておく
var ui = DocumentApp.getUi();

function onOpen(e) {
  ui.createMenu("テストメニュー")
  .addItem("AlertDialog表示", 'showAlertDialog')
  .addToUi();
}

// ※この処理は文字を追加するので、初回実行時は承認が必要です。
function showAlertDialog() {
  var result = ui.alert("AlertDialogサンプル", "表示されましたか?", ui.ButtonSet.YES_NO);

  if(result == ui.Button.YES) {
    // Yes
    DocumentApp.getActiveDocument().getBody().appendParagraph("表示された!");
  } else {
    // No
    DocumentApp.getActiveDocument().getBody().appendParagraph("表示されないよ!");
  }
}

Spreadsheet編

は、Browser.Msgbox()で表示されます。これについては、私の著書を参照していただければ…。

Form編

コード.gs
var ui = FormApp.getUi();

function onOpen(e) {
  ui.createMenu("テストメニュー")
  .addItem("AlertDialog表示", 'showAlertDialog')
  .addToUi();
}


function showAlertDialog() {
  var result = ui.alert("AlertDialogサンプル", "表示されましたか?", ui.ButtonSet.YES_NO);

  if(result == ui.Button.YES) {
    // Yes
    FormApp.getActiveForm().addTextItem().setTitle("表示された!");
  } else {
    // No
    FormApp.getActiveForm().addTextItem().setTitle("表示されないよ!");    
  }
}

まとめ

AlertDialogを使うことで、対話型のGASを作ることができる。
(次回はPromptを作ってみたいと思います…となると全部で4回かな?)

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2