如何将数组(Google应用程序脚本)返回给HTML侧栏? [英] How to return an array (Google apps script) to a HTML sidebar?

查看:89
本文介绍了如何将数组(Google应用程序脚本)返回给HTML侧栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我在我的code.gs中创建的一些数组返回到我的HTML侧栏并使用它来填充选区,至此为止:



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



code.gs

 函数ExampleArray(){
var tempArr = [['this','part','of','','array','for',''','select'],[]];
return tempArr;





$ b

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

 < script> 
google.script.run.ExampleArray();
< / script>
< div>
<?
var data = //来自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(); 

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

 < ;? 
var data = ExampleArray();
?>

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


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? :)

解决方案

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.

这篇关于如何将数组(Google应用程序脚本)返回给HTML侧栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆