14

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?

4
  • Can you pass in the excel objects to JavaScript?instead of 0 and 1? Commented Aug 26, 2016 at 13:47
  • 1
    @ArchimedesTrajano, look at ExcelDNA to see how they do it, then adapt to JS instead of C# Commented Aug 28, 2016 at 2:12
  • Why would you want to do this? The VBA environment is much, much better for Excel development. If it is just about {}s vs End If then you will learn that End If is actually better... Commented Feb 22, 2021 at 22:17
  • @Tuntable I agree, at the time I had some js I wanted to execute. In general using VBA is better. Commented Jul 23, 2021 at 1:50

3 Answers 3

22

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 2016.

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.

https://www.funfun.io/1/#/edit/5a4e0d461010eb73fe125c4e

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

Sign up to request clarification or add additional context in comments.

Comments

5

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?

Comments

1

Jade is probably the best Excel Add-In for manipulating data. JADE

FunFun is probably the best for analytics. FunFun

Jade Excel JavaScript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.