How to test type of range parameter in Google Spreadsheet script?
A range of cells is just a Array (multidimensional array) Javascript has a problem in that way. Array's are seen as an object. So first check if you have the "object" type and then you could test like this.
if(typeof intput=="object"&&intput.length!=undefined) {
//input is a array
}else{
//Not a array
}
By testing a default property you can determine for certain that you have a array
A range may represent a single cell (e.g. 'A1'
) or group of cells (e.g. 'A1:A2'
).
A range is converted into a range value when it is passed as a custom function parameter (e.g. =processRangeVal(A1:A2)
).
If the range is a single cell then the range value is simply the data in that cell.
If the range is a group of cells then the range value is a 2-dimensional array. The first dimension is the rows and the second dimension the columns in each row.
To test for the range value representing a cell vs a group of cells:
function processRangeVal(rangeVal) {
if (Array.isArray(rangeVal[0])) {
// do 2d-array handling
} else {
// do cell data handling
}
}
rangeVal[0]
resolves to undefined
if the range is a single cell and the cell data does not support indexing. In this case Array.isArray(undefined)
resolves to false which is what we want.