Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Javascript in Excel

I'm currently creating an Excel workbook that runs a Monte Carlo type simulation. The simulator code is currently in Javascript though, and porting entirely to VBA appears to be non-trivial given the team's inexperience with this language. So, I've been able to incorporate the javascript components into a WSC file, which works well. (Simplified example below.)

    Sub Simulate()
        Set ATPSim = GetObject("script:http://www.example.com/ATPSim.wsc")
        'Set ATPSim = GetObject("script:C:\ATPSim.wsc")
        Dim ParamOne As Integer
        ParamOne = Range("B2").Value
        Dim ParamTwo As Double
        ParamTwo = Range("B3").Value
        Dim ParamThree As Double
        ParamThree = Range("B4").Value
        Range("B1").Value = ATPSim.simulate(ParamOne, ParamTwo, ParamThree)
    End Sub

Unfortunately, this requires me to host the javascript code or rely on unsophisticated users to update the absolute path. Is there a self-contained solution? I'd prefer to keep everything in one .xlsm file which can be e-mailed to the users.

like image 309
Izomiac Avatar asked Apr 09 '13 20:04

Izomiac


People also ask

Can I use JavaScript in Excel?

An Excel add-in interacts with objects in Excel by using the Office JavaScript API, which includes two JavaScript object models: Excel JavaScript API: Introduced with Office 2016, the Excel JavaScript API provides strongly-typed objects that you can use to access worksheets, ranges, tables, charts, and more.

Can I use JavaScript to automate Excel?

You can use any script languages like JavaScript from Internet Explorer to automate Excel (out-of-proc), but most of web browsers (where your script code can be run) are not aware of COM servers and this technology in general.

How do you get data from Excel using JavaScript?

Javascript Excel - Import Data From Excel The Excel file is read into Uint8Array object, which is then passed to the load method exposed by the Excel library. Once the worksheet is loaded into the Excel library object, we can read each cell value and build a JSON array that will be used as the igGrid data source.


1 Answers

You should be able to host the JS in a COM Component and use WSH to access it.

See the example here: How can I use JavaScript within an Excel macro?

like image 63
Laurence Moroney Avatar answered Nov 15 '22 01:11

Laurence Moroney