B2:B4 matches exactly given range:
Try range.getA1Notation()
:
function myFunction(){
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const range = sh.getRange('B2:B4'); //given range
if (range.getA1Notation()=='B2:B4'){
var vals = range.getValues().flat(); //flat is optional but it returns 1D list
}
Logger.log(vals)
}
B2:B4 is within given range:
As Cooper suggested in his comment, use getRow(), getColumn(), getWidth(), getHeight():
function myFunction(){
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const range = sh.getRange('B1:B5'); //given range
if(range.getRow()<=2 && range.getColumn()<=2 && range.getWidth()>=1 && range.getHeight()>=3){
var vals = sh.getRange('B2:B4').getValues().flat(); //flat is optional but it returns 1D list
}
Logger.log(vals)
}
References: