Google Web 应用程序脚本加载时出现未知参数错误

Google Web App Script Unknown Parameter Error on Load

加载时我的 web app 产生了这个错误:

DataTables warning: table id=data-table - Requested unknown parameter '9' for row 21, column 9. For more information about this error, please see http://datatables.net/tn/4

Code.gs

    function doGet() {
      return HtmlService.createTemplateFromFile('Index').evaluate();
    }
     
    //GET DATA FROM GOOGLE SHEET AND RETURN AS AN ARRAY
    function getData(){
      var spreadSheetId = "1VzHY8fTq8OsXhpHYHESSSPxeVNOnqxpjcsyWJpbuEOs"; //CHANGE
      var dataRange     = "Base Stats!A2:L"; //CHANGE
     
      var range   = Sheets.Spreadsheets.Values.get(spreadSheetId,dataRange);
      var values  = range.values;
     
      return values;
    }
     
    //INCLUDE JAVASCRIPT AND CSS FILES
    function include(filename) {
      return HtmlService.createHtmlOutputFromFile(filename)
          .getContent();
    }

Index.html

    <!DOCTYPE html>
    <html>
      <head>
        <base target="_top">
        <!--INCLUDE REQUIRED EXTERNAL JAVASCRIPT AND CSS LIBRARIES-->
        <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
        <script src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.23/js/dataTables.bootstrap4.min.js"></script>
        <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.23/css/dataTables.bootstrap4.min.css">
     
        <?!= include('JavaScript'); ?> <!--INCLUDE JavaScript.html FILE-->
      </head>
     
      <body>
        <div class="container">
          <br>
          <div class="row">
            <table id="data-table" class="table table-striped table-sm table-hover table-bordered">
              <!-- TABLE DATA IS ADDED BY THE showData() JAVASCRIPT FUNCTION ABOVE -->
            </table>
          </div>
        </div>  
      </body>
    </html>

JavaScript.html

    <script>
      /*
      *THIS FUNCTION CALLS THE getData() FUNCTION IN THE Code.gs FILE, 
      *AND PASS RETURNED DATA TO showData() FUNCTION
      */
      google.script.run.withSuccessHandler(showData).getData();
     
      //THIS FUNCTION GENERATE THE DATA TABLE FROM THE DATA ARRAY
      function showData(dataArray){
        $(document).ready(function(){
          $('#data-table').DataTable({
            data: dataArray,
            //CHANGE THE TABLE HEADINGS BELOW TO MATCH WITH YOUR SELECTED DATA RANGE
            columns: [
              {"title":"Date Added"},
              {"title":"SpotRacer"},
              {"title":"Brand"},
              {"title":"Model"},
              {"title":"Acceleration"},
              {"title":"Speed (MPH)"},
              {"title":"Speed (KPH)"},
              {"title":"Handling (%)"},
              {"title":"Star Rating"},
              {"title":"Comments"},
              {"title":"Score (Cumlative)"},
              {"title":"Score (Weighted)"}
            ]
          });
        });
      }
    </script>

我不确定是什么导致了特定行和列的错误,但可能与未显示纯文本的列有关?第 9 列是 'Star Rating'.

Google Sheet: SpotRacers Fanbase Database

在您的脚本中,使用了 Sheets.Spreadsheets.Values.get 张 API 张。在这种情况下,检索到的值是二维数组。但是,例如,当所有行都没有被单元格值嵌入时(例如,第一行的值在“A”、“B”、“C”列中,第二行的值在列中“A”和“B”),所有行的长度都不同。我很担心这种情况。所以,我认为你的问题的原因可能是由于这个。

如果我对你当前问题的理解是正确的,那么下面的修改怎么样?

发件人:

function getData(){
  var spreadSheetId = "###"; //CHANGE
  var dataRange     = "Base Stats!A2:L"; //CHANGE
 
  var range   = Sheets.Spreadsheets.Values.get(spreadSheetId,dataRange);
  var values  = range.values;
 
  return values;
}

收件人:

function getData(){
  var spreadSheetId = "###"; // Please set your Spreadsheet ID.
  var sheet = SpreadsheetApp.openById(spreadSheetId).getSheetByName("Base Stats");
  var values = sheet.getRange("A2:L" + sheet.getLastRow()).getDisplayValues();
  return values;
}
  • 在此修改中,使用 getDisplayValues() 检索值。并且,数据是从数据范围中检索的。

注:

参考: