GAS: prompt for user input with default value
Consider the following Google script applications:
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 display the default value in the input field.
Secondly, do we have keyboard shortcuts for entering / sending data? So that the user can click Enter(or like) to accept the default or value
- Enterto submit their own values.
+3
source to share
1 answer
I didn't find a way for these and searched a lot, so ended up doing the following:
The question (dialogBox text) has a default value and if they wanted it left it blank, the code just used the OR operator.
If you really need it, you can serve and HTML instead of the UI prompt , showModalDialog (userInterface, title) .
For example (not 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>
+3
source to share