从doGet()内部从Google电子表格中提取数据

时间:2016-10-26 14:16:31

标签: google-apps-script google-apps google-spreadsheet-api

由于SpreadsheetApp.openById(" sheetid")是某些已弃用或不再有效的内容https://code.google.com/p/google-apps-script-issues/issues/detail?id=5174。什么是将电子表格数据拉入doGet()方法的替代方法。我一直在尝试按照本教程https://www.youtube.com/watch?v=3deomYqHKgA

1 个答案:

答案 0 :(得分:0)

您还可以使用JavaScript onload函数来代替检索电子表格数据。 试试这个index.html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
  <script>
    function onLoadFunction() 
    {
      google.script.run.withSuccessHandler(onSuccessData).getData();
    } 
    window.onload = onLoadFunction;
     
    function onSuccessData(rawdata) 
    {
      var div = document.getElementById('result');
      div.innerHTML += '<BR>' + rawdata; 
    }
</script>
<div id="result"><b>result:</b><br></div>
  </body>
</html>

和Code.gs:

var SHEET_ID = 'YOUR_SHEET_ID';

function getData() {
  var sheet   = SpreadsheetApp.openById(SHEET_ID);
  var dataRange = sheet.getDataRange();
  var values = dataRange.getValues();
  return values;
}

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

该功能有效,未提及被弃用: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#openbyidid