Question

I'm using the csvToArray() compact version from this question and answer to write all of the rows as a single array to a range in a spreadsheet:
Access, parse, and write .csv data - google apps script

My issue is that I'm ending up with an empty array for the last "row" in the array after it has parsed my data, and it's causing an error to throw on the setValues line:

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

The error I am getting is "Incorrect Range size, was 1, but should be 4. I've narrowed it down to the last "row" by looking at the debugger, which shows the last array in the set with as [""].

I've tried to parse arrData with:

    for (row in arrData){if(arrData[row] !=""){return(arrData[row])}}

And other similar methods (for if, while, etc), but can't quite figure out how to stop adding values to arrData if the row is blank.

Any suggestions?

Was it helpful?

Solution

I added this block of code in the lines after csvToArray() was called, but I imagine that it would have been just as valid to add a variation of it just prior to the return arrData in the csvToArray() function:

  var csvAdd = [];
  for (var row in csvData){
    if (csvData[row] != ''){
      csvAdd.push(csvData[row]);
    }

  }
  Logger.log(csvAdd);
  cisSheet.getRange(1,1,csvAdd.length,csvAdd[0].length).setValues(csvAdd);

Leaving the question here if anyone else ever has a similar problem in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top