Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
like image 474
s2000coder Avatar asked Jul 03 '18 20:07

s2000coder


1 Answers

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"

like image 177
Alan Wells Avatar answered Oct 03 '22 20:10

Alan Wells