Excel Macros with Javascript - 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?

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

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?

Related

Is there any way to append data into local excel file using jquery?

I have a web page that calculate data about specific thing, this page does not have a database to store data so i want to make an excel file on the server to store data in it when user click 'submit', the problem is that i want to create only one excel file and i want to append new data into this file, can i do that with javascript or jquery ?
EDIT : i prefer making an offline project.
The question is too broad, but for the scope of answering,
1) You could use Google Sheets API to store the value to a sheet in the google cloud. (Yes, you can use javascript for this)
2) You could create a node.js server and use javascript to write to csv or an excel file(research for npm modules based on your requirement) and expose it using a REST API for your application
Yes this is possible.
Steps:
1. You can keep one excel sheet on the server, (example- test.csv).
2. Every time you want to append some data to this excel sheet, you may copy data from test.csv into json format, append the new data into the json (array).
3. delete the test.csv from server.
4. Use FILE APIs, to create a new csv file by the name test.csv with the data that was generated in json format in step 2.
(This is independent of node/google sheets, can be used with just html, js, and also if your project is in php);
I have a working code for this somewhere in my backup, would try to provide a link here to it.
P.s - This is probably my first answer here, please excuse for any issues.
You will need to install node.js into your machine to do this locally. (https://nodejs.org/en/download/). It would need a lot of security disables to allow file manipulation on the browser.
After, if your purpose is to only store data, than a CSV file would be easier and lighter (and you can open them with Excel). Run npm search csv to check a lot of packages that you can use to work with CSV
If you really need and Excel file, you can install this package npm install exceljs. And here is the docs for it https://github.com/exceljs/exceljs

Copy Spreadsheet Ready CSV to Clipboard

I would like to display some comma separate values to a user in JavaScript (but really, language is not the point here) and allow them to paste that right into their spreadsheet program, much like SQL Server Management Studio does.
For example, in SQL Management Studio (and other programs), if I select the columns here, I can paste those right into Excel, no formatting or importing needed.
If I just output CSV as it is:
It puts everything in one row:
What do I need to do to my output to make it so when I copy it, I can just easily paste it into a spreadsheet program?

Read and Write DOCX file

I have 2 docx files that I am working with. One docx file contains text information of a product (start serial number, length, width, and height). The other docx file contains a sticker label with an image and all of the text information from the first file.
This is what I do currently:
I open the first docx file and copy all of the text information (serial, length, width, and height)
Then I paste each info into the second docx file that contains the formatted label.
If I need to make more than one label, I copy the label and increment the serial number by 1.
This takes a lot of time to make several labels for different products. My goal is to come up with an easier way to take data from one docx and inject it into the other. Also, generating more labels when needed.
My first thought was to extract the docx file to get it's xml contents. Then read the data using javascript, c++, or any other language. Then Ask user to input number of labels to generate, manipulate the xml, and repack it as a docx file.
Then I thought about trying to use the windows office "mail merge" feature, but I have never done this before.
I would like to know if anyone has any suggestions for an easy solution to import data from one docx file and generating labels into another.
I am open for any suggestion.
Also, I am not a professional programmer. I am an undergraduate computer engineering student with some experience in c, c++, java, javascript, python, MIPS assembly, and php.
The only open-source (and probably easier to come by) solution I know know is:
http://poi.apache.org/
http://poi.apache.org/document/quick-guide-xwpf.html
This is a good bet when it comes to speed and it is free software.
But if you open a file, alter it and save it again - the result can be flaky: The formatting can be slightly off. At least in my tests with the pptx counterpart.
I reckon when you have user interaction (web page?) in order to create the document, you can build a small HTTP Api around the library.
There is also: http://www.docx4java.org/trac/docx4j - which I have not tested yet.
You can also go the C#/Redmond way: How do I create the .docx document with Microsoft.Office.Interop.Word?
The Interop (2nd Example in the first answer of the question above) way gives the best result when it comes to the accuracy of the formatting. Basically when you open a file with Interop - it will look the same when you alter and save it. But you cannot use this when interacting with a user - because it starts a separate MS Office process - and I would not count on this from my own user experience. But if you want to generate these files as a batch in a single user session - it will deliver a good result.
I cannot comment on the "OpenXML SDK" library described in the above SO question.
Wath about the Open XML https://www.youtube.com/watch?v=rMnEl6JZ7I8 and website developer http://openxmldeveloper.org/ .
On the site you found sdk for:
Open XML SDK for JavaScript: http://openxmldeveloper.org/wiki/w/wiki/open-xml-sdk-for-javascript.aspx. Demo: http://openxmldeveloper.org/blog/b/openxmldeveloper/p/openxmlsdkjs_demo.aspx
Open XML and Java http://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2006/11/21/openxmlandjava.aspx
.Net Resources http://openxmldeveloper.org/resources/dotnet/m/cc/default.aspx

what language do i use to write a webpage in to automatically update from a database of sorts?

I have what I consider a bit of a tricky question. I am currently working on quite a large spread sheet (266 rows aith 70 coloumns and its only going to get bigger) that is a database of sorts and I want to remove it from Excel and put it on to an intranet page. I am currently writing it in a combination of HTML and Javascript for functionality, but it is becoming very hard to ensure that the data is in the right place. I am wondering if there is a possible way of being able to save the Excel spreadsheet into a certain format (like CSV or XML) and then write a program (for on a HTML page) that would display all of the infomation in a table automatically? is this even possible?
Unfortunatly i do not have access to a server to be able help with this, it all needs to be able to be coded in the page itself.
Thankyou for all your input Guys and Gals
Based on your comment, a normalized database for this type of thing would look like this:
table `workers`
- id
- name
- ...
table `trainings`
- id
- title
- description
- ...
table `workers_in_training`
- worker_id
- training_id
This allows you to create a logical matrix as well without the need to change the schema (keep adding columns) for each new training/worker. Of course, this realistically requires a database server of some sort and knowledge in a server side programming language (PHP, Python, Ruby, C#, anything). If you don't have that, an Access database/app may be an acceptable compromise. Doing it all in Javascript is certainly interesting, but is an idea you should abandon as early as possible.
Given your constraints, I would save the Excel spreadsheet as a CSV and put it in the same location as your HTML file, then use AJAX to fetch the contents of the CSV and dynamically generate a HTML table based on the contents.
Look here for how to fetch a URL's contents using AJAX (jQuery library): http://api.jquery.com/jQuery.get/
After fetching the URL content, you will have the CSV as a big string in a JavaScript variable. I'll let you have the fun of figuring out how to parse it :-)
Once you know how to parse your CSV string to recognise rows and columns, look here for how to generate HTML table dynamically using jQuery library: Building an HTML table on the fly using jQuery

How to display excel chart in browser?

I have an excel sheet containing excel chart. I am able to display entier excel sheet on browser using an iframe. However, I only want to display the chart present inside the excel sheet on the browser.
How do I achieve this? I see that it needs Javascript + ActiveX APIs. I tried but couldn't get it to work.
Please see code given below. It doesn't work as I am not able to set data attribute of "object" tag dynamically. It doesn't allow.
<object id="objFrame" data="" type="application/vnd.ms-excel"></object>
<script>
var Excel = new ActiveXObject("Excel.Application");
Excel.Visible = false;
document.getElementById("objFrame").data = Excel.Workbooks.Open("Test.xls").Sheets("Chart1");
Excel.Quit();
</script>
Is there any other way to achieve this? Please post sample code. Thanks.
You should check this tool out, I believe this will achieve exactly what you're looking for, without having to load the memory bloat/leak known as the Office COM server, into browser memory, and feel like your violating security best practices.
SpreadsheetGear for .NET provides the ability to load Excel workbooks, plug in values, calculate and then render a chart (or a range of cells which contain a chart) on the server which can then be displayed in the browser. You can see live ASP.NET (C# and VB) samples here.
Disclaimer: I own SpreadsheetGear LLC
You might want to check the Funfun Excel add-in. Basically, Funfun allows you to use JavaScript directly in your Excel as the image below shows.
Here I have a sample chart which is plotted using HighChart.js with data in the spreadsheet. You could see the chart at the right side of your Excel. You could then click the button at the left-top corner of the chart to open the chart in default browser. Which looks like this.
Also, if you want to show both the spreadsheet and the chart in the browser. You have two option.
The first is to use Excel online. Then you have the same thing on your browser as in your local Excel.
As for the second option, Funfun also has an online editor in which you could explore with your JavaScript code alongside with your data.
You could check the detailed code of this example on the link below.
https://www.funfun.io/1/edit/5a439b96b848f771fbcdedf0
Disclosure: I'm a developer of Funfun

Categories