Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Macros with Javascript

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?

like image 766
Matthew Molloy Avatar asked Nov 24 '13 06:11

Matthew Molloy


People also ask

Can you write Excel macros in JavaScript?

It's less like English. You can't record JavaScript macros in Excel or Word, as you can for VBA.

Can JavaScript interact with 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 we use JavaScript in VBA?

For example, JavaScript has enhanced library routines to handle JSON data which is tough to manipulate within VBA. In such case, we can make use of this technique and refer JSON libraries within VBA.

How can I use Excel in JavaScript?

A simple way to access Excel's power is to put your data in a table. That lets you quickly filter or sort your data. Select your data by clicking the first cell and dragging to the last cell in your data. To use the keyboard, hold down Shift while you press the arrow keys to select your data.


1 Answers

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

like image 106
Chuan Qin Avatar answered Sep 19 '22 08:09

Chuan Qin