VBA debug object

Debugging Google Apps Script

There is a debugger for google apps script.

Here's how to use it

How to Use Google Apps Script Debugger


Compared to the VBA debugger it sucks, so you probably need to be able to log and assert. javaScript does not have an assert, and the Logger object is normally used to log messages. There is also an Execution Log which shows the calls that have been made to the google docs API. These can be found in the view menu of the script API.

VBA hack

As usual, to simplify transitioning from VBA, I have created a couple of hacks. This is now part of a Google Apps Script shared library you can incorporate in your own project.

DebugPrint

The first one is rather straightforward - Debug.Print()

It is called DebugPrint() and can take any number of arguments. These will be logged and can be viewed in the Log viewer.  If you pass an object as an argument, it will show the properties of the object and their values too (something you cant do in VBA).

function DebugPrint(){
  var s ='';
  for( var i = 0; i < arguments.length; i++ ) {
    if (s) s += "|";
    var a = arguments[i];
    if (IsObject(a)) {
      var t='';
      for (var c in a) {
        if (t) t+=',';
        t += (c + ':' + 
        (a[c] ? 
          (a[c].toString ? 
            a[c].toString() : 
            'error') : 
            'null' ) 
        );
      }
      s+= 'object{'+t+'}|';
     }
     else
       s += a.toString ? a.toString() : 'error'  ;
   }
  Logger.log(s);
}

DebugAssert

Mimicking Debug.Assert is a little more complex, since we want to throw an exception if something is not true. I also provide a second argument where you can put some kind of message to be created as well as the exception.

If the mustBeTrue argument to DebugAssert is not true, an exception message will be generated and execution aborted.

function VbaAssert(what) { 
  this.what = what; 
  this.toString = function () {
  return 'VbaAssert: ' + this.what;
  };
}

function DebugAssert(mustBeTrue, sayWhat) {
  if (!mustBeTrue) 
      throw new VbaAssert(sayWhat);
      
  return mustBeTrue;
}

Here's an example usage
    DebugAssert(!isUndefined(defaultValue) ,
      'programming error: no default value for missing argument');

For more like this, see  From VBA to Google Apps Script . Why not join our forum,follow the blog or follow me on twitter to ensure you get updates when they are available. Much of  the code referred to here is the mcpher library. You can find out how to include it in your project here.

Transitioning is covered more comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly.




Comments