sreepurna
sreepurna

Reputation: 1666

Reading first n lines of sheet using xlsx module

I am trying to read first five rows of data from an excel sheet using xlsx module. Initially, I tried by using a sheet_to_json method which converts whole sheet data to an array of arrays.

let sheetData = xlsx.utils.sheet_to_json(workbook.Sheets[sheetsList[i]], {
  header: 1,
  defval: '',
  blankrows: true
});

But the problem (out of memory) incurred when the file size is huge(>10K records present in a sheet).

Secondly, I tried using the following link: https://github.com/SheetJS/js-xlsx/issues/214#issuecomment-96843418 But I am getting the following error:

    f:\xxx\node_modules\xlsx\xlsx.js:2774
function decode_range(range) { var x =range.split(":").map(decode_cell); return {s:x[0],e:x[x.length-1]}; }
                                            ^

TypeError: Cannot read property 'split' of undefined

How can I resolve it? or are they any other method or modules that are available such that I can get data from either csv, xlsx, xls?

Thanks!

Upvotes: 6

Views: 21006

Answers (1)

sreepurna
sreepurna

Reputation: 1666

Can get the first n lines of the sheet with the help of the sheetRows option that is present.

So, the code looks as follows:

let workbook = xlsx.readFile(path, {sheetRows: 5})
 let sheetsList = workbook.SheetNames
 let sheetData = xlsx.utils.sheet_to_json(workbook.Sheets[sheetsList[i]], {
      header: 1,
      defval: '',
      blankrows: true
 });

Here I have limited to first 5 rows.

Thanks to all who tried in solving this problem. Special thanks to xlsx community member. Here is the link: https://github.com/SheetJS/js-xlsx/issues/1225

Upvotes: 15

Related Questions