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());
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With