Google 电子表格脚本,来回传递数组

Google spreadsheets scripts, passing arrays back and forth

我似乎无法执行这个简单的操作,我想知道我是否只是遗漏了一些明显的东西。我正在尝试将数组发送回 HTML 文件,在 code.gs 文件中它被正确创建并且我可以按预期迭代它但是当我将它传回 html文件,它失败了。

我做错了什么吗?

我的 index.html 文件的内容:

<script>
  google.setOnLoadCallback(draw);

  function draw() 
  {
    var rows = google.script.run.callTo();
    alert("this will get called");
    for(i = 0; i < rows.length; i++)
    {
      alert(rows[i]);
      alert(i);
      alert("this will never get called");
    }
  }
</script>

我的 Code.gs 文件的内容:

function onOpen()
{
    SpreadsheetApp.getUi()
    .createMenu('Open this')
    .addItem('Show', 'doGetHtml')
    .addToUi();
}

function callTo()
{
    var empty_array = ['one', 'two', 'three', 'four'];
    for(i = 0; i < empty_array.length; i++)
    {
        //Browser.msgBox("item2 is " + empty_array[i]);
    }
    return empty_array;
}

function doGetHtml() 
{
    html = HtmlService.createHtmlOutputFromFile('index')
    .setSandboxMode(HtmlService.SandboxMode.IFRAME)
    .setHeight(400)
    .setWidth(1024);

    SpreadsheetApp.getUi() 
    .showModalDialog(html, 'Show this message');
}

我相信您只能通过将服务器端函数返回的值传递给成功处理程序来访问它:https://developers.google.com/apps-script/guides/html/reference/run

<script>
  function draw(rows) 
  {
    alert("this will get called");
    for(i = 0; i < rows.length; i++)
    {
      alert(rows[i]);
      alert(i);
      alert("this will hopefully get called");
    }
  }

  google.script.run.withSuccessHandler(draw).callTo();
</script>