3

I'm using SheetJS in order to parse Excel sheets however I run into the following error:

"Uncaught TypeError: jszip is not a function"

When executing 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 located here: https://github.com/SheetJS/js-xlsx

Are there any suggestions for an easier implementation of parsing Excel files?

6
  • Looks like you're missing the jszip dependency on your project. Are you using the dist version of js-xlsx? Commented Aug 4, 2015 at 22:15
  • Sorry what do you mean by "dist"? Commented Aug 4, 2015 at 22:16
  • 1
    The distribution version of xlsx.js. How did you include the script in your project? According to their bower.json, js-xlsx has no dependencies, and the file you should be using is located in dist/xlsx.js. Commented Aug 4, 2015 at 22:17
  • O my god! Well there goes 2 hours of running around in circles. Thanks @yvesmancera. For the sake of my sanity do you mind explaining the logic behind your leading to that conclusion. I'm REALLY new into this type of programming and I want to learn the mindset of finding the root of these types of errors. Commented Aug 4, 2015 at 22:21
  • 1
    Sure, first I looked at the GitHub project and read the Installation section, then I saw the project's source files, more specifically, their bower.json file, which indicates what the main file and which dependencies it has, since I saw it had no dependencies, I assumed you must have included the file xlsx.js located in src instead of the one located in dist. Commented Aug 4, 2015 at 22:25

2 Answers 2

5

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

It looks like you're using the src/xlsx.js version of xlsx.js, which is dependent on other source files, like jszip.js.

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

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

Comments

2

Here is another solution for people who have problem when trying to use Excel file in JavaScript.Instead of reading an Excel file with JavaScript, you could directly use JavaScript in Excel with the help of the Funfun Excel add-in. Basically, Funfun is a tool that allows you to use JavaScript in Excel, therefore you don't need to use write additional code to parse Excel files.

Basically, what you need to do is

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

enter image description here

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

enter image description here

3). Write JavaScrip code as you do in any other JavaScript editor. In this step, in order to directly use the data from the spreadsheet, you need to write some JSON I/O to make Excel cell reference. The place this value is in Setting-short but this would be just several lines. For example, let's assume we have some data like below in the spreadsheet.

enter image description here

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

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

Then in the script.js file, you just need to use one single line of code to read this data.

var dataset = $internal.data;

The dataset would be an array, each item would be one row in the spreadsheet. You could check the Funfun documentation for more explanation.

4). Run the code to plot chart

Here is a sample chart that I made using JavaScript(HighChart.js) and Excel data on Funfun online editor. You could check it on the link below. You could also easily load it to your Excel as described in Step2.

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

enter image description here

Disclosure: I'm a developer from Funfun.

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.