0

我正在使用 Google Apps 脚本导入 CSV 数据。所有 CSV 数据都已导入,但仍然出现此错误消息:

范围宽度不正确,为 1 但应为 5

我的 CSV 文件是用';'逗号分隔的,里面也有逗号。我的 CSV 文件中的示例:

FJ32-19A354-AA;'BES_301 - Exterior Trim;'000101;BOM No CAD;04/01/16

总共有 5 列,当我在getRange()调用中说明列数时,我收到另一个错误:

找不到方法 getRange(number,number,number,number,number)

我目前正在使用的示例代码(改编自“Ask Ben”):

var ss = SpreadsheetApp.openById('1V8YG8lyNZiTllEPHENcnabYRLDPCK6mHGUyAyNhW0Is'); // data_sheet_id = id of spreadsheet that holds the data to be updated with new report data
var s = SpreadsheetApp.getActiveSheet();
var csv = file.getBlob().getDataAsString();
var csvData = CSVToArray(csv); // see below for CSVToArray function
var lastrow = s.getLastRow(); 
  s.getRange(lastrow+ 1, 1,5, csvData.length, csvData[0].length).setValues(csvData);

function CSVToArray( strData, strDelimiter ){

    strDelimiter = (strDelimiter || ";");


    var objPattern = new RegExp(
        (
            // Delimiters.
            "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

            // Quoted fields.
            "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

            // Standard fields.
            "([^\"\\" + strDelimiter + "\\r\\n]*))"
        ),
        "gi"
        );


    var arrData = [[]];

    var arrMatches = null;

    while (arrMatches = objPattern.exec( strData )){

        // Get the delimiter that was found.
        var strMatchedDelimiter = arrMatches[ 1 ];

        // Check to see if the given delimiter has a length
        // (is not the start of string) and if it matches
        // field delimiter. If id does not, then we know
        // that this delimiter is a row delimiter.
        if (
            strMatchedDelimiter.length &&
            strMatchedDelimiter !== strDelimiter
            ){

            // Since we have reached a new row of data,
            // add an empty row to our data array.
            arrData.push( [] );

        }

        var strMatchedValue;

        // Now that we have our delimiter out of the way,
        // let's check to see which kind of value we
        // captured (quoted or unquoted).
        if (arrMatches[ 2 ]){

            // We found a quoted value. When we capture
            // this value, unescape any double quotes.
            strMatchedValue = arrMatches[ 2 ].replace(
                new RegExp( "\"\"", "g" ),
                "\""
                );

        } else {

            // We found a non-quoted value.
            strMatchedValue = arrMatches[ 3 ];

        }

        // Now that we have our value string, let's add
        // it to the data array.
        arrData[ arrData.length - 1 ].push( strMatchedValue );
    }

    // Return the parsed data.
    return( arrData );
}
4

2 回答 2

2

导入 CSV 文件时,行的长度会有所不同,这将导致此错误。您需要确保所有行的大小相同

首先,您需要一个函数来调整行的大小,从另一个 stackoverflow 主题复制:

function resize(array, newSize, defaultValue) {
    while(newSize > array.length)
        array.push(defaultValue);
    array.length = newSize;
}

然后你可以导入:

function readCSV(sheet, data) {

  var table = [];
  var lines = data.split("\n");
  var maxCol = 0;
  for (var l = 0; l < lines.length; ++l) {
    var line = lines[l];
    var cells = line.split(",");
    table.push(cells);

    if (maxCol < cells.length)
      maxCol = cells.length;
  }

  // Important!
  // resize all rows to be of the same column number, otherwise setValues will fail!
  for (var i = 0; i < table.length; ++i)
    resize(table[i], maxCol, '');


  sheet.getRange(1, 1, table.length, table[0].length).setValues(table);


  SpreadsheetApp.flush();
}
于 2017-12-29T19:14:49.303 回答
2

错误来自这行代码:

s.getRange(lastrow+ 1, 1,5, csvData.length, csvData[0].length).setValues(csvData);

括号内有 5 个值。 getRange()最多取4个参数。

.getRange(start Row, start Column, Number of Rows, Number of Columns)

删除数字5csvData.length

目前,csvData.length可能正在用于“列数”设置,而不是csvData[0].length.

当我CSVToArray()使用您的示例数据运行该函数时,我得到一个二维数组:

[["FJ32-19A354-AA", "'BES_301 - Exterior Trim", "'000101", "BOM No CAD", "04/01/16"]]

当我记录长度时:

Logger.log('arrData[0].length: ' + arrData[0].length);

CSVToArray()它返回正确的长度 5。因此,对于您提供的数据示例,该函数看起来工作正常。

现在,您在应该5在的地方有号码。number of rows但是外部数组中只有一个内部数组。这将理解错误消息。代码“认为”应该有 5 行,但只有 1 行。

于 2016-01-05T18:45:17.830 回答