电子表格的内容未显示在已部署的Apps脚本Webapp中

时间:2018-07-11 12:18:00

标签: html google-apps-script

我尝试做一个3rd tutorial on this ctrlq page的示例,在该示例中,我们在网页上显示了电子表格的内容。这是我刚刚输入的文件-我没有激活任何东西,只是做了 Publish -> 部署为网络应用

我希望看到所有spreadsheet's values,但是什么也没显示: enter image description here

Code.gs

function doGet() {
  var html = HtmlService.createTemplateFromFile("index").evaluate();
  html.setTitle("Dynamic Webpage");
  return html; 
}

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename)
      .setSandboxMode(HtmlService.SandboxMode.IFRAME)
      .getContent();
}

function getData(){
  var sheet = SpreadsheetApp.openById("1k_kj98U__0Bk44gh0qRFpaVx0ru3sN1pSPGiMQwimxo").getSheets()[0];
  return sheet.getDataRange().getValues();
}

index.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<?!= include('script_js'); ?>
<body>
 <div id="data"></div>
</body>
</html>

script_js.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<script>

    window.onload = function(){
      google.script.run.withSuccessHandler(showData).getData();
    }

    function showData(data){
      var html = "";
      for (var i=0; i<data.length; i++) {
        html += "<br>" + data[i].join(":");
      }
      document.getElementById("data").innerHTML = html;
    }

</script>
</body>
</html>

Link to the script project

1 个答案:

答案 0 :(得分:1)

编辑,如@tehhowch指出的his answer是此处的正确解决方案。

对于您的情况,应该进行以下更改,getData()

function getData(){
  var sheet = SpreadsheetApp.openById("1k_kj98U__0Bk44gh0qRFpaVx0ru3sN1pSPGiMQwimxo").getSheets()[0];
  return JSON.stringify(sheet.getDataRange().getValues()); // return value as Json
}

还有showData(data)

function showData(data) {
      var arr = JSON.parse(data);
      var html = "";
      for (var i=0; i < arr.length; i++) {
        html += "<br>" + arr[i].join(":");
      }
      document.getElementById("data").innerHTML = html;
    }