[英]Pass an Array from Code.gs to Javascript Google App Script
I have a function that returns an array of objects in my Code.GS file that I want to pass onto a variable in my index.html
file to use in JavaScript. 我有一个函数,该函数返回Code.GS文件中的对象数组,该数组要传递到
index.html
文件中的变量中以在JavaScript中使用。 But for some reason, the variable is not being updated, and it seems to me the onSuccess
function is not being run. 但是由于某种原因,该变量没有被更新,在我看来
onSuccess
函数没有被运行。
Code.gs file Code.gs文件
function getData(){
var dates = [{
title : 'obj',
time : '2018-07-13T',
color: '#C2185B'
},
{
title : 'obj2',
start : '2018-07-19',
end : '2018-07-20',
},
];
return dates;
}
index.html file: index.html文件:
<script>
var dates;
function onSuccess(array) {
dates = array;
}
google.script.run.withSuccessHandler(onSuccess).getData();
<!-- code that requires dates array-->
</script>
All google.script.run
invocations are asynchronous, meaning it takes time for the function to get back a response from the Apps Script server. 所有
google.script.run
调用都是异步的,这意味着该功能需要一段时间才能从Apps Script服务器获取响应。 Meanwhile, the code that requires the dates array is synchronous and is executing before the dates variable gets updated. 同时,需要使用dates数组的代码是同步的,并且在dates变量更新之前就已执行。 So the code that requires the dates array should probably live inside your
onSuccess
handler. 因此,需要dates数组的代码可能应该存在于
onSuccess
处理函数中。
If you really want to get fancy you can even leverage Promises to make your code asynchronous and sequentially readable at the same time. 如果您真的想花哨的话,甚至可以利用Promises来使您的代码同时异步并顺序可读。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.