HelloWorld
HelloWorld

Reputation: 11247

Can't pass values to HtmlService

Here is the function I am using to get all the active values on my spreadsheet:

function getAllValuesInSheet(){
  var values = SpreadsheetApp.getActiveSpreadsheet().getDataRange().getValues();
  Logger.log(values);
  return values;
}

This returns an 2D Array of my values.

In my HtmlService I have:

<script>

    function onSuccess(values) {
      console.log("ran");
      console.log(values);
     }

    function populate(){
      google.script.run.withSuccessHandler(onSuccess).getAllValuesInSheet();
    }


</script>

<script>
  // Using the "load" event to execute the function "populate"
  window.addEventListener('load', populate);
</script>

I get an error Uncaught ScriptError: The script completed but the returned value is not a supported return type.

I have functions similar to this one that work perfectly fine. I wonder if the data is too large to pass? It's not that big it's under 500 cells.

Why am I getting this error? How can I fix this? I am trying to load all my spreadsheet data to my HtmlService. Is this a bad practice for small data sets? Should I make ajax calls back and forth instead?

Upvotes: 0

Views: 290

Answers (2)

Mogsdad
Mogsdad

Reputation: 45710

The reason that Gerardo's answer works is because it ensures that your server-side function returns a String, which is a JavaScript primitive.

Your original request would fail if any of the array contents consisted of unsupported types such as Date objects.

Source: HTML Service: Communicate with Server Functions

Upvotes: 0

Gerardo
Gerardo

Reputation: 3845

Try converting the array to a json format with the function JSON.stringify(object) so the array is easier to treat.

your code would be like:

return JSON.stringify(values);

Check if this doesn't work for you.

Upvotes: 1

Related Questions