从Google工作表中获取列表以填充html中的对话框

时间:2015-03-08 22:28:55

标签: google-apps-script

我有一个Google表格中的项目列表。我想使用该表中的数据来通知下拉列表,允许用户从该列表中选择一个项目。我可以让列表在脚本中弹出,但无法弄清楚如何将其返回到html对话框中。 这是我的.gs代码:

function getPoList(){
  var fpa = SpreadsheetApp.openById('__WORKING ID___');
  var poSheet = fpa.getSheetByName('testSheet');
  var poVendor = poSheet.getRange('testVendor');
  var poPo = poSheet.getRange('testPo');
  var poVendorList = poVendor.getValues();
  var poPoList = poPo.getValues();     

  var poList = '';
  for (var i = 0; i < poPo.getNumRows(); i++) {
     poList += "<option value=" + poVendorList[i] + ": " + poPoList[i] + ">" + poVendorList[i] + ": " + poPoList[i] + "</option>";
     return poList;    
  }            
}

这是我的.html代码:

   <script type="text/javascript">
google.script.run.withSuccessHandler(onSuccess).getPoList(poList);
  var div = document.createElement('div');
  div.className = 'newClass';
  div.innerHTML = '<select>' + poList + '</select>';
  document.body.appendChild(div); 
</script> 

1 个答案:

答案 0 :(得分:0)

您似乎没有为withSuccessHandler(onSuccess)编写函数。

应该是这样的:

<script type="text/javascript">
  google.script.run
    .withSuccessHandler(onSuccess)
    .getPoList(poList);

  function onSuccess(argReturnedHTML) {
    var div = document.createElement('div');
    div.className = 'newClass';
    div.innerHTML = '<select>' + argReturnedHTML + '</select>';
    document.body.appendChild(div);
  }
</script>

此外,.getValues()返回二维数组。我不认为你正在处理这个问题。

二维数组:

[ ['row one cell one value', 'row one cell two value'], ['row two cell one value', 'row two cell two value'] ]

要获取内部数组的内部值,您需要使用两个索引号:

poVendorList[i][j]

其中i是表示电子表格中每一行的每个内部数组的索引,j是行中每个单元格的值。

poVendorList[rowIndex][cellInRowIndex]

您不需要同时使用这两个索引,但代码需要以某种方式首先获取内部数组,然后从内部数组中获取值。