Home > database >  Range not found when passing argument in getRangeList
Range not found when passing argument in getRangeList

Time:03-11

I'm trying to change the font of some ranges in bold

To gain time I want to use the function getRangeList to modify all ranges at the same time

I detect the list of ranges to modify with a loop and concatenate them in a variable like this:

for(i = 0; i <= mytab.length -1; i  ){
    if(condition){
      if(coord == ''){
        coord = coord   '\'A'   i   '\''   ', \'F'   i   '\'';
      }
      else{
        coord = coord   ', \'A'   i   '\''   ', \'F'   i   '\''; 
      }
    }
  }

The Logger.log(coord) looks as follow:

10:56:48 AM Notice  Execution started
10:56:58 AM Info    'A3', 'F3', 'A6', 'F6', 'A8', 'F8', 'A12', 'F12', 'A18', 'F18'

The issue appears when I try to use my coord variable with getRangeList

sheet.getRangeList([coord]).setFontWeight('bold');

returns "Exception: Range not found"

But when I manually write all of the values in the function it works.

sheet.getRangeList(['A3', 'F3', 'A6', 'F6', 'A8', 'F8', 'A12', 'F12', 'A18', 'F18']).setFontWeight('bold');

I think the issue might come from the commas not treated the same in a string variable and in the function itself but I can't manage to solve the problem

CodePudding user response:

Use array and push instead of string and appending the range. String looking like array is still a string and will not make it an array just because you were able put it inside between brackets.

coord = [];
for(i = 0; i <= mytab.length -1; i  ){
  if(condition){
    coord.push(`A${i}`)
    coord.push(`F${i}`)
  }
}
// use coord directly as it is an array now
sheet.getRangeList(coord).setFontWeight('bold');

In your code, Range is not found because it is invalid.

'A3', 'F3', 'A6', 'F6', 'A8', 'F8', 'A12', 'F12', 'A18', 'F18' is counted as one element and is not a valid range.

Note:

  • No need to use else coord has contents or not.
  • Related