我的脚本从CSV文件中获取数据,然后将其导出到Google表格。 CSV文件由2D数组组成,有9000多行。我当前的脚本运行正常,但出现错误
范围宽度不正确,为1,但应为5
仅当我处理整个CSV文件时才会出现此错误,而如果我将文件分成块,则使用相同的代码处理没有任何错误。因此,错误与行数有关。我使用循环功能超时。
我想知道是否有办法运行代码来处理2000行退出然后再次启动而没有任何中断。几个星期以来我一直坚持这个错误,真的需要帮助。谢谢
这是我的代码:
Function getCSV() {
var fSource = DriveApp.getFolderById('0B2lVvlNIDosoajRRMUwySVBPNVE'); //reports_folder_id = id of folder where csv reports are saved
var date= Utilities.formatDate(new Date(), "GMT", "dd-MM-yy");
var fi = fSource.getFilesByName('L661_BOM-CAD_07-01-16.csv');
// latest report file
var ss = SpreadsheetApp.openById('1V8YG8lyNZiTllEPHENcnabYRLDPCK6mHGUyAyNhW0Is').getSheet s()[0]; // data_sheet_id = id of spreadsheet that holds the data to be updated with new report data Sheet will be opened server side.
ss.getName() == "Sheet1"
if ( fi.hasNext()) { // proceed if "report.csv" file exists in the reports folder
var file = fi.next();
//file.setName('L661_BOM-CAD_'+ date +'(EXPORTED)'+'.csv');
var csv = file.getBlob().getDataAsString();
var csvData = CSVToArray(csv);
Logger.log('csvData[0].length: ' + csvData[0].length + ' csvData.length:' + csvData.length);
var lastrow = ss.getLastRow();
ss.getRange(lastrow + 1,1,csvData.length,csvData[0].length).setValues((csvData));
}
//adds the last modified date to the first row
if( ss.getName() == "Sheet1" ) { //checks that we're on the correct sheet
var r= ss.getRange('A1');
if( r.getColumn() == 1 ) { //checks the column
var nextCell = r.offset(0, 5);
if( nextCell.getValue() === '' ) //is empty?
var date = new Date();
var date= Utilities.formatDate(new Date(), "GMT", "dd-MM-yy");
nextCell.setValue(date); //enters the date in F1 in dd/mm/yyyy format
};
};
};
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 );
Logger.log('arrData[0].length: ' + arrData[0].length);
}
// Return the parsed data.
return( arrData );
}
答案 0 :(得分:1)
我相信你的分隔符是分号。
以下是如何导入和写入当前打开的工作表
function getcsv(){
// get the data from drive
var csvString = DriveApp
.getFileById('0B92ExLh4POiZTHFDUThmaHE2d2s1LVpvbWhyNjJaOE1MejBZ')
.getBlob()
.getDataAsString();
// convert to array of arrays
var csvData = Utilities.parseCsv(csvString, ';');
// write to sheet
SpreadsheetApp
.getActiveSheet()
.getRange(1,1,csvData.length, csvData[0].length)
.setValues(csvData);
}
答案 1 :(得分:0)
我找不到代码中行的循环,但基本上你可以做类似的事情(你需要var rowcount = 0
每行增加的值):
if (rowcount == 2000) {
console.log("found row 2000");
}
然后在console.log()
上设置断点。