Home » excel » Excel Macros with Javascript

Excel Macros with Javascript

Posted by: admin March 9, 2020 Leave a comment


I wish to manipulate excel spreadsheets using macros in Javascript rather than the default VBA. I can execute javascript code using the following VBA code

'javascript to execute
Dim b As String
b = "function meaningOfLife(a,b) {return 42;}"

'VBA tool to run it
Dim o As New ScriptControl
o.Language = "JScript"
o.AddCode b
MsgBox o.Run("meaningOfLife", 0, 1)

this enables me to execute arbitrary javascript, however I do not have access to the excel spreadsheet from within the javascript environment. Is there any way I can set and get worksheet values in the active worksheet from within javascript?

How to&Answers:

It depends what sort of “Excel programming” you want to do.

If you want to manipulate Excel files, you can do so via COM automation, or even ODBC to some degree, using JavaScript running under the Windows scripting environment.

But if you want to have your code running within an active Excel session doing things the visitor will see, you have fewer options.

See this question I posted a few years back when I had some JavaScript I wanted to run within Excel and didn’t have the budget to convert to VBA:

How can I use JavaScript within an Excel macro?


For people using Excel 2016 or later version, there is an Excel add-in called Funfun in the add-in store that actually allows you to write and run JavaScript code directly in Excel. And of course, your JavaScript code also has access to the data stored in the spreadsheet. Here is a screenshot of how it looks like in Excel

enter image description here

Well in the middle of the interface you have a section in which you could write JavaScript, CSS and HTML code. It is pretty much like a playground built into the Excel. But the Funfun also has an online editor in which you could test with your code. You could see it in the pic below. I also posted the link of the example in the first picture so you could play with.


enter image description here

What is special about the Funfun online editor is that it contains a ‘spreadsheet’ just like Excel. Though you can’t actually do any formatting in here, you could copy your data into the cells and test your code directly.

To use the data stored in the spreadsheet, all you need to do is to write some configuration in the short.io file of Funfun to tell JavaScript which area in the spreadsheet that contains your data. For example, in the example that I posted, all you need to write is

    "data": "=A2:B9"

And in the JavaScript code, an object called $internal is used to read the data. So in order to read the data that stored in A2:B9, you need to write

var data = $internal.data;

And its done. You could go to the documentation of Funfun if you want to know more.

If you are satisfied with the result you achieved in the online editor, you could easily load the result into you Excel using the URL above. Of couse first you need to insert the Funfun add-in from Insert – My add-ins. Here are some screenshots showing how you could do this.

enter image description here

enter image description here

Disclosure: I’m a developer of Funfun


Of course now in Excel 2013, you have the JavaScript API (http://zoom.it/UAzs#full) and can interact with the spreadsheet using task pane and content APPS.


Try Excel-DNA for a programmatic interface to Excel. Two other nice tools to look at are xlwings and Excel-REPL which support Python and Clojure interfacing respectively.


WSTools by WizardSoft let’s you mix VBA and JScript, including objects and return values (Variants). It has lots of other useful functions too. Kind of a utility pack.

These functions are specifically for intertwining VBA/VBScript/JavaScript

    Starts JavaScript runtime (modern IE 11 JavaScript) initialized with Script. 
    Use this to add predefined functions that you can call later with JsFunction.

JsFunction(Function.s, P1, P2, ...)
    Calls JavaScript Function with variable amount of arguments and returns result.

    Interpretes Eval as JavaScript and returns result.

Set json = WS.JsEval("JSON.parse('[ ""Test 1"", 2, 3 ]')")
Debug.Print json.Item(0)

See https://wizardsoft.nl/products/wstools