O'Reilly logo

Going GAS by Bruce Mcpherson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required