javascriptgoogle-apps-scriptgoogle-sheets

How to test type of range parameter in Google Spreadsheet script?


I'm just starting tinkering with scripts for Google Spreadsheet and I have a problem :

How to test if the type of a function's parameter is a range of cells ?

I'd like to do something like this :

if(typeof intput != "range") {
  throw "input must be a range";
}

From Google's examples here (middle of the page) :

if (typeof inNum != "number") {  // check to make sure input is a number
  throw "input must be a number";  // throw an exception with the error message
} 

So this seems to be the right way to test the type of a variable. But I don't know how to test if the type is a range of cells.

It'd be even better if I could specify if the range is one or two dimensions.


Solution

  • 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