2
votes

I am currently working on a conditional formatting color scale script for Sheets and I am stuck at selecting the ranges to be colored. It works fine if I only select one arbitrary large range, but not if I am highlighting two or more separate ranges on one sheet. In this case, my function is only applied to the range selected at last. Here is a simple example of a function: It takes the active range and sets the colors of the cells red.

function red() {
var curRange = SpreadsheetApp.getActiveRange();  
  curRange.setBackgroundColor("#FF0000");      
}

How can this be applied to all selected ranges, when multiple ranges or separate? Something like getActiveRanges(). Thanks

2
Good question! I am very curious about this one. I know they recently added the multiple highlight feature to the UI. Currently I do not see support for this on the google app script side. If I come across anything I will update. - nwill001
Awesome, thanks a lot! - user3691205

2 Answers

1
votes

Maybe getActiveRangeList() would suit your use case

From the docs:

Returns the list of active ranges in the active sheet or null if there are no ranges selected. The active range containing the current highlighted cell is placed last in the list. If there is a single range selected, this behaves as a getActiveRange() call.

// Returns the list of active ranges.
var rangeList = SpreadsheetApp.getActiveRangeList();
0
votes

Unfortunately a range only refers to a group of adjacent cells. If you are selecting multiple ranges you have a Selection Class but that doesn't have nearly as much functionality as the Range Class.

Ranges vs Selections Selection Class Documentation