[Solved] vba arrays from google apps script


Google Apps script is based on javascript. VBA is based on Visual Basic. Hardcoding it is a possibility but you end up with some funky array and you’re then limited to VBA’s clunky array methods (or lack thereof).

Function createChargeList()
    Dim var(5)
    var(0) = [{"No.", "Name", "Cooldown", "Power", "Energy Loss", "Type", "Damage Window Start"}]
    var(1) = [{1.0, "AerialAce", 240.0, 55.0, 33.0, 3.0, 190.0}]
    var(2) = [{2.0, "AirCutter", 270.0, 60.0, 50.0, 3.0, 180.0}]
    var(3) = [{3.0, "AncientPower", 350.0, 70.0, 33.0, 6.0, 285.0}]
    var(4) = [{4.0, "AquaJet", 260.0, 45.0, 33.0, 11.0, 170.0}]
    var(5) = [{5.0, "AquaTail", 190.0, 50.0, 33.0, 11.0, 120.0}]
    createChargeList = var
    
End Function

Or you could “hardcode” it to a Dictionary or Collection or an ArrayList (of ArrayLists). There would be some benefits to using a Dict or ArrayList because they have other methods you can use. Dictionary example, this is not how I would structure it but it shows how you could do it:

Function createChargeDict()
    Dim d As Object
    Set d = CreateObject("Scripting.Dictionary")
    d("No.") = Array(1#, 2#, 3#, 4#, 5#)
    d("Name") = Split("AerialAce,AirCutter,AncientPower,AquaJet,AquaTail", ",")
    'etc...
    Set createChargeDict = d
End Function

But, it’s going to be difficult to “hardcode” it if it’s more than a very small amount of data e.g., per Tim’s answer (or likewise through what I did, above), and of course if anything changes you need to go and edit the code.

Another option would be to save the data as a CSV or otherwise delimited file and read it in to an array/dict/list/collection at runtime.

What I would do (Opinion)

Another more common approach — as you inquired about in your comments — would be to simply create a table on a worksheet (optionally: hide the worksheet so user’s can’t mess it up), assign a Name to represent the table’s range, and then you can just refer to the range’s .Value property which will return an array.

The added benefits of this is that you can use all of the Worksheet methods and functions against the range of cells (or as a Table/ListObject on a sheet), and then you can do a lot more with it in terms of filtering, slicing, finding elements, matching/indexing, etc., and it’s also easier to update.

solved vba arrays from google apps script