iT邦幫忙

2024 iThome 鐵人賽

DAY 7
0

Google Apps Script 可在多種 Google 產品中建立自訂選單/自定義選單!
像是 Google 文件、試算表、簡報、或表單中等等都可以實現
而選單中的項目可以與 GAS 中的自定義 Function 做連結

範例: 我想要有一個 My tools 的選單, 下面要有一個叫做 hello 的項目

function onOpen() {
  SpreadsheetApp
    .getUi()
    .createMenu('My tools') // 自訂選單名稱
    .addItem('Say Hello', "hello") // 前:選單項目名稱, 後: 連結的 function 名稱, 注意是"字串"
    .addToUi()
}

Note: hello 的函示可先留空, 下方還有進階教學!

這樣就會產生出一個像下圖的自定義選單啦!是不是超簡單!
day 7 create menu

如果想再多產出個選單項目,就在後面再多加一個 addItem 就行了!

function onOpen() {
  SpreadsheetApp
    .getUi()
    .createMenu('My tools') 
    .addItem('Say Hello', "hello") 
    .addItem('Say Bye', "bye") // 新增一個選單項目
    .addToUi()
}

如果要創造 SubMenu 的話就使用 .addSubMenu()


function onOpen() {
  SpreadsheetApp
    .getUi()
    .createMenu('My tools') // 自訂選單名稱
    .addItem('Say Hello', "hello") // 前:選單項目名稱, 後: 連結的 function 名稱, 注意是"字串"
    .addSeparator()
    .addSubMenu(ui.createMenu('Sub-menu')
          .addItem('Say goodbye', 'goodbye'))
    .addToUi()
}

https://ithelp.ithome.com.tw/upload/images/20240920/20137680kyAvvhPred.png

假如我今天想要按下按鈕後,他跳出來一個訊息框跟使用者 Say Hello,可以用 getUi().alert 來完成

  • Alert Dialog 快訊對話方塊
function onOpen() {
  SpreadsheetApp
    .getUi()
    .createMenu('My tools') // 自訂選單名稱
    .addItem('Say Hello', "hello") // 連結的 function 名稱, 注意是"字串"
    .addToUi()
}

function hello() {
  SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp or FormApp.
     .alert('hello!');
}

https://ithelp.ithome.com.tw/upload/images/20240920/20137680EpuIdag2C7.png
若要讓使用者按「確認」或「取消」按鈕,可以搭配使用 ui.ButtonSet.YES_NO

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show alert', 'showAlert')
      .addToUi();
}

function showAlert() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.alert(
     'Please confirm',
     'Are you sure you want to continue?',
      ui.ButtonSet.YES_NO);

  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
    ui.alert('Confirmation received.');
  } else {
    // User clicked "No" or X in the title bar.
    ui.alert('Permission denied.');
  }
}

https://ithelp.ithome.com.tw/upload/images/20240920/20137680VzPORoLMAH.png

https://ithelp.ithome.com.tw/upload/images/20240920/20137680i6GCEStXaF.png

Note: App Script 需繫結至文件、試算表或表單,OnOpen() 函式才能使用。

如果我想要讓使用者輸入訊息框,能用 Prompt Dialog getUi().prompt()

  • Prompt Dialog 提示對話方塊
function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show prompt', 'showPrompt')
      .addToUi();
}

function showPrompt() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.prompt(
      'Let\'s get to know each other!',
      'Please enter your name:',
      ui.ButtonSet.OK_CANCEL);

  // Process the user's response.
  var button = result.getSelectedButton();
  var name = result.getResponseText();
  if (button == ui.Button.OK) {
    // User clicked "OK".
    ui.alert('Hello ' + name + '.');
  } else if (button == ui.Button.CANCEL) {
    // User clicked "Cancel".
    ui.alert('I didn\'t get your name.');
  } else if (button == ui.Button.CLOSE) {
    // User clicked X in the title bar.
    ui.alert('You closed the dialog.');
  }
}

https://ithelp.ithome.com.tw/upload/images/20240920/20137680oSIY3sxBcn.png

https://ithelp.ithome.com.tw/upload/images/20240920/20137680Fm2H58mDRT.png
今天這樣就介紹完了如何自訂選單&跳出訊息框,前往下一天前進吧!

reference

https://spreadsheet.dev/pop-up-alert-messages-in-google-sheets


上一篇
[Day 6] GAS - Google form 表單自動化寄出信件
下一篇
[Day 8] GAS - Google Sheet 操作大全 Part 1 - 讀取資料
系列文
30 天玩轉 GAS: 打造你的個人自動化助手13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言