我正在使用Google脚本导入CSV数据。将导入所有CSV数据,但仍会出现此错误消息:
不正确的范围宽度,是1但应该是5
我的CSV文件使用';'分隔,其中也有逗号。示例来自我的CSV文件:
FJ32-19A354-AA;'BES_301 - Exterior Trim;'000101;BOM No CAD;04/01/16总共有5列,当我在getRange()调用中声明列数时,会得到另一个错误:
找不到方法getRange(编号,编号)
我目前使用的示例代码(改编自“问本”):
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 );
}发布于 2016-01-05 18:45:17
错误来自于这一行代码:
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)移除数字5或csvData.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);它返回正确的长度为5。因此,对于您提供的数据示例,CSVToArray()函数看起来工作正常。
现在,在5应该在的位置上有一个号码number of rows。但是外部数组中只有一个内部数组。这将使错误消息有意义。代码“认为”应该有5行,但只有一行。
发布于 2017-12-29 19:14:49
导入CSV文件时,行的长度会有所不同,这将导致此错误。您需要确保所有行都是相同大小的。
首先,您需要一个函数来调整行的大小,并从另一个堆栈溢出主题复制:
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();
}https://stackoverflow.com/questions/34607590
复制相似问题