Upload
bruce-mcpherson
View
32.583
Download
4
Embed Size (px)
DESCRIPTION
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.
Citation preview
Some common Excel functions translated to Google Apps Script
Quick start to migration – part 1 Excel Liberation
Excel Liberation for details
Google Apps Script migrationMany 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 conversion strategymimic vba
Excel Liberation for details
ApproachjavaScript 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.
example projectsExcel Liberation
Excel Liberation for details
String functions - 1
VBA javaScript
RTrim(s) function RTrim(s) { return CStr(s).replace(/\s\s*$/, "");}
LTrim(s) function LTrim(s) { return CStr(s).replace(/\s\s*$/, "");}
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() ;}
case conventionmimic vba existing functions
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,optStart,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);}
starting index valuemimic vba – start at 1
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);}
Excel Liberation for details
String functions - 4
VBA javaScript
InStr (optStart, inThisString, lookFor, optCompare)
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 (inThisString,lookFor, optStart, optCompare)
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;}
More complexNot 100% done
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) ;}
Dates in javascriptVery different to VBA
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';}
CheckingJust a small subset
Excel Liberation for details
Part 2That’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.
conversion strategyUse a shared library