如何将数组(Google应用程序脚本)返回到HTML侧边栏

How to return an array (Google apps script) to a HTML sidebar?

本文关键字:返回 HTML 侧边栏 脚本 应用程序 数组 Google      更新时间:2023-09-26

我想把我在code.gs中创建的一些数组返回到我的HTML侧边栏,并用它来填充一个选择,到目前为止我已经有了:

假设我想使用"[this','part','of','the','array','for','the','select']"作为html选择:

代码.gs

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

这就是数组,我需要这个数组来填充html选择对象,所以我也需要一个html页面。这是我选择的HTML代码:

<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>

我怎样才能做到这一点?:)

您可以使用successHandler,也可以直接调用脚本,例如:

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

<?
  var data = ExampleArray();
?>

我的代码总是使用第一种方法,我认为它对应用程序有更好的控制。但这只是一种观点。