SlideShare uma empresa Scribd logo
1 de 10
Some common Excel functions
translated to Google Apps Script
Quick start to migration – part 1 Excel Liberation
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
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.
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() ;
}
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);
}
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,
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;
}
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) ;
}
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';
}
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.

Mais conteúdo relacionado

Destaque

Using Groovy with Jenkins
Using Groovy with JenkinsUsing Groovy with Jenkins
Using Groovy with Jenkinssascha_klein
 
Belajar macro excel 2007
Belajar macro excel 2007Belajar macro excel 2007
Belajar macro excel 2007doni sandra
 
Belajar Excel Tingkat Mahir
Belajar Excel Tingkat MahirBelajar Excel Tingkat Mahir
Belajar Excel Tingkat MahirAYU LESTARI
 
Ebook Lengkap Microsoft Excel 2007
Ebook Lengkap Microsoft Excel 2007Ebook Lengkap Microsoft Excel 2007
Ebook Lengkap Microsoft Excel 2007Puguh Nugroho
 
17 Excel shortcuts to learn in 2017
17 Excel shortcuts to learn in 201717 Excel shortcuts to learn in 2017
17 Excel shortcuts to learn in 2017Excel Strategies LLC
 
Business Analysis Techniques
Business Analysis TechniquesBusiness Analysis Techniques
Business Analysis TechniquesIIBA UK Chapter
 
INTRODUCTION A GOOGLE SCRIPT [SLI]
INTRODUCTION A GOOGLE SCRIPT [SLI]INTRODUCTION A GOOGLE SCRIPT [SLI]
INTRODUCTION A GOOGLE SCRIPT [SLI]Johan Claeys
 
Working With Big Data
Working With Big DataWorking With Big Data
Working With Big DataSeth Familian
 

Destaque (8)

Using Groovy with Jenkins
Using Groovy with JenkinsUsing Groovy with Jenkins
Using Groovy with Jenkins
 
Belajar macro excel 2007
Belajar macro excel 2007Belajar macro excel 2007
Belajar macro excel 2007
 
Belajar Excel Tingkat Mahir
Belajar Excel Tingkat MahirBelajar Excel Tingkat Mahir
Belajar Excel Tingkat Mahir
 
Ebook Lengkap Microsoft Excel 2007
Ebook Lengkap Microsoft Excel 2007Ebook Lengkap Microsoft Excel 2007
Ebook Lengkap Microsoft Excel 2007
 
17 Excel shortcuts to learn in 2017
17 Excel shortcuts to learn in 201717 Excel shortcuts to learn in 2017
17 Excel shortcuts to learn in 2017
 
Business Analysis Techniques
Business Analysis TechniquesBusiness Analysis Techniques
Business Analysis Techniques
 
INTRODUCTION A GOOGLE SCRIPT [SLI]
INTRODUCTION A GOOGLE SCRIPT [SLI]INTRODUCTION A GOOGLE SCRIPT [SLI]
INTRODUCTION A GOOGLE SCRIPT [SLI]
 
Working With Big Data
Working With Big DataWorking With Big Data
Working With Big Data
 

Mais de Bruce McPherson

Do something in 5 with gas 8-copy between databases
Do something in 5 with gas 8-copy between databasesDo something in 5 with gas 8-copy between databases
Do something in 5 with gas 8-copy between databasesBruce McPherson
 
Do something in 5 with gas 9-copy between databases with oauth2
Do something in 5 with gas 9-copy between databases with oauth2Do something in 5 with gas 9-copy between databases with oauth2
Do something in 5 with gas 9-copy between databases with oauth2Bruce McPherson
 
Do something in 5 with apps scripts number 6 - fusion crossfilter
Do something in 5 with apps scripts number 6 - fusion crossfilterDo something in 5 with apps scripts number 6 - fusion crossfilter
Do something in 5 with apps scripts number 6 - fusion crossfilterBruce McPherson
 
Do something in 5 with gas 7-email log
Do something in 5 with gas 7-email logDo something in 5 with gas 7-email log
Do something in 5 with gas 7-email logBruce McPherson
 
Do something useful in Apps Script 5. Get your analytics pageviews to a sprea...
Do something useful in Apps Script 5. Get your analytics pageviews to a sprea...Do something useful in Apps Script 5. Get your analytics pageviews to a sprea...
Do something useful in Apps Script 5. Get your analytics pageviews to a sprea...Bruce McPherson
 
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheetDo something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheetBruce McPherson
 
Do something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing appDo something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing appBruce McPherson
 
Do something in 5 with gas 2-graduate to a database
Do something in 5 with gas 2-graduate to a databaseDo something in 5 with gas 2-graduate to a database
Do something in 5 with gas 2-graduate to a databaseBruce McPherson
 
Do something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseDo something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseBruce McPherson
 
Google cloud datastore driver for Google Apps Script DB abstraction
Google cloud datastore driver for Google Apps Script DB abstractionGoogle cloud datastore driver for Google Apps Script DB abstraction
Google cloud datastore driver for Google Apps Script DB abstractionBruce McPherson
 
Using script db as a deaddrop to pass data between GAS, JS and Excel
Using script db as a deaddrop to pass data between GAS, JS and ExcelUsing script db as a deaddrop to pass data between GAS, JS and Excel
Using script db as a deaddrop to pass data between GAS, JS and ExcelBruce McPherson
 
JavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primerJavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primerBruce McPherson
 
VBA API for scriptDB primer
VBA API for scriptDB primerVBA API for scriptDB primer
VBA API for scriptDB primerBruce McPherson
 

Mais de Bruce McPherson (15)

Do something in 5 with gas 8-copy between databases
Do something in 5 with gas 8-copy between databasesDo something in 5 with gas 8-copy between databases
Do something in 5 with gas 8-copy between databases
 
Do something in 5 with gas 9-copy between databases with oauth2
Do something in 5 with gas 9-copy between databases with oauth2Do something in 5 with gas 9-copy between databases with oauth2
Do something in 5 with gas 9-copy between databases with oauth2
 
Goa tutorial
Goa tutorialGoa tutorial
Goa tutorial
 
Do something in 5 with apps scripts number 6 - fusion crossfilter
Do something in 5 with apps scripts number 6 - fusion crossfilterDo something in 5 with apps scripts number 6 - fusion crossfilter
Do something in 5 with apps scripts number 6 - fusion crossfilter
 
Do something in 5 with gas 7-email log
Do something in 5 with gas 7-email logDo something in 5 with gas 7-email log
Do something in 5 with gas 7-email log
 
Do something useful in Apps Script 5. Get your analytics pageviews to a sprea...
Do something useful in Apps Script 5. Get your analytics pageviews to a sprea...Do something useful in Apps Script 5. Get your analytics pageviews to a sprea...
Do something useful in Apps Script 5. Get your analytics pageviews to a sprea...
 
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheetDo something in 5 with gas 4- Get your analytics profiles to a spreadsheet
Do something in 5 with gas 4- Get your analytics profiles to a spreadsheet
 
Do something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing appDo something in 5 with gas 3-simple invoicing app
Do something in 5 with gas 3-simple invoicing app
 
Do something in 5 with gas 2-graduate to a database
Do something in 5 with gas 2-graduate to a databaseDo something in 5 with gas 2-graduate to a database
Do something in 5 with gas 2-graduate to a database
 
Do something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as databaseDo something in 5 minutes with gas 1-use spreadsheet as database
Do something in 5 minutes with gas 1-use spreadsheet as database
 
Google cloud datastore driver for Google Apps Script DB abstraction
Google cloud datastore driver for Google Apps Script DB abstractionGoogle cloud datastore driver for Google Apps Script DB abstraction
Google cloud datastore driver for Google Apps Script DB abstraction
 
Dbabstraction
DbabstractionDbabstraction
Dbabstraction
 
Using script db as a deaddrop to pass data between GAS, JS and Excel
Using script db as a deaddrop to pass data between GAS, JS and ExcelUsing script db as a deaddrop to pass data between GAS, JS and Excel
Using script db as a deaddrop to pass data between GAS, JS and Excel
 
JavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primerJavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primer
 
VBA API for scriptDB primer
VBA API for scriptDB primerVBA API for scriptDB primer
VBA API for scriptDB primer
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 

Último (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

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.