0

I have an html page that will be served to a google sheet app to be used as a UI. I would like to access an array from a server side function within the html file. I am having trouble accessing a returned array. Here is what I have:

in html file:

<div id="id1">
   Starting 1
</div>

<div id= "id2">
   Starting 2
</div>

<script type="text/javascript">
   document.getElementById("id1").innerHTML = "A change";
</script>


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

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

In code.gs:

function getPermits()
{
  var permits = [];

  for(var i = 0; i < 10; i++)
  {
     permits.push('Element ' + i);
  }

  return permits;
}

Right now I am just trying to figure out why the div with id = "id2"

does not get changed to the first element from the passed array. Instead, it is not changed. Also, there is no alert. If I change the return of the gePermits() function to a string, both the div and the alert work as I would expect.

Thanks in advance!

1
  • Your code runs well on my end. I can see both the alert and the div with id = "id2" get changed. See the code on my end here: script.google.com/macros/d/… Commented Mar 18, 2015 at 18:05

1 Answer 1

1

Some types are not passed trough HTMLService, but you can always STRINGFY and PARSE it, try:

return JSON.stringify(permits);

and in the html:

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

1 Comment

I can not even seem to pass a string. Or at least can't seem to access it on the html side.

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.