0

I have followed code snippets and directions given at https://developers.google.com/apps-script/guides/html/communication#index.html

And added following codes in my scripts project:

Code.gs

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index');
}


function doSomething() {
  Logger.log('I was called!');
}

Index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>

google.script.run.doSomething();

</script>

  </head>

</html>

But my function doSomething is not getting called. This is a fresh google scripts project that I have started and published it as web app.

I have seen similar issue reported in this thread and this thread. I have tried the measures mentioned there, but still facing the issue. Also, I see that there is no accepted answer there, so posting this question again. I don't know how to bring those questions back to life.

Thanks, Mukesh

Mukesh Ghatiya
  • 398
  • 1
  • 5
  • 15
  • I would do it like this: ` ` – Cooper Jun 11 '20 at 03:27
  • Did you issue a get with a browser? Did you authorize the scripts? – Cooper Jun 11 '20 at 03:28
  • I think that in your script, when the Web Apps reflects the latest script, `doSomething()` works. And you can see the log of `doSomething()` of `google.script.run.doSomething()` at "View" -> "Logs" -> Open the link "Apps Script Dashboard" of the script editor. In this case, the log on script editor shows "No functions have been run in this editor session.". But you can see it at Function and Type of "doSomething" and "Web App" at "Apps Script Dashboard", respectively. So can you confirm again? – Tanaike Jun 11 '20 at 03:31
  • As @Cooper has suggest I would use the [`onLoad`](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload) property to ensure the correct executions. But your code should have been executed as it is. Take a look at the logs for your project and see if the event has been logged. – Raserhin Jun 11 '20 at 07:57
  • @Cooper, are you suggesting that code given at Google website is incomplete and I should add window.onload? Is it mandatory? Yes, I did issue get with browser. I used "Publish-> Deploy as web app" and then hit the url that was given. – Mukesh Ghatiya Jun 11 '20 at 09:58
  • It’s not mandatory but running scripts that add listeners or content to the page before it’s completely loaded will cause problems – Cooper Jun 11 '20 at 14:11

1 Answers1

1

You can test it a lot easier in a dialog.

GS:

function doSomething() {
  SpreadsheetApp.getActive().toast('I was called!');
  return 'I called';
}

function openDialog() {
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutputFromFile('ah3'), "Testing")
}

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
    </script>
  </head>
  <body>

<script>
window.onload=function(){
  google.script.run
  .withSuccessHandler(function(msg){
     window.alert(msg);//display returned message
     google.script.host.close();//close the dialog
  })
  .doSomething();//The call to the server
  }
  </script>
 </body>
</html>
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thanks Cooper. Can you please what could be going wrong in my code itself? I don't want to get into another complexity of using Spreadsheet. I don't intend to use spreadsheet in this application (at least for now) and don't want to get there. Also, you are suggesting it to be used on onload. But why? How will I use it in other cases where I don't want it on onload? – Mukesh Ghatiya Jun 11 '20 at 10:02
  • In general I don’t like to run any scripts until the document is completely loaded into the browser. The same is true for setting up listeners. – Cooper Jun 11 '20 at 14:06
  • I don't think that's the problem I am facing though. I had some other script.google.run call which was being called after page was loaded, but the call was not actually calling the function. I tried to debug it. While debugging I simplified it, but it still is failing. – Mukesh Ghatiya Jun 11 '20 at 15:27
  • 1
    Actually your code is running before the page is loaded because it’s running out of the head but nonetheless and I tested it it actually works maybe you just don’t know how to use the logger I don’t know do you know and V-8 the longer it takes a long time so it’s better to actually go to view executions and look in the stack logging – Cooper Jun 11 '20 at 16:36
  • Yes, I just did that and looks like that was it! Logs are indeed showing in stackdriver logs. I don't know why they are not showing in "View Logs" though. It is so confusing. Why do two exist? Why can't they clearly tell which one to use? – Mukesh Ghatiya Jun 11 '20 at 17:41
  • Well, being an online environment is challenging usually the software upgrades come first and then the documentation follows. That's why you can never get too complacent and feel like you know everything because it's constantly changing. I think that the number of function in Google Apps Script has probably more than doubled since I've been using it and now with ES6 the capability is hugely improved and some transitioning hiccups are to be expected. – Cooper Jun 11 '20 at 18:10
  • Sure. Could you put your previous comment as an answer? I'll accept that a the answer for this question. – Mukesh Ghatiya Jun 12 '20 at 00:55