Converting Excel with Macros to Google Spreadsheets
One of my recent project involved converting a finance related Microsoft Excel Spreadsheet into a web based Google Spreadsheet that can be accessed from a web browser. The Excel sheet had VBA Macros that were converted into corresponding functions using Google Apps Scripts.
Here’s one such VBA routine that scraped Yahoo Finance and was rewritten in Google Apps. In Visual Basic, you create an XMLHttpRequest object to connect to an external server which can be replaced with URLFetch in Apps Script. The debug.print methods can be replaced with Logger.log while the regular JavaScript functions can be used for text manipulation.
The StatusBar property of the Application object in Excel VBA displays the macro progress in the Excel UI and you can replace it with the .toast() method of SpreadsheetApp class in Google Scripts.
Function GetFundName(symbol As String) As String
Dim Inet1 'As Inet
Dim fndSym As Integer, endCnt As Integer, begCnt As Integer
Dim bFound As Boolean
Application.StatusBar = "Getting Fund Name for " & symbol
Set Inet1 = CreateObject("Microsoft.XMLHTTP")
sStockPage = "http://finance.yahoo.com/q/hp?s=" & symbol
With Inet1
.Open "GET", sStockPage, False
.send
sStockPage = Inet1.ResponseText
End With
Set Inet1 = Nothing
fndSym = InStrRev(sStockPage, "(" & symbol) // .indexOf in JS
endCnt = fndSym - 2
bFound = False
Do Until bFound
fndSym = fndSym - 1
'Debug.Print (Mid(sStockPage, fndSym, 1)) // Logger.log in Google Scripts
bFound = (Mid(sStockPage, fndSym, 1) = ">")
Loop
GetFundName = Mid(sStockPage, fndSym + 1, endCnt - fndSym + 1) // .substring() in JS
Application.StatusBar = False
End Function
Amit Agarwal
Google Developer Expert, Google Cloud Champion
Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.
Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory