2

According to the documentation, calling a Google Apps Script function from a client HTML script should be as simple as google.script.run.myFunction(). However it doesn't seem to work for me:

My Code

function onOpen() {
  SpreadsheetApp.getUi()
      .createMenu('Dialog')
      .addItem('Open', 'openDialog')
      .addToUi();
}

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile('Index')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  SpreadsheetApp.getUi()
      .showModalDialog(html, 'My Dialog');
}

function doSomething() {
  var sheet = SpreadsheetApp.getActive().getActiveSheet();
  sheet.getRange(sheet.getLastRow()+1, 1).setValue("Hello :)");
}

Note that calling doSomething() from the IDE runs smoothly. However not from the HTML script:

My HTML

<script>
google.script.run.doSomething();
</script>
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
neydroydrec
  • 6,973
  • 9
  • 57
  • 89
  • Try replacing the `getActive` function with `openById(id)`. I suspect that since the "environment scope" is different when a function is called from the browser, the active spreadsheet might not be available. – Henrique G. Abreu Jun 25 '15 at 13:05

2 Answers2

2

I use it normally, and it works fine.

But I never tried it before the page load. Since Google has a lot of security barriers, such as replacing the html body with its own custom body, I think it's worth trying to call it in a handler for the page load.

Try putting alerts in the handlers (see below) to see if it is really being run. Any little mistake in other parts of the html code often makes the entire script simply not run.

Things to be aware of: if you have global variables in your server side script, these will not persist!!! They will reset everytime you use google.script.run. So, make sure doSomething() has everything it needs on its own.

And if you expect to see a result after doSomething(), consider adding the handlers to google.script.run. You server side script will not be able to change the user interface nor open a new one if it's modal. (Never tried non modal...)

google.script.run
    .withSuccessHandler(function(data,element){window.alert("executed");})
    .withFailureHandler(function(msg,element){window.alert("failed"); })
    .doSomething();

If none of the messages appear, the problem is not here, but elsewhere. Some other thing is preventing your script from even starting.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • I tried with window alert statements and it is constantly going to the failure handler. My doSomething() function simply returns a string. What could be wrong in it? – shanti Dec 12 '20 at 12:55
1

That appears to be all about timing - google.script.run may be getting called before the client-server connection is ready*. The usual intent of displaying a user interface is to interact with a user - and because people are much slower than machines, you'd not encounter this problem.

Even a short delay to display a toast is enough to make it work.

function doSomething() {
  SpreadsheetApp.getActive().toast("hello")
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.appendRow(["Hello :)"])
  return true;
}

Free bonus! Thinking ahead to the next problem you'll run into, note the return statement. A server function must have some kind of return to cause a callback to the google.script.run.onSuccess() handler.


*Edit - Something isn't quite right with this explanation, since the toast call is on the server. Would be nice if a Googler would speak up to offer a better explanation.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • "The usual intent of displaying a user interface is to interact with a user", fair enough. Then how should I proceed to take data from my spreadsheet and feed it into the HTML? (example, list of names draw from the spreadsheet to select from in a drop down on the dialogue)? – neydroydrec Jun 30 '15 at 06:57
  • I usually use a `ready` function to start these transactions, similar to what you will find in Google's examples. See an example in [this answer](http://stackoverflow.com/questions/24773177/how-to-poll-a-google-doc-from-an-add-on/24773178#24773178). – Mogsdad Jun 30 '15 at 11:41
  • So what I did eventually is editing the HTML string with data from the spreadsheet, before passing it into the HTMLService. – neydroydrec Jul 23 '15 at 14:53