When converting from VBA, unless you are abandoning VBA, it's just as well to mimic some of the functions that are missing from GAS so that you can minimize your script Conversion work. Here is a selection of useful ones for quick reference. These are also available in a shareable Google Apps Script Library.
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Some common VBA functions translated to Google Apps Script
1. Some common Excel functions
translated to Google Apps Script
Quick start to migration – part 1 Excel Liberation
2. Excel Liberation for details
Google Apps Script migration
Many people are either migrating to Google Docs or
using them alongside Microsoft Office Products
One inhibitor is where VBA automation has been
implemented in Excel workbooks. Google Apps
Script is essentially javaScript and has some
conceptual differences that provide a steep learning
curve when coming from Microsoft land.
A strategy for easing and speeding the transition is to
mimic common VBA functions in Google Apps
Script to minimize code conversion
3. Excel Liberation for details
Approach
javaScript differs considerably from VBA. This deck
does not cover learning javaScript, but rather
shows how some common VBA function might be
written in javaScript.
In order to minimize changes, we need to break
various javaScript conventions (like case
conventions for names)
A more detailed javaScript conversion primer and
some example project conversions can be found
here.
The functions discussed here are a subset of those
already available in a Google Apps Script
shareable library which you can include in your
project.
4. Excel Liberation for details
String functions - 1
VBA javaScript
RTrim(s) function RTrim(s) {
return CStr(s).replace(/ss*$/, "");
}
LTrim(s) function LTrim(s) {
return CStr(s).replace(/ss*$/, "");
}
Trim(s) function Trim(v) {
return LTrim(RTrim(v));
}
Len(v) function Len(v) {
return CStr(v).length ;
}
CStr(v) function CStr(v) {
return v===null || IsMissing(v) ? '' : v.toString() ;
}
5. Excel Liberation for details
String functions - 2
VBA javaScript
Left
(str,optLen
)
function Left(str,optLen) {
return Mid( str, 1 , optLen);
}
Right
(str,optLen
)
function Right(str,optLen) {
return Mid( str, 1 + Len(str) - fixOptional ( optLen, Len(str) ) );
}
Mid
(str,optSta
rt,optLen)
function Mid (str,optStart,optLen) {
var s = CStr(str);
var start = IsMissing (optStart) ? 0 : optStart - 1;
start = start < 0 ? 0 : start;
var length = IsMissing (optLen) ? Len(s) - start + 1 : optLen ;
return s.slice ( start, start + length);
}
6. Excel Liberation for details
String functions - 3
VBA javaScript
Split
(s,optDelim,
optLimit)
function Split(s,optDelim,optLimit) {
return CStr(s).split(fixOptional(optDelim,","),fixOptional(optLimit,-1));
};
LCase(s) function LCase(s) {
return CStr(s).toLowerCase();
}
function
UCase(s)
function UCase(s) {
return CStr(s).toUpperCase();
}
function
Chr(n)
function Chr(n) {
return String.fromCharCode(n);
}
function
Asc(s)
function Asc(s) {
return s.charCodeAt(0);
}
7. Excel Liberation for details
String functions - 4
VBA javaScript
InStr
(optStart,
inThisString
, lookFor,
optCompar
e)
function InStr(optStart,inThisString,lookFor,optCompare) {
// TODO optCompare
var start = fixOptional (optStart, 1);
var s = Mid (inThisString, start);
var p = s.indexOf(lookFor);
return (s && lookFor) ? (p == -1 ? 0 : p+start ): 0;
}
InStrRev
(inThisStrin
g,lookFor,
optStart,
optCompar
e)
function InStrRev(inThisString,lookFor,optStart,optCompare) {
// TODO optCompare
var start = fixOptional (optStart, -1);
var s = CStr(inThisString);
start = start == -1 ? Len(s) : start ;
return (s && lookFor) ? s.lastIndexOf(lookFor,start-1)+1 : 0;
}
8. Excel Liberation for details
Conversions
VBA javaScript
DateSerial
(y,m,d)
function DateSerial(y,m,d) {
return new Date(y,m,d);
}
Year (dt) function Year(dt) {
return dt.getFullYear();
}
CLng(v) function CLng(v) {
return isTypeNumber(v) ? Math.round(v) : parseInt(v) ;
}
CDbl(v) function CDbl(v) {
return parseFloat(v) ;
}
9. Excel Liberation for details
Tests
VBA javaScript
IsEmpty
(v)
function IsEmpty(v) {
return typeof(v) == "string" && v == Empty();
}
IsDate
(sDate)
function IsDate (sDate) {
var tryDate = new Date(sDate);
return (tryDate.toString() != "NaN" && tryDate != "Invalid
Date") ;
}
IsNumeric
(s)
function IsNumeric(s) {
return !isNaN(parseFloat(s)) && isFinite(s);
}
IsObject
(x)
function IsObject (x) {
return VarType(x) == 'object';
}
10. Excel Liberation for details
Part 2
That’s a brief introduction to some GAS
equivalents (almost) for VBA functions.
Part 2 will look at some of the more workbook
specific translations.
A more detailed javaScript conversion primer and
some example project conversions can be
found here.
The functions discussed here are a subset of
those already available in a Google Apps
Script shareable library which you can include
in your project.