If you liked Reporting file, function and line number in Apps Script, you’ll probably also like this one. This shows how to get some properties about an object in apps script, and can be useful for debugging.

Here’s how

This function is available in the Useful stuff library, or you can copy from here. It will return information about JavaScript primitives or objects

 /**
 * return an object describing what was passed
 * @param {*} ob the thing to analyze
 * @return {object} object information
 */
function whatAmI (ob) {

  try {
    // test for an object
    if (ob !== Object(ob)) {
        return {
          type:typeof ob,
          value: ob,
          length:typeof ob === 'string' ? ob.length : null 
        } ;
    }
    else {
      try {
        var stringify = JSON.stringify(ob);
      }
      catch (err) {
        var stringify = '{"result":"unable to stringify"}';
      }
      return {
        type:typeof ob ,
        value : stringify,
        name:ob.constructor ? ob.constructor.name : null,
        nargs:ob.constructor ? ob.constructor.arity : null,
        length:Array.isArray(ob) ? ob.length:null
      };       
    }
  }
  catch (err) {
    return {
      type:'unable to figure out what I am'
    } ;
  }
  }

Returned values for primitives

Not super useful, but just to complete the picture.

Logger.log(cUseful.whatAmI(1.7));
   {length=null, value=1.7, type=number}

  Logger.log(cUseful.whatAmI(true));
   {length=null, value=true, type=boolean}

  Logger.log(cUseful.whatAmI('some string'));
   {length=11, value=some string, type=string}

 and these special ones

  Logger.log(cUseful.whatAmI(null));
    {length=null, value=null, type=object}

  Logger.log(cUseful.whatAmI(NaN));
    {length=null, value=NaN, type=number}

  Logger.log(cUseful.whatAmI());
  {length=null, value=null, type=undefined}

Arrays

Of course Arrays are just a kind of object, and typeof [] will return ‘object’. You can see here that the name property returned will be ‘Array’ – so an Array is defined as type:object, name:Array

  Logger.log(cUseful.whatAmI([]));
    {name=Array, length=0.0, value=[], nargs=1, type=object}
  Logger.log(cUseful.whatAmI([1,2,3]));
  {name=Array, length=3.0, value=[1,2,3], nargs=1, type=object}

Custom Objects

The main usage of this is to help with debugging your custom objects. An anonymous objects is detected as type:object, name:Object

Logger.log (cUseful.whatAmI({}));
    {name=Object, length=null, value={}, nargs=1, type=object}
  Logger.log (cUseful.whatAmI({a:'a',b:'b'}));
  {name=Object, length=null, value={"a":"a","b":"b"}, nargs=1, type=object}

Function

When you create your own ‘classes’, then the constructor is a function. A constructor is detected like any other function – type:function , name:Function

Logger.log(cUseful.whatAmI(AnotherClass));
  {name=Function, length=null, value=null, nargs=1, type=function}

Object instance

Testing an instance of a ‘class’ will give you something more interesting. The name property will be the name of the constructor function used to create the object, the nargs property will show how many arguments that function expects to receive, and the value will be the stringified properties of the instance.

 Logger.log(cUseful.whatAmI(new SomeClass()));
    {name=SomeClass, length=null, value={"someProperty":"2args"}, nargs=2, type=object}
  Logger.log(cUseful.whatAmI(new AnotherClass()));
  {name=AnotherClass, length=null, value={"someProperty":"4args"}, nargs=4, type=object}

Apps Script Services

Unfortunately, there’s not a lot of info on Apps Script services – they are detected as an Anonymous object.

Logger.log(cUseful.whatAmI(SpreadsheetApp));
  {name=Object, length=null, value={}, nargs=1, type=object}

For more like this see Google Apps Scripts Snippets

For help and more information join our forum, follow the blog orfollow me on Twitter