question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

XLSX.utils.sheet_to_json returning empty array

See original GitHub issue

Hey,

I am trying to give our users the possibility to upload an XLSX file (through ng-file-uploader), and to convert this XLSX file to JSON data afterwards. The uploading of the file works fine, this is the result when I print $scope.file in the console: screen shot 2018-03-02 at 10 08 16

When I try to use the sheet to json function, I get an empty array back, while there is a lot of data in the uploaded XLSX file.

In my code I use the following: XLSX.utils.sheet_to_json($scope.file);

This returns an empty array as said before. Is there something I’m forgetting or should execute differently?

Kind regards,

Thomas

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

16reactions
jvicensfarruscommented, Jul 24, 2018

@Xsmael I solved this setting a the name of the sheet you want to use and also the header, which might help you to identify each column.

var workbook = XLSX.read(bstr, {type:'binary'});
var form = XLSX.utils.sheet_to_json(workbook.Sheets[workbook.SheetNames[3]], {header:["headerName1","headerName2","headerName3"]});

Where workbook.SheetNames[3] is the name of my third sheet, which is the one I’m using in this example. I hope this helps you

0reactions
jvicensfarruscommented, Sep 15, 2021

@jvicensfarrus proving headers just outputs an array with provided headers, no other sheet data is prasent ? anything else we need to do ?

const wb = XLSX.read( bufferArray, { type: 'buffer' } );
      const wsname = wb.SheetNames[7];
      const ws = wb.Sheets[wsname];
      const data = XLSX.utils.sheet_to_json(ws, {
        raw : true,
        header : ['Date','Currency','Income','Expense','Fee','Balance','Reason','Remarks']
      });

@shamseerahammedm you could try to not use constants and creating the ‘ws’ directly within the wb.SheetName[7]

var wb = XLSX.read( bufferArray, { type: 'buffer' } );
var ws = wb.SheetNames[7];
var data = XLSX.utils.sheet_to_json(ws, {
        raw : true,
        header : ['Date','Currency','Income','Expense','Fee','Balance','Reason','Remarks']
 });
Read more comments on GitHub >

github_iconTop Results From Across the Web

sheet_to_json producing bad array of array - Stack Overflow
Using Angular 9, I am building a step wizard and the first step is to read and parse an .xlsx file. I have...
Read more >
Utility Functions - SheetJS Community Edition
XLSX.utils.aoa_to_sheet takes an array of arrays of JS values and returns a worksheet resembling the input data. Values are interpreted as follows:.
Read more >
How to use the xlsx.utils.json_to_sheet function in xlsx | Snyk
To help you get started, we've selected a few xlsx.utils.json_to_sheet ... function (rows, options) { if (!rows) { throw new Error('Empty json array...
Read more >
Check for Empty/Undefined/Null String in JavaScript - Morioh
Note: If the starting point was a file or HTML table, XLSX.utils.sheet_to_json will generate an array of JS objects. Package and Release Data....
Read more >
xlsx - npm
var jsa = XLSX.utils.sheet_to_json(worksheet, opts);. Create an array of arrays of JS values from a worksheet.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found