0

In the HtmlService docs async sample code...

How does the parameter things get passed into the function showThings(things) after the page loads?

Running as is, the html list just gets emptied.

In my Code.gs, I added in...

function getLotsOfThings() {
  return [1,2,3];
}

but it doesn't pass or store the result so showThings() can use it.

2
  • works fine for me function doGet(request) { return HtmlService.createTemplateFromFile('Page') .evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE); } function getLotsOfThings() { Logger.log('was called') return [1,2,3]; } Commented Oct 31, 2013 at 22:11
  • yeah, i see how getLotsOfThings() gets called, but not how showThings() is ever sent things. Commented Oct 31, 2013 at 22:20

2 Answers 2

1

The built-in google.script.run does this for us.

You can think that the Apps Script platform gets the return value of your server-side function JSON.stringify it and then JSON.parse on the client-side normally. And since withSuccessHandler receives you return function, running it and passing a parameter is easy. As shown by Frits.

Here is what the withSuccessHandler documentation says:

Because client-side code continues to the next line without waiting for a server call to complete, the google.script API allows you to specify another client-side function to run when the server responds. If the server function returns a value, the API passes the value to the new function as a parameter.

By the way, the code example worked just fine for me. Have you had any problems? This is the code I used (plus the example exact html file, which I called "page"):

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

function getLotsOfThings() {
  return [1,2,3];
}
Sign up to request clarification or add additional context in comments.

2 Comments

was using createTemplateFromFile() and missing the non-visible API pass concept.
createTemplateFromFile should work too. It's just not necessary since there's nothing to be evaluated on that html example. Actually, that's the main idea of doing asynchronous work. The same page should load faster when using "createOutput..." instead of "createTemplate...", specially because the can cache the cajoling part and not have to redo it every time (which they can't avoid when you createTemplate).
0

I think that might be a mistake.

It can still work though, see this example:

function foo(arg) {
    console.log(arg);
}
function run(callback) {
    callback("bar");
}
run(foo); // "bar"

From the code it's not obvious what the value of things is though, or where it comes from.

1 Comment

yeah i'm trying to find out where things comes from or what the assumptions are

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.