1

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 :

  • when using another browser the scripts works fine
  • when using chrome and adding an withFailureHandler it still fails with the same console message without handling the failure (Chrome Version 81.0.4044.122 (Build officiel) (64 bits) on Mac OS High Sierra)
  • when calling the same function from a custom menu the script works fine even in the same Chrome session

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

13
  • Can you check if you are running the V8 runtime or the legacy runtime? Commented Apr 29, 2020 at 22:59
  • 1
    I could confirm that when I tested your script, no error occurs with and without V8. Unfortunately, I couldn't replicate your situation. So for example, when it creates new Spreadsheet and put your script in the script editor and run it, can you confirm whether the same error occurs? But, I'm not sure whether this leads to the solution. I apologize for this. Commented Apr 29, 2020 at 23:47
  • Shouldn't google.script.host.close be called? google.script.host.close(). Also try clearing browser cache,log out and login Commented Apr 30, 2020 at 3:43
  • I cannot reproduce this behaviour. As others said, I'd try clearing cache, and also trying with a new spreadsheet and with other accounts. Commented Apr 30, 2020 at 8:16
  • 1
    Adding a failureHandler might help to catch the uncaught Commented Apr 30, 2020 at 14:54

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

reported on issue tracker

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.