1

I am trying to pass a 2D array created in a Javascript file here:

Code.gs

function getPermits()
{
  Logger.log('Starting permits');
  var permits =  SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
  Logger.log('Got permits: ' + permits [0][0]);

  return permits;
}

And I call that function with a handler in this html file:

<script type="text/javascript">
   function onSuccess(numUnread) {
    alert('You have ' + numUnread[0][0]
       + ' unread messages in your Gmail inbox.');
      document.getElementById("id2").innerHTML = numUnread[0][0];


}

   function onFail(numUnread){
   alert('Script failed!');
   }

 google.script.run.withSuccessHandler(onSuccess).getPermits();
 google.script.run.withFailureHandler(onFail).getPermits();
</script>

The Logger.log('Got permits: ' + permits[0][0]); call works perfect no matter what. However, while I can pass a 1D array in this manner and it works fine, passing a 2D array is causing this to fail. Why does this happen? Am I using improper syntax for this?

Thanks in advance

1 Answer 1

2

Stringify it as a JSON object, then you can pass just a String, which works fine:

return JSON.stringify(permits);

And on HTML:

function onSuccess(numUnread) {
  numUnread = JSON.parse(numUnread);
Sign up to request clarification or add additional context in comments.

6 Comments

is this technically necessary? As in, is there no alternative? If so, why does using a 1d array work just fine but this doesn't?
What's the data in the permits? Number, string, dates...?
The data is coming from a google spreadsheet that contains numbers, dates, and strings
AFAIK, Dates are not a permitted type to pass trough Apps Script Servers and the Javascript Client. developers.google.com/apps-script/guides/html/reference/run, you can try to encode the date or stringfiy it.
Ahh thanks very much! I have confirmed that without the dates I can return the 2d array successfully. Is JSON the goto method then for handling dates?
|

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.