GoogleSheets google.script.run always going to FailureHandler

Viewed 563

I am using GoogleSheets HTMLService. I am calling google.script.run from my Html page's script. But it is always going to FailureHandler. What is wrong in it? Please see the code below. When I run it, it always shows the alert Failed. Also, the logger does not show any error. It is also not showing the console log "Inside Hello" in the hello() function. Do we also need to do some browser settings (I am using chrome - javascript allowed).

[UPDATED] After replacing Logger.log with console.log, I am seeing it as Transport Error.

modeDialog.gs

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile("test");
  html.setWidth(90).setHeight(1);
  var ui = SpreadsheetApp.getUi().showModalDialog(html, "Opening ..." );
}

function hello() {
  console.log("Inside Hello");
  return "hello";
}


test.html

<!DOCTYPE html>

<html>  
  <head>
  <base target="_top">
  <script>
  function onSuccess(str) {
  window.alert("executed");
  }
  
  function onFailure(error) {
  window.alert("failed");
  Logger.log(error);
  }
  
  google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).hello();
  
  </script>
  </head>
  <body>
    <div id="failureContent"></div>
    Hello world
  </body>  
</html>


2 Answers

As I mentioned that it was working earlier in Chrome and is currently working in FireFox, I tested it again after changing my chrome settings to default by going to Chrome > Settings > Advanced > Reset and clean up > Restore settings to their original defaults.

It is working fine after that. So setting this as the answer.

I ran it this way:

GS:

function openDialog() {
  SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutputFromFile("ah3"), "Opening ..." );
}

function hello() {
  return "hello";
}

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Hello world
    <script>
    window.onload=function(){
      google.script.run
      .withSuccessHandler(function(str){window.alert("executed");})
      .withFailureHandler(function(error){window.alert("failed");})
      .hello();
    }  
    console.log('MyCode');
    </script>
  </body>
</html>

I just like to use onReadyState function or onload to run most javascript so that html is already loaded. Not that it makes much difference in this trivial example. Also I tend to put the scripts in the body rather than in the head.

Related