Selecting a range from within a named range google sheets script

2998 views javascript
0

Is there any way to select a range specified from within another named range?

For example if I have a named range say "firstRange" and I want to get just the 2nd column from within that range to copy to another named range.

I can do this by copying cells individually with a for loop but it takes a long time.

Unfortunately getRangeByName("firstRange").getRange(1,1,2) is not valid as getRange is not a method of getRangeByName

Thanks

answered question

1 Answer

4

You can get the named range and then get the values which returns a multi-dimensional array. Then you can parse the values that you are looking for out of that. For example

var parsed = SpreadsheetApp.getActiveSpreadsheet()
    .getRangeByName('namedRange')
    .getValues()
    .map(function(row) {
      return [row[1]];
    });

This gets all the values from your named range and then maps over them and grabs the values from the second column (the 1 index in this example)

I hope that helps.

posted this

Have an answer?

JD

Please login first before posting an answer.