Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GAS: prompt for user input with a default value

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.

like image 870
antonio Avatar asked Oct 20 '22 19:10

antonio


1 Answers

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>
like image 63
Kriggs Avatar answered Oct 27 '22 11:10

Kriggs