GAS在HTML服务中检索电子表格数组

时间:2014-10-21 00:34:51

标签: google-apps-script

我正在尝试从电子表格中检索一行,并在按下按钮时将其附加为html元素。关于为什么我的代码不起作用的任何建议?我之前尝试过这个 - Retrieve cell date from a google spreadsheet to HTML service - 解决方案,但我可能没有正确实现它。感谢。

的index.html

<div id='vendordiv'>
      <p>
      <select>
        <option vlaue='abc'>abc</option>
        <option vlaue='xyz'>xyz</option>
      </select>
      </p>
      <p>
      <input type="Button" value="Get Rows"     onclick="google.script.run.withSuccessHandler(add_rows).rowArray()"/>
      </p>
    </div>

    <div id ='transdiv'>

    </div>

    <script>

    function add_rows(rowArray) {

     var parent = document.getElementById('transdiv');
     var child = document.createElement('p');
     var text = rowArray[1];
       child.innerHTML = text;
       parent.appendChild(child);
}
</script>

Code.gs

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index');
}

function rowArray() {
var ssID = "1pYBdbrCQWrY9haQP-d2G8GrytUJdtHLVLSOth02IwUc";
var sourceSheet = SpreadsheetApp.openById(ssID).getSheetByName("APAll");
return sourceSheet.getDataRange();
}

1 个答案:

答案 0 :(得分:1)

在rowArray函数中,您将返回Range对象而不是值本身。 范围表示您正在处理的单元格,但您必须调用getValues()以返回单元格内容的数组。

function rowArray() {
    var ssID = "1pYBdbrCQWrY9haQP-d2G8GrytUJdtHLVLSOth02IwUc";
    var sourceSheet = SpreadsheetApp.openById(ssID).getSheetByName("APAll");
    return sourceSheet.getDataRange().getValues();
}

参见https://developers.google.com/apps-script/reference/spreadsheet/sheet#getDataRange() https://developers.google.com/apps-script/reference/spreadsheet/range#getValues()