将数组从GAS脚本传递到html文件,Global J.S.变量

问题描述 投票:1回答:1

我有以下谷歌应用程序脚本,根据谷歌表的值填充数组,以及创建一个网络应用程序。我的代码的顶部已经过测试,工作正常。

function doGet() {
  return HtmlService.createHtmlOutputFromFile('FormFile')
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

function ChannelList() {

var InfoSheetIterator = DriveApp.getFilesByName("InfoSheets");
var InfoSheetFile = InfoSheetIterator.next();
var InfoSheet = SpreadsheetApp.open(InfoSheetFile);

var NumChannels = getLastRow('A');
var ChannelTwoDimArray = InfoSheet.getRange('A2:A'+String(NumChannels)).getValues();

return ChannelTwoDimArray;
}

然后我正在尝试在Javascript中将数组声明为全局变量,然后使用此代码将该数组用于其他目的。我得到一个空阵列,我不知道为什么。任何提示赞赏。

     <!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
<button type="button" onclick="tester()">Click Me!</button>
<p id = "tester"></p>
    <script>
    //declaring global variables
    window.onload = populateArrays;

    function getChannelList(value) {
    window.ChannelList = value;
    }




    function populateArrays() {
    google.script.run.withSuccessHandler(getChannelList).ChannelList();

    }

    function tester() {
    document.getElementById("demo").innerHTML = window.ChannelList[0][0];
    }

   </script>
  </body>
</html>

编辑:我尝试过的事情: - 添加多个脚本标签。 - 为打印ChannelList值设置超时。 - 全局变量 - 本地存储窗口变量

我在这里遗漏了一些东西。当我在getChannelList()方法中打印ChannelList的值时,它工作正常,但是当我尝试访问该范围之外的值时......没有这样的运气。

javascript google-apps-script google-apps
1个回答
1
投票

添加window.onload事件和成功处理程序

<script>
  window.getChannelList = function(rtrnValue) {//Runs AFTER the server function completes
    //ToDo - Process the return value
  }

  window.onload = function() {
    google.script.run
      .withSuccessHandler(getChannelList)//define the function to run on completion
      .ChannelList();//Call the server function

    console.log(window.ChannelList[0][0]);

  };


</script>
© www.soinside.com 2019 - 2024. All rights reserved.