0

According to this post Insert row between different data I'm able to insert an empty data row between some rows in a spreadsheet (based on a condition).

I'm trying to make this script adaptable based on the number of columns.

I mean, if I have 3 columns I have to write

newValues.push(['','','']);

If I have 5 columns (i.e.) I have to change in

newValues.push(['','','','','']);

I want to make this change automatic. I've tried with something like

var blankRow = ' " " ';

for (var x = 0; x<lastCol; x++){
blankRow = blankRow + ',""';
}

.....

newValues.push([blankRow]);

when I run the script gives me an error.

Any idea how can I do? Thanks!

Community
  • 1
  • 1
Andrea Volontè
  • 85
  • 1
  • 3
  • 10

1 Answers1

1

Please provide what (if any) error you are getting.

Seems like it would be a range error. If so, then it's most likely how you build your new array. At a glance, I suspect you are getting 1 column too much, because you start with 1 column already added, and your for loop starts off at 0, which means you get lastCol+1 length when you have lastCol range.

So if we follow your code, and you want to setValues() for 3 columns you have

Start: blankRow = ' " " '
x = 0 blankRow = ' " ", " " '
x = 1 blankRow = ' " ", " ", " " '
x = 2 blankRow = ' " ", " ", " ", " " '

so as you can see, you have a 3 column range, yet you are trying to set 4 columns worth of values. I also recommend getting rid of the " " in favour of "" as it will leave a blank value instead of a space in the cells.

Vytautas
  • 2,238
  • 1
  • 9
  • 20
  • I've fixed the for cycle (checked with Logger). When i try to run newValues.push([blankRow]); or newValues.push(blankRow); I get the error "uncorrect length of interval, it was 1 but it should be 5" (error message translated from italian). – Andrea Volontè Oct 07 '16 at 06:13