Browser Pop up in Google Apps Script

Vasim picture Vasim · Jul 29, 2013 · Viewed 10k times · Source

Background: I have prepared a form in HTML Service in Google apps script which I call with DoGet function from Code.gs.

my doget function
function doGet() {
  return HtmlService.createTemplateFromFile('HTMLUI').evaluate();
}

Once published it presents a simple browser form with some labels, input box, submit, reset and find buttons on it. The user(s) will input information click submit and the data will get stored in a spreadsheet (background). - Working fine till here.

Now when the user clicks find button - a popup kind of window needs to populated, in this popup user can enter information (from a dropdown) and the selected entry would be populated back in the input boxes which can be amended and submitted again.

Question:

How can I have a POP up kind off window in GAS when on browser.

my find button in HTML service is as follows:

<div><input type="button" onclick="createPopup()" value="Find"></div>

in end for calling the javascript:

<script type="text/javascript">
        function createPopup() {
        google.script.run.popup(document.forms[0]);
    }
</script>

the CreatePopup()javascript code:

function popup(form){
Logger.log("I am first called");
//Mycode should probably go here I think...  
Logger.log("I am last called");
}

When the log is viewed it shows "I am first called" and "I am last called".

My research: I found that the Spreadsheet.toast (something like this) works on spreadsheet, but how do I get the small window on browser..

Answer

Mogsdad picture Mogsdad · Jul 29, 2013

A jQuery dialog will suit your needs. It is an overlay to the current window - not a "pop up".

The demo code here can be easily adapted to Google Apps Script. Here it is, with much of the extra bits removed:

Screenshot Modal Form

Code.js

function doGet() {
  var template = HtmlService
                 .createTemplateFromFile('ModalForm');

  var htmlOutput = template.evaluate()
                   .setSandboxMode(HtmlService.SandboxMode.NATIVE)
                   .setTitle('jQuery UI Dialog - Modal form');

  return htmlOutput;
}

ModalForm.html

<!-- Adapted from http://jqueryui.com/dialog/#modal-form -->

<link rel="stylesheet" href="https://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<script>
$(function() {
var name = $( "#name" ),
email = $( "#email" ),
password = $( "#password" ),
allFields = $( [] ).add( name ).add( email ).add( password ),
tips = $( ".validateTips" );
function updateTips( t ) {
tips
.text( t )
.addClass( "ui-state-highlight" );
setTimeout(function() {
tips.removeClass( "ui-state-highlight", 1500 );
}, 500 );
}
$( "#dialog-form" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Add User": function() {
var bValid = true;
allFields.removeClass( );
// validation removed
if ( bValid ) {
$( "#users tbody" ).append( "<tr>" +
"<td>" + name.val() + "</td>" +
"<td>" + email.val() + "</td>" +
"<td>" + password.val() + "</td>" +
"</tr>" );
$( this ).dialog( "close" );
}
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass(  );
}
});
$( "#form-action" )
.button()
.click(function() {
$( "#dialog-form" ).dialog( "open" );
});
});
</script>

<!-- body -->
<div id="dialog-form" title="Create new user">
<form>
<fieldset>
<label for="name">Name</label>
<input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" />
<label for="email">Email</label>
<input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
<label for="password">Password</label>
<input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
</fieldset>
</form>
</div>
<div id="users-contain" class="ui-widget">
<h1>Existing Users:</h1>
<table id="users" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header ">
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>[email protected]</td>
<td>johndoe1</td>
</tr>
</tbody>
</table>
</div>
<button id="form-action">Open Modal Form</button>