![見出し画像](https://assets.st-note.com/production/uploads/images/67147146/rectangle_large_type_2_052129718f59e71eef23eec39c2e1e48.png?width=1200)
GAS中級7期 ノンプロ研中級講座【GASコース】 Utility Services1 その3
前回のnote
2.UIの操作
GAS中級7期動画 1:06:08 / 1:43:50 あたりから。
UIの操作っていうとみんな期待しちゃうけど、けっこうショボめらしい...?でも、覚えておくといい機能だよ~。
がっつり対話型したいのであれば、フォーム使ったほうが全然よいらしい。
Baseサービス
UIの操作
UI操作できるコンテナ
・スプレッドシート
・ドキュメント
・スライド
・フォーム
操作できるUI
・メニュー
・ダイアログ
・サイドバー
サイドバーやったことないなー。それよりも、Googleさんはいま、カードサービスというのを推してるらしい。下図のアイコンを独自に設定できるそうな。
![](https://assets.st-note.com/img/1638680185453-ChIvyIaI0W.png?width=1200)
ダイアログについて
GAS中級7期動画1:10:12 / 1:43:50 あたり。
Browserクラスはスプレッドシートに限り使用できる。
再利用性等を考えるとUiクラスを用いたほうがよさげ。
ダイアログについては、こちらにちょっと書いた↓
Enum Button
OKとYESは同居できない。
キャンセルやNOの場合は、それ以降の処理をしないという,ガード節、return このへん、ちょっとまだ曖昧理解だな。
演習4-09 Ui
GAS中級7期動画1:13:00 / 1:43:50 あたり
![](https://assets.st-note.com/img/1638680722764-co2ux0a0CV.png)
![](https://assets.st-note.com/img/1638680740983-yqlANRmliE.png)
演習4-10 ★宿題
YESの場合: 「それはいいですね!」
NOの場合: 「それは残念ですね」
とアラートダイアログを表示するようにスクリプトを追加してください。
※HTMLServiceは使用禁止でお願いします。
私の提出したコードはこんな感じ。
バツボタンでダイアログを閉じてしまった人用のメッセージも付けてみた。
function myFunction4_10() {
const ui = SpreadsheetApp.getUi();
const title = 'ダイアログ';
const prompt1 = '名前を入力してください';
const response = ui.prompt(title, prompt1, ui.ButtonSet.OK);
const name = response.getResponseText();
const prompt2 = `Hello ${name}さん!\nお元気ですか?`;
const response2 = ui.alert(title, prompt2, ui.ButtonSet.YES_NO);
const prompt_yes = 'それはいいですね!';
const prompt_no = 'それは残念ですね';
const prompt_none = 'おや?';//バツボタンでダイアログを閉じてしまった人用のメッセージ
if (response2 == ui.Button.YES) {//response2.getSelectedButton() と書いていたが、.getSelectedButton()は書かなくていい
ui.alert(prompt_yes, ui.ButtonSet.OK);
} else if (response2 == ui.Button.NO) {
ui.alert(prompt_no, ui.ButtonSet.OK);
} else {
ui.alert(prompt_none, ui.ButtonSet.OK);
}
}
「response2.getSelectedButton() と書いていたが、.getSelectedButton()はいらない」
これは、alertメソッドの戻り値が選択されたEnum Buttonだからですね◎その戻り値をresponse2に代入しているので、response2には選択されたEnum Buttonが入っているため再取得する必要がないんです。https://developers.google.com/apps-script/reference/base/ui?hl=en#alertprompt
メニューの作成手順
1:15:15 / 1:43:50
マクロからもメニューの追加はできるが、特定のユーザにのみメニュー表示などする場合はGASで仕込む必要がある。
![](https://assets.st-note.com/img/1638709116492-F7sjWv9bwh.png?width=1200)
![](https://assets.st-note.com/img/1638709138410-gw5Mt6jmp3.png)
![](https://assets.st-note.com/img/1638709215442-8Ip4FmBGeh.png)
演習4-11 独自メニュー
![](https://assets.st-note.com/img/1638701546337-XyzkjUysjV.png?width=1200)
演習4-12 独自メニューに機能追加 ★宿題
1:19:13 / 1:43:50
どっちだ?どっちでもいいのか?
activate()
https://developers.google.com/apps-script/reference/spreadsheet/range?hl=en#activate
setActiveSelection(range)
https://developers.google.com/apps-script/reference/spreadsheet/sheet?hl=en#getmaxrows
続きはまた今度。
いいなと思ったら応援しよう!
![good-sun(a03)](https://assets.st-note.com/production/uploads/images/74915135/profile_e6435615735e2ec66a673a40d90bb4a5.png?width=600&crop=1:1,smart)