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?
It's less like English. You can't record JavaScript macros in Excel or Word, as you can for VBA.
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.
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.
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.
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.
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
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.
Disclosure: I'm a developer of Funfun
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With