Chapter 3. Translating VBA Functions

The chapter will help you to get started with JavaScript by converting common VBA built-in functions to their JavaScript equivalents. This should do two things:

  • Create a useful library of functions that have the same name and operate the same way as the VBA built-in functions. When porting you can choose to simply keep the VBA names, or to refactor in the JavaScript code.

  • Demonstrate some everyday JavaScript syntax. The code for these functions is likely to cover most basic manipulation operations.

Conventions

Because these functions will retain their VBA names (and capitalization), we’ll be breaking some JavaScript conventions on function names. However, I think it’s worth it, as porting will become more of a cut-and-paste operation. By retaining the non-JavaScript-like names, we reduce the risk of a name collision with other, nonported functions.

Library and Namespace

If these functions were to be created locally, I would implement them in their own namespace to isolate them. However, the objective here is to create a reusable library, which will come with its own namespace. All VBA functions will be accessible from other projects using this pattern:

VB

VBA.builtinFunctionName()

For example:

JS

var s = VBA.Trim(aString);

It’s not very well known, but in VBA, the fully qualified way to call built-in functions is exactly the same. If you enter VBA. into the IDE, you’ll see a list of all built-in VBA functions, which can then be referenced ...

Get Going GAS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.