【问题标题】:GAS: prompt for user input with a default valueGAS:使用默认值提示用户输入
【发布时间】:2015-01-11 13:48:02
【问题描述】:

考虑以下 Google 应用程序脚本代码:

var ui = SpreadsheetApp.getUi();
var result = ui.prompt(
   'How many days in advance you want to receive the event notification?',
   '',
   ui.ButtonSet.OK_CANCEL);  

我希望对话框在输入框中显示默认值。
其次,我们是否有键盘快捷键来输入/发送数据?这样用户可以点击Enter(或类似的)来接受默认值或value-Enter 来发送他/她自己的值。

【问题讨论】:

    标签: google-apps-script google-api modal-dialog google-docs-api google-spreadsheet-api


    【解决方案1】:

    我都没有找到方法,并且搜索了很多,所以最终做了以下事情:

    在问题(对话框文本)中说明默认值是什么,如果他们想要它,请将其留空,在代码中只使用 OR 运算符。

    如果您真的需要它,您可以使用showModalDialog(userInterface, title) 提供 HTML 来代替 UI 提示。

    例如(未测试)。

    代码.gs

    function showTab() {
      var html = HtmlService.createTemplateFromFile('modalDialog').evaluate()
          .setSandboxMode(HtmlService.SandboxMode.IFRAME)
          .setTitle('Planejamento e controle')
          .setWidth(300);
      SpreadsheetApp.getUi().showModalDialog(html, "Title") // Or DocumentApp or FormApp.
    }
    
    function onOen(){
      var ui = SpreadsheetApp.getUi();
      var fun = 'FuncoesOrcamentoV2.'
    
      ui.createMenu('Carregar controles').addItem('Sidebar P&C', fun+'showTab')
      .addToUi();
    }
    
    function writeToSheet( servico, colNivel ){
      var ss = SpreadsheetApp.getActive(),
          rangeAt = ss.getActiveRange(),
          novoRange = ss.getActiveSheet().getRange(rangeAt.getRow() + 1, rangeAt.getColumn());
      ss.setActiveRange( novoRange );
      SpreadsheetApp.flush();
    
      if( servico != "" )
        rangeAt.setValue( servico );
    
      return "Suceffully inserted " + servico;
    }
    

    在modalDialog.hmtl中:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
    <input id="autocomplete" placeholder="Digite a composição desejada e aperte enter" >
    <input type="button" value="Inserir" onclick="escrever()" /><br><br>
    <script>
    function escrever(){
        var compAtual = $( "#autocomplete" ).val();
        $( "#autocomplete" ).focus().val('');
        google.script.run.withSuccessHandler( logga ).writeToSheet( compAtual, 3);
    }
    
    function logga( e ){
        console.log( e );
    }
    
    $("#autocomplete").keyup(function (e) {
        if (e.keyCode == 13) {
            escrever();
        }
    });
    </script>
    

    【讨论】:

    • 谢谢@Kriggs,在`.createHtmlOutput'中使用什么标签来获取文本框并提交呢?常用的 HTML 表单标签?
    • 是的,它主要是一个普通的 HTML 页面,你可以使用 HTML5,Jquery,几乎任何东西,但是太加载了 Java,它不会在某些浏览器中加载,为了安全起见,使用沙箱 NATIVE 而不是IFRAME,因为它将是一个简单的表单。
    • 要将答案发送回服务器,您必须使用google.script.run。你会把它用作图书馆吗?
    • 我将它用作电子表格宏,读取日期并创建日历事件。
    • 添加到原始答案中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    相关资源
    最近更新 更多