簡體   English   中英

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM