2

I would like to return some array I made in my code.gs to my HTML sidebar and use it to fill a select, I have this so far:

Lets say I would like to use "['this', 'part', 'of', 'the', 'array', 'for', 'the', 'select']" for the html select:

code.gs

function ExampleArray(){
var tempArr = [['this', 'part', 'of', 'the', 'array', 'for', 'the', 'select'], []];
return tempArr;
}

So that is the array, I need that array to populate a html select object so I need a HTML page as well. This is my HTML code for the select:

<script> 
google.script.run.ExampleArray();
</script>    
<div>
         <?
            var data    =  //the array from the code.gs
         ?>
       <div class="two">
       <select id="Select1">
       <? for (var i = 0; i < data.length; ++i) { ?>
       <option><?!= data[i] ?></option>
       <? } ?>
       </select>
       </div>

How can I achieve this? :)

BjörnBogers
  • 161
  • 3
  • 16

1 Answers1

2

You can either use successHandler or just call the script, as such:

google.script.run.withSuccessHandler(onSuccess).ExampleArray();

function onSuccess( values ){
  $.each(values, function(key, value) {   
     $('#Select1')
         .append($("<option></option>")
         .attr("value",key)
         .text(value)); 
  });
}

or

<?
  var data = ExampleArray();
?>

I always use the first method for my codes, I think it has a better control over the application. But that's just an opinion.

Kriggs
  • 3,731
  • 1
  • 15
  • 23
  • Thanks for your response, I tried your first method. I can see the code reaching the onSucces function (I put an alert in there and it showed) but after that nothing happends. When I: alert(values); it says: 'this', 'part', 'of', 'the', 'array', 'for', 'the', 'select'. What goes wrong here? – BjörnBogers Mar 19 '15 at 12:51
  • That's a Jquery example, have you included it? `` – Kriggs Mar 19 '15 at 12:54
  • I did this: ` ` – BjörnBogers Mar 19 '15 at 13:00
  • Wow I missed a $ ... I'm sorry for wasting your time man. Thanks for your solution, it works great! – BjörnBogers Mar 19 '15 at 13:14