neydroydrec
neydroydrec

Reputation: 7323

HTML-service pages to call server-side Apps Script functions with dialog box

I've tried following the model for Client-to-Server Communication on Google Scripts, using unlike in the model, a modal dialog.

Model Code:

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

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

Model HTML:

<script>
  google.script.run.doSomething();
</script>

My Code:

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

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

My HTML is the same as the model HTML.

Running this script however, does not call the Logger as expected. Did I do this wrong?

Upvotes: 0

Views: 651

Answers (1)

Mogsdad
Mogsdad

Reputation: 45750

This has been reported as Issue 5177, star it for updates & to get attention for the problem.

There are circumstances where we should not expect the Logger to operate, for example when using a Time-based Trigger function, because there is no association between the execution instance and a debugger instance to record logs.

However, that's not the case here. We should have the ability to use the logger here, but it's unreliable. The logs do show up, sometimes:

screenshot

Because of this behaviour, I use the BetterLog library to record logs to a spreadsheet when debugging. (You can even invoke it via google.script.run to log to the spreadsheet from within your client-side JavaScript. See more about that in How do I run Server-side functions using HtmlService.)

Upvotes: 1

Related Questions