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!

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.