If you've been following along, I'm in the process of moving a library of Excel VBA scripts over to Google App Script (specifically Sheets). I've now successfully converted one of my key functions from VBA to App Script.
The function itself is relatively basic - it's a version of VLOOKUP on steroids. Instead of returning a single matching item, it will return a delimited list of matching items. This is handy when you're trying to map a 1:many relationship. If you grok SQL, it's equivalent to:
SELECT ROWID FROM (some range) WHERE fieldName like "%pattern%"
If someone finds it interesting / useful, let me know and I can share it.
Once converted Google makes a very handy little "Debug" function available... except that you cannot actually use it for debugging code that's fully dynamic. I should not that it works if you aren't trying to pass in parameters from a spreadsheet. Maybe this is why everyone writes using code like "SpreadsheetApp.getActiveSheet().getRange(2,2).getValues()"
VBA doesn't have this issue since you can set a breakpoint and the system will break when called with the stack from the caller. For instance, in Excel Sheet1 cell A1 you have "=myFx(A2,B2:B100)", when the breakpoint hits your stack will be the contents from that call. It makes it trivial to debug the function using real world test data.
In App Script, you'll be stuck writing a test function. That's not necessarily bad but it does require you to plan things out a bit more since you're Spreadsheet has to be loaded, the ActiveSheet has to be on the right tab, etc. You could use the named Sheet array but my initial concept on this was to develop and deploy this as a library so I was trying to avoid fixed references. This hasn't worked out so well in practice but more on this later.
As a convention, I've started now using fx prefixes for the functions and test_ for test code. Eventually, I'll probably build a unit test around this so I can monitor the code quality easier. I haven't seen a great way to do this yet but I'll look into in the future.
Example test routine:
Using this pattern, you can place a breakpoint either in the test_myFx or in myFx and use the inline debugger. Note my previous comments about the difference between ranges and values. findRange is actually an array of values. You could pass in the range as:
SpreadsheetApp.getActiveSheet().getRange(2,2)
and findRange would then be represented as a range rather than the array of values; however, Sheets will pass you an array of values when you call the function in a cell. I highly recommend passing the values from the test routine to avoid success in the debugger and failure when using the function.
Once a test routine is implemented, test from the test routine and all of the variables in the debugger will stop saying "Undefined". They should now represent what you've passed from the test routine to the function.
The debugger itself is a standard step into / step over / run / stop so not much surprise there.
The Logger function actually was a very slick feature VBA doesn't really offer. Note that you can't use sprintf type outputs or even the embedded
Logger.log("Test myV=${myV}")
Instead, it's basic addition operators without formatting... but it does it's job.
Logger.log("Test myV="+myV)
Note that you can pull from the Utilities.format function for value conversion. I didn't bother since everything I needed was text but it may be useful for some.
Comments