Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert VBA to Google Apps Script?

My company is switching from Microsoft Office to Google Docs, I have this (several) code I want to use in Google Spreadsheets but I have no idea on how to do that.

Consider this sample of code:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("E:J")) Is Nothing Then
        Cells(Target.Row, 1) = MonthName(Month(Date))
        Cells(Target.Row, 2) = Format(Now, "mm/dd/yyyy")
    End If
End Sub
like image 366
Tom Ruiz Avatar asked Sep 16 '14 19:09

Tom Ruiz


People also ask

How can I convert VBA to Google Script?

Your best chance for success is to use the VBA Sub as a kind of specification; if you can clearly explain what functionality is implemented by a VBA Sub, you have the basis for a specification for a new function implemented in your google spreadsheet. You can convert VBA to Google script automatically by using GSpread.NET .

Is it hard to move from VBA to Google Apps?

You have built up an expertise and body of work in VBA, but your company is now seriously considering moving to Google Apps and ditching Microsoft Office products. Is it the end of the world? Actually, in the end it’s not so hard. You can find the application and test data in the VBA to Google Apps Script Roadmapper project download.

What is the difference between VBA and Google Apps Script?

You’ll also find some adapted material from the book here. VBA is to Office as Google Apps Script is to Google Apps. Those tasks to automate repetitive sequences, or extend the functionality of Office can usually be done in Google Apps Script.

How do I convert a Google Drive macro to a script?

These APIs tend to perform critical operations for a macro, like connecting to a database or accessing a local resource, and Apps Script is generally not a good solution. On your computer, open Google Drive. On the right side panel, click the Macro Converter add-on .


3 Answers

Use GSpread.NET. to work with Google Spreadsheets by using Microsoft Excel API. You needn't rewrite all code, just replace CreateObject(Excel.Application) to CreateObject(GSpreadCOM.Application).

examples

like image 160
miro Avatar answered Sep 23 '22 14:09

miro


In 2020, Google released Macro Converter add-on, which automatically converts VBA to Apps Script.

Note:

  • Only Enterprise users can acess to this add-on.
  • Some APIs cannot be converted automatically (e.g. operations on file system). You have to rewrite them by hand.
like image 28
kamocyc Avatar answered Sep 22 '22 14:09

kamocyc


The function is structured like this:

Private Sub Worksheet_Change(ByVal Target As Range)
  Statements here
End Sub

That would probably convert to something like:

function Worksheet_Change(ByVal) {
  Statements Here;
};

Instead of End Sub the function in JavaScript ends with a curly brace }.

Instead of End If, the If, then statement also ends with a curly brace }.

if (condition) {code here;}

That's the syntax for a JavaScript If statement. There is no Then key word in JavaScript.

like image 43
Alan Wells Avatar answered Sep 22 '22 14:09

Alan Wells