Parsing an excel sheet with javascript

I am using SheetJS to parse Excel sheets, however I am facing the following error:

"Uncaught TypeError: jszip is not a function"

When you run the following code:

var url = "/test-files/test.xlsx";
var oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";

oReq.onload = function(e) {
  var arraybuffer = oReq.response;

  var data = new Uint8Array(arraybuffer);
  var arr = new Array();
  for(var i = 0; i != data.length; i++) arr[i] = String.fromCharCode(data[i]);
  var bstr = arr.join("");

  var workbook = XLSX.read(bstr, {type: "binary"});
}

oReq.send();

      

The original code is here: https://github.com/SheetJS/js-xlsx

Are there any suggestions for a simpler implementation of parsing Excel files?

+3


source to share


2 answers


Posting as an answer (solution provided in comments) in case this might help someone else in the future:

It looks like you are using the src / xlsx.js version of xlsx.js which depends on other source files like jszip.js .



To fix this use the dist version of xlsx.js located at dist / xlsx.js

+5


source


Here is another solution for people who have trouble trying to use an Excel file in JavaScript. Instead of reading an Excel file using JavaScript, you can directly use JavaScript in Excel using the Funfun Excel add-in. Basically, Funfun is a tool that allows you to use JavaScript in Excel, so you don't need to use additional code to parse Excel files.

Basically, you need to do

1). Insert the Funfun add-in from the Office Add-in store

enter image description here

2). Create a new Funfun or download a sample from the Funfun online editor

enter image description here

3). Write the JavaScrip code like you would in any other JavaScript editor. In this step, to directly use the data from the spreadsheet, you need to write some JSON I / O to create an Excel cell reference. The place where this value is in Setting is short , but it will be just a few lines. For example, let's say we have some data as shown below in a spreadsheet.

enter image description here

In this case, the JSON I / O value would be:



{
    "data": "=A1:E9"
}

      

Then, in your script.js file, you just need to use one line of code to read that data.

var dataset = $internal.data;

      

The dataset will be an array, each element will have one row in the spreadsheet. More information can be found in the Funfun documentation.

4). Run the code to plot the graph

Here is an example of a chart I made using JavaScript (HighChart.js) and Excel data in the Funfun online editor. You can check it out at the link below. You can also easily download it to Excel as described in step 2.

https://www.funfun.io/1/edit/5a439b96b848f771fbcdedf0

enter image description here

Disclosure: I'm a developer at Funfun.

+2


source







All Articles