【问题标题】:Prompt message box through custom menu on Google Sheets/App Script通过 Google 表格/应用脚本上的自定义菜单提示消息框
【发布时间】:2018-02-24 17:18:08
【问题描述】:

我在 Google 表格上创建了​​一个自定义菜单,其中包含两个选项:

function onOpen(e) {
 var menu = SpreadsheetApp.getUi().createMenu('Custom')
 menu.addItem('Add new store', 'openForm')
 menu.addItem('Update Database', 'replacebackenddatabase') }

当用户选择“更新数据库”时,我希望出现一个消息框并要求确认“您要继续”和“是/否”基础。如果用户选择“是”,我希望运行“replacebackenddatabase”功能。如果没有,我只想关闭消息框,不要发生任何事情。

我该怎么做? 谢谢!

【问题讨论】:

    标签: if-statement google-apps-script google-sheets


    【解决方案1】:

    查看Prompt boxes here

    function replacebackenddatabase() {
      var ui = SpreadsheetApp.getUi();
    
      var result = ui.prompt(
        'Ask a question...',
        ui.ButtonSet.OK_CANCEL);
    
      // Get the response...
      var button = result.getSelectedButton();
      var text = result.getResponseText();
      if (button == ui.Button.OK) {
        //If they clicked OK do something with 'text' variable
    
      } else if (button == ui.Button.CANCEL) {
        // If they clicked Cancel.
    
      } else if (button == ui.Button.CLOSE) {
        // If they closed the prompt
    
      }
    }

    【讨论】:

      【解决方案2】:
      function onOpen(e) {
       var menu = SpreadsheetApp.getUi().createMenu('Custom')
       menu.addItem('Add new store', 'openForm')
       menu.addItem('Update Database', 'checkResponse')
       //.addSeparator()
       //.addSubMenu(SpreadsheetApp.getUi().createMenu('Sub Menu')
       //.addItem('One sub-menu item', 'subFunction1')
       //.addItem('Another sub-menu item', 'subFunction2'))
       .addToUi(); 
      
      // do not set a variable to any chain of methods ending in .addToUi()
      // after the Menu is created, the value of menu will be undefined
      // the .addToUi() method does not return anything.
      
       }
      
      
      function checkResponse() {
        var ui = SpreadsheetApp.getUi();
        var response = ui.alert('Are you sure you want to proceed?', ui.ButtonSet.YES_NO);
      
        if (response == ui.Button.YES) {
          replacebackenddatabase();
        } else {
          Logger.log("The user wasn't sure.");
        }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-16
        • 1970-01-01
        • 2019-12-14
        • 2018-03-13
        • 1970-01-01
        相关资源
        最近更新 更多