0

I am attempting to build a UI for a spreadsheet using GAS HtmlService. The HTML below is a very simple form with a single text box that pulls a value ("Kristina") from the sheet, successfully. However, when I try to submit the form a new tab is opened in Chrome that attempts to open the URL "bffc95ee-ff64-4d2c-xxxx-19d9824eb4b4.foo.bar/?fname=Kristina" with "xxxx" replacing more random letters and numbers (just in case). At no point do I use the words "foo.bar" in my code, so I'm pretty sure that that part isn't coming from me. It does not change each time or after logging out and back in. I'm getting the same result on two different computers.

<html>
<body>
  <div>
    <form id="formtest1">
      <label>First Name</label>
      <input name="fname" type="text" maxlength="255" value="<?= fname ?>"/>
      <input type="submit" value="Submit" 
          onclick="google.script.run.processForm(document.getElementById('formtest1'));
          google.script.host.close()"/>
    </form>
  </div>
</body>
</html>

The above is being displayed using the following function:

function htmltest(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sht = ss.getActiveSheet();
  var html = HtmlService.createTemplateFromFile("HTML");
  html.fname = sht.getRange(2, 3).getValue();
  ss.show(html.evaluate());
};

If I understand correctly, the "google.script.run.processForm(...)" script in the HTML should trigger the following function, as set up in the projects triggers:

function onFormSubmit(){
Browser.msgBox("Test");
};

But it doesn't appear to do so as the form doesn't close and the msgBox doesn't appear. Only the foo bar URL in a new tab.

Hopefully I've explained the issue clearly and am not making an embarrassing mistake.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user2391040
  • 1
  • 1
  • 2

3 Answers3

2

You cannot use a real "submit" button with google.script.run (this is a documented restriction in the user guide). Change it to "button" and it should work fine.

Corey G
  • 7,754
  • 1
  • 28
  • 28
1

The project trigger onFormSubmit() will be triggered by a submission via the Forms Service. There is no relationship between this trigger and your HTML code; they are two different ways to interact with users.

An html forms pattern is shown in the HTML Service documentation here, and the script below is an adaptation of it.

Code.gs

The only real change from your original is that onFormSubmit() has been replaced with processForm(form), which includes a parameter, for the object that will be passed from the html code.

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "htmltest",
    functionName : "htmltest"
  }];
  sheet.addMenu("Custom Menu", entries);
};

function htmltest(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sht = ss.getActiveSheet();
  var html = HtmlService.createTemplateFromFile("HTML");
  html.fname = sht.getRange(2, 3).getValue();
  //Logger.log( html.getCodeWithComments() );
  ss.show(html.evaluate());
};

function processForm(form){
  var fname = form.fname;
  Browser.msgBox("Test - " + fname);
};

HTML.html

This is a modification of your original, echoing the pattern from the documentation. The form submission SuccessHandler is a one-liner, which closes the dialog. Once it completes, the server-side function is invoked with the form content, retrieved using this.parentNode (to refer to the form).

There are other ways - see Get value of html text box in Apps Script function for a different approach.

<html>
<script type="text/javascript">
  // SuccessHandler to close form
  function close() {google.script.host.close();}
</script>
<body>
  <div>
    <form>
      <label>First Name</label>
      <input name="fname" type="text" maxlength="255" value="<?= fname ?>"/>
      <input type="button" value="Submit" onclick="google.script.run
                                                   .withSuccessHandler(close)
                                                   .processForm(this.parentNode)"/>
    </form>
  </div>
</body>
</html>
Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Apologies for the delay. Just got back to this. Thanks for your comprehensive explanation. Your solution does, of course, work and I was right, it was an embarrassing mistake. I´ve been going through the documentation again and again trying various different combinations, but unfortunately never made the connection that the onFormSubmit trigger was for the forms service as opposed the the Html Service. Thanks again for your your time. – user2391040 May 18 '13 at 12:24
1

Just add this to your script tag on your html file.

// Prevent forms from submitting.
function preventFormSubmit() {
  var forms = document.querySelectorAll('form');
  for (var i = 0; i < forms.length; i++) {
    forms[i].addEventListener('submit', function(event) {
      event.preventDefault();
    });
  }
}
window.addEventListener('load', preventFormSubmit);

Source: HTML Service: Communicate with Server Functions

egermano
  • 1,809
  • 1
  • 12
  • 13