Mark Hatton
Mark Hatton

Reputation: 888

In Google Sheets, how do I check that a given cell is within a range?

I have a Google Script running in Google Sheets that assigns a value to the current cell. As part of the script, I want to validate that the current cell is part of a particular named range, as the script should only be run when in that area.

I can't find any API methods to validate whether a cell belongs to a range, or more generally, whether one range is encompassed by another. How do I do this? Do I need to write the algorithm manually?

Upvotes: 0

Views: 176

Answers (1)

Konstant
Konstant

Reputation: 2199

There seems to be no API method to check this. You could try something like this:

// find if checkRange is within inRange
function isSubset(checkRange, inRange) {
  return (checkRange.getRow() >= inRange.getRow()) && (checkRange.getColumn() >= inRange.getColumn()) && (checkRange.getRow() + checkRange.getNumRows() <= inRange.getRow() + inRange.getNumRows()) && (checkRange.getColumn() + checkRange.getNumColumns() <= inRange.getColumn() + inRange.getNumColumns()); 
}

Upvotes: 1

Related Questions