s2000coder
s2000coder

Reputation: 941

What's the difference between getActiveRange() and getSelection()?

It seems like they both do the same thing, which is returning the selected range in an active "opened" sheet. What am I missing here? Are there cases where you need to use getSelection()?

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveRange();    
var selection = sheet.getSelection();

// These return the same values
Logger.log("Range Values: %s", range.getValues());
Logger.log("Selection Values: %s", selection.getActiveRange().getValues());

// These also return the same values
Logger.log("Sheet Current Cell: %s", sheet.getCurrentCell().getValue());
Logger.log("Selection Current Cell: %s", selection.getCurrentCell().getValue());

Upvotes: 7

Views: 1187

Answers (2)

Sage
Sage

Reputation: 1

getActiveRangeList() seems to be actually needed to get non-adjacent ranges in the end, it seems that we can use it with both of selection and active range. thus there is no difference or why we use it until now to me imo.

Upvotes: -1

Alan Wells
Alan Wells

Reputation: 31300

According to the documentation, there is a difference.

getActiveRange() returns the Class Range, but getSelection() returns the Class Selection. The difference being that a selection can be non-adjacent ranges.

So, if you need to get selected non-adjacent ranges, then you'd need to use getSelection() A range can only be a "group of adjacent cells in a sheet"

Upvotes: 6

Related Questions