My script gets data from a CSV file and then exports it to a Google Sheet. the CSV file consists of a 2D array and has 9000 + rows. My current script runs fine but give an error of
Incorrect Range Width, was 1 but should be 5
This error only occurs if I process the whole CSV file, whereas if I separate the file into chunks it processes fine without any errors using the same code. Therefore the error is with the amount of rows. I use a loop feature it times out.
I was wondering if there is a way to run the code to process 2000 rows quit and then start again without any interruptions. I have been stuck on this error for several weeks and really need help. Thank you
Here is my code:
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 );
}
I believe your delimiter is a semicolon.
Here's how to import and write to the currently open sheet
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);
}