Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将一个数组从Code.gs传递到Javascript Google应用程序脚本_Javascript_Google Apps Script - Fatal编程技术网

将一个数组从Code.gs传递到Javascript Google应用程序脚本

将一个数组从Code.gs传递到Javascript Google应用程序脚本,javascript,google-apps-script,Javascript,Google Apps Script,我有一个函数,它返回Code.GS文件中的一个对象数组,我想将该数组传递到index.html文件中的一个变量,以便在JavaScript中使用。但是由于某些原因,变量没有被更新,在我看来,onSuccess函数似乎没有运行 Code.gs文件 function getData(){ var dates = [{ title : 'obj', time : '2018-07-13T',

我有一个函数,它返回Code.GS文件中的一个对象数组,我想将该数组传递到index.html文件中的一个变量,以便在JavaScript中使用。但是由于某些原因,变量没有被更新,在我看来,
onSuccess
函数似乎没有运行

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文件:

<script>
 var dates;

function onSuccess(array) {
   dates = array;
}
google.script.run.withSuccessHandler(onSuccess).getData();
<!-- code that requires dates array-->
</script>

风险值日期;
函数onSuccess(数组){
日期=数组;
}
google.script.run.withSuccessHandler(onSuccess.getData();

All
google.script.run
调用是异步的,这意味着函数从应用程序脚本服务器获取响应需要时间。同时,需要dates数组的代码是同步的,并且在dates变量更新之前执行。因此,需要dates数组的代码可能位于
onSuccess
处理程序中


如果您真的想变得更花哨,您甚至可以利用承诺使您的代码异步并同时具有顺序可读性

请添加一个。谢谢,现在我了解了这个问题