首页 > 解决方案 > 数组不能在数据表中使用

问题描述

我试图通过使用带有数组的数据表来制作表格,但不知何故它没有在我的 html 文件中显示表格。该数组在我的 gs 文件中定义,如下面的代码所示。

这是一项简单的工作,但我仍然不确定它出了什么问题。

var ssId = 'xxxxxxxxxxxxx';
var ss = SpreadsheetApp.openById(ssId);
var indexPage_sheetName = 'xxxxxxxx';
var valuesFromIndexPage = ss.getSheetByName(indexPage_sheetName).getDataRange().getValues();//array of 850rows×15cols
valuesFromIndexPage.shift();

function getData() {
  $(document).ready(function(){
    $("#foo-table").DataTable({
      data: valuesFromIndexPage
    });
  }); 
}
<html>
<head>
<script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css"/> 
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>

<body>
<table id="foo-table" class="display" width="100%"></table>
</body>
</head>
</html>

@ZektorH 这是运行我的代码的控制台日志。

userCodeAppPanel:9 Uncaught TypeError: Cannot read property 'slice' of null
    at initializeTable (userCodeAppPanel:9)
    at af (4105580746-mae_html_user_bin_i18n_mae_html_user__ja.js:67)
    at 4105580746-mae_html_user_bin_i18n_mae_html_user__ja.js:10
    at ng.J (4105580746-mae_html_user_bin_i18n_mae_html_user__ja.js:94)
    at Hd (4105580746-mae_html_user_bin_i18n_mae_html_user__ja.js:42)
    at Dd (4105580746-mae_html_user_bin_i18n_mae_html_user__ja.js:43)
    at Bd.b (4105580746-mae_html_user_bin_i18n_mae_html_user__ja.js:39)

我再次查看了我的数据,发现console.log 上的数据变为空(但是当我在Logger.log 上看到它时它有数据)。我在下面发布我所做的和得到的。

function getData() {
  Logger.log(valuesFromIndexPage); //the array is in valuesFromIndexPage
  return valuesFromIndexPage;
}
        function initializeTable(data) {
        console.log(data); //it returns null here...
            var aDataSet = data.slice(1);

[19-10-31 09:47:00:116 JST] [[ID,案件名,.......

@ZektorH 这些是没有数据的全部代码。

代码.gs

var ssId = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
var ss = SpreadsheetApp.openById(ssId);

var indexPage_sheetName = 'xxxxxxxxxxxxxx';
var valuesFromIndexPage = ss.getSheetByName(indexPage_sheetName).getDataRange().getValues();


function createSidebar() {
SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutputFromFile('index').setTitle('My custom sidebar').setWidth(300))
}

function getData() {
  return valuesFromIndexPage;
}



function doGet(e) {
      return HtmlService.createTemplateFromFile('index').evaluate().setTitle('title');
}

索引.html

<!DOCTYPE html>
<html>

<head>
    <base target="_top">
    <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css">
    <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js"></script>
    <script>
        $(document).ready(function() {
            console.log("ready!");
            google.script.run.withSuccessHandler(initializeTable).getData(); //calls the getData funciton from Apps Script and returns the results to the initializeTable function
        });

        function initializeTable(data) {
            console.log(data)
            var aDataSet = data.slice(1); // all except header
            var head = []; // headers
            data[0].forEach(function(e) {
                head.push({
                    'sTitle': e
                });
            });
            $('#foo-table').dataTable({
                "aaData": aDataSet,
                "aoColumns": head
            });
        }
    </script>
</head>

<body>
    <table id="foo-table" class="display" width="100%"></table>
</body>

</html>

标签: javascriptjqueryhtmlgoogle-apps-scriptdatatables

解决方案


columns在 dataTable 中使用of 键更改表头和$("#foo-table").DataTable$("#foo-table").dataTable

var valuesFromIndexPage=[{"free-text-c1":"free-text-r1","c2":"r1","c3":"r1","c4":"r1","c5":"r1","c6":"r1","c7":"r1","c8":"r1","c9":"free-text-r1","c10":"free-text-r1"},{"free-text-c1":"free-text-r2","c2":"r2","c3":"r2","c4":"r2","c5":"r2","c6":"r2","c7":"r2","c8":"r2","c9":"free-text-r2","c10":"free-text-r2"}];

valuesFromIndexPage.shift();

function getData() {
  $(document).ready(function(){
    $("#foo-table").dataTable({
      destroy: true,
      scrollX: true,
      data: valuesFromIndexPage,
      columns: _.keys(valuesFromIndexPage[0]).map((key) => { return { "title": key, "data": key } })
    });
  }); 
}

getData()
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
<html>
<head>
<script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.1.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css"/> 
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>

<body>
<table id="foo-table" class="display" width="100%"></table>
</body>
</head>
</html>


推荐阅读