Before moving on to applying this to testing function arguments, let's look at some simple examples.
Optional or multiple types
Array validation for missing arguments
Custom objects - from libraries or you create yourself
and so on.
Of course the real reason for this function is to validate arguments arriving in a function at run time against what was expected. One way is to repeat the function arguments.
The arguments parameter can be tweaked to look like an array and passed over, so a better way is to do this.
That way, the only thing you have to maintain is the expected types array and validateArgs will complain if you have less valid types specified than arguments arriving.
By default, it will throw an error when an argument is mismatched. However, passing the optFail parameter as false will not fail but return a results object like this, which shows the entire stack and what the problem was in the detail property. Even if you choose not to fail, this result will be written to the log file. You can access it like this
You'll get this
Best way to get this is to include the cUseful library, or you can find on GitHub or below.
Many of the snippets in this section of the site are part of the cUseful library. You can find the details below.
You want to learn Google Apps Script?
Learning Apps Script, (and transitioning from VBA) are covered 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.
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > Google Apps Scripts snippets >