简体   繁体   English

将数组从Code.gs传递到Javascript Google App脚本

[英]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.

相关问题 Google Apps脚本,将两个(变量)2个“变量”或“数组”从code.gs传递给index.html / Javascript - Google apps script, pass (two) 2 “variables” or “array” from code.gs to index.html / Javascript 在 Google App Script 中将变量从 Code.gs 传递到 html - Passing variable from Code.gs to html in Google App Script 从 code.gs Google App Script 内部获取变量值到 Javascript - Get Variable Value from inside code.gs Google App Script to Javascript 如何将元素值变量从 Javascript html 文件传递到 google apps 脚本中的 code.gs 文件? - How to pass an element-value variable from Javascript html file to code.gs file in google apps script? 如何将变量从 Code.gs 传递到包含的脚本片段 - How to pass variable from Code.gs to included script snippet 在Google App脚本上将数组输入值传递给code.gs - Passing array input values to code.gs on google app scripting Google 脚本从 code.gs 返回到 index.html - Google script return from code.gs to index.html Code.gs中的Google Apps脚本返回对象 - Google Apps Script return object in Code.gs 使用Google Apps脚本将对话框中输入字段的值返回到code.gs中的var - return value from input field in dialog box to var in code.gs using Google Apps Script 使用 html 对话框中输入字段的值使用 Google Apps 脚本在 code.gs 中进行 var - use value from input field in html dialog box to var in code.gs using Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM