Serge insas
Serge insas

Reputation: 46792

google.script.run in html UI doesn' t call server function

I have a couple of scripts that don't work anymore although I didn't make any change. I don't spend a lot of time programming these last month so I didn't notice it right away... Here is a simplified code that used to work and that doesn't, I can't figure out my error. The HTML part is entirely built on server side since it's a very simple code (runs from a spreadsheet).

function myFunction() {
  var doc = '<body style="font-family:arial,sans;font-size:12pt">';
  doc+='<select id="target" multiple style="font-family:arial,sans;font-size:12pt">';
  doc+='<option value="1">choice 1</option><option value="2">choice 2</option><option value="3">choice 3</option></select><br><br>';
  doc+='<input type="button" onClick="processJS()" style="background:#BFA;font-size:12pt" value="Validate"/>';
  doc+='<script>function processJS(){var e=document.getElementById("target");var values=Array.from(e.selectedOptions).map(option => option.value);';
  doc+='console.log(JSON.stringify(values));google.script.run.withSuccessHandler(function(msg){ window.alert(msg);google.script.host.close;}).processGSTest(values)};</script>';
  doc+='</body>';

  var ui = HtmlService.createHtmlOutput(doc).setWidth(500).setHeight(250);
  SpreadsheetApp.getUi().showModelessDialog(ui, "test");
}

function processGSTest(values){
  Logger.log(JSON.stringify(values));
  return "processGSTest ok";
}

I get an error in JS console like this when I hit "validate" :

enter image description here

the HTML rendered window looks like this :

enter image description here


EDIT

Following comments (thanks again) I've got a few more informations :

conclusion of this edit : I don't understand the issue :)

Upvotes: 1

Views: 103

Answers (1)

Serge insas
Serge insas

Reputation: 46792

Since this post is about a very specific case that does not seem to be reproductible I answer it so it doesn't stay open indefinitely. In the same time I'll post an issue report on the tracker.

Here is the script I use to show the issue. The main function (myFunction) reports an error when called from the script editor but works when called from the menu...

This happens only on Chrome Version 81.0.4044.122 (Build officiel) (64 bits) on Mac OS High Sierra 10.13.6, it works normally on Safari, Firefox and even Chrome on Windows 10. (It works also normally on the same version of Chrome under Mac OS 10.10.5 !! )

function myFunction() {
  var doc = '<body style="font-family:arial,sans;font-size:12pt">';
  doc+='<select id="target" multiple style="font-family:arial,sans;font-size:12pt">';
  doc+='<option value="1">choice 1</option><option value="2">choice 2</option><option value="3">choice 3</option></select><br><br>';
  doc+='<input type="button" onClick="processJS()" style="background:#BFA;font-size:12pt" value="Validate"/>';
  doc+='<script>function processJS(){var e=document.getElementById("target");var values=Array.from(e.selectedOptions).map(option => option.value);';
  doc+='console.log(JSON.stringify(values));google.script.run.withFailureHandler(function err(){window.alert("error triggered by withFailureHandler");})';
  doc+='.withSuccessHandler(function(msg){ window.alert(msg);google.script.host.close();}).processGSTest(values)};</script>';
  doc+='</body>';

  var ui = HtmlService.createHtmlOutput(doc).setWidth(500).setHeight(250);
  SpreadsheetApp.getUi().showModelessDialog(ui, "test");
}

function processGSTest(values){
  Logger.log(JSON.stringify(values));
  return "processGSTest ok";
}

function onOpen(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [{name: "test", functionName: "myFunction"}]
  ss.addMenu("test",menuEntries);
}

I created a shared spreadsheet for anyone that would want to play with it (read only, make a copy to use)

Upvotes: 1

Related Questions