gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:59:30 26 4
gpt4 key购买 nike

我想将我在 code.gs 中制作的一些数组返回到我的 HTML 侧边栏并使用它来填充选择,到目前为止我有这个:

假设我想对 html 选择使用“['this', 'part', 'of', 'the', 'array', 'for', 'the', 'select']”:

代码.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();
?>

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

关于javascript - 如何将数组(Google 应用程序脚本)返回到 HTML 侧边栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29140053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com