7

Consider the following Google apps script code:

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

I would like the dialog to show a default value in the input box.
Secondly, do we have keyboard shortcuts to enter/send data? So that the user can hit Enter (or the likes) to accept the default or value-Enter to send his/her own values.

antonio
  • 10,629
  • 13
  • 68
  • 136

1 Answers1

3

I didn't find a way for neither, and have searched alot, so ended up doing the following:

Stated in the question (dialogBox text) what was the default value, and if they wanted it, leaved it blank, in the code just used the OR operator.

If you really really need it, you can serve and HTML instead of a UI prompt, with showModalDialog(userInterface, title).

Eg (Haven't tested).

Code.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;
}

in 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
  • 3,731
  • 1
  • 15
  • 23
  • Thanks @Kriggs, what tags are to be used in `.createHtmlOutput' to obtain the text boxes and submit it? The usual HTML form tags? – antonio Jan 11 '15 at 14:48
  • Yes, it's mostly a normal HTML page, you can use HTML5, Jquery, almost anything, but too loaded with Java it won't load in some browser, to be safe use the sandbox NATIVE instead of IFRAME since it will be a simple form. – Kriggs Jan 11 '15 at 15:07
  • To send the answer back to the server you'll have to use [google.script.run](https://developers.google.com/apps-script/guides/html/reference/run#myFunction(...)). Will you use this as a library? – Kriggs Jan 11 '15 at 15:10
  • I am using it as a spreadsheet macro, reading dates and creating calendar events. – antonio Jan 11 '15 at 15:17
  • Added in original answer. – Kriggs Jan 11 '15 at 16:00