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.stream.to_json doesn't ends when a blank row is the last row in Excel sheet.

See original GitHub issue

sheetjs/bits/97_node.js

		stream._read = function() {
 			if(R > r.e.r) return stream.push(null);
			while(R <= r.e.r) {
				//if ((rowinfo[R-1]||{}).hidden) continue;
				var row = make_json_row(sheet, r, R, cols, header, hdr, dense, o);
				++R;
				if((row.isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) {
					stream.push(row.row);
					break;
				}
			}
		};
		return stream;

Hi @SheetJSDev !

This piece of code has the R > r.e.r checking outside of the loop but when a blank row is inserted at the end of a given sheet then the if((row.isempty === false) || (header === 1 ? o.blankrows !== false : !!o.blankrows)) condition inside of the while loop isn’t executed and the stream does not end.

Ill be submitting a pull request for the same for a fix for this issue that I have found. Please verify and merge the changes as soon as possible as I am using this library in production in my system.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mpgo13commented, Jul 29, 2020

I’m having the same issue. I noticed that setting blankrows: true will emit the end event. Here my reproducible problem:

const xlsx = require('xlsx');
const workbook = xlsx.utils.book_new();
const worksheet = xlsx.utils.json_to_sheet([
  {
    a: '1',
    b: '2',
  },
  {},
  {
    a: '11',
    b: '22',
  },
  {},
  {},
]);
xlsx.utils.book_append_sheet(workbook, worksheet);

const stream = xlsx.stream.to_json(workbook.Sheets[workbook.SheetNames[0]], {
  //blankrows: true, // WORKS with blankrows = true
  header: ['a', 'b'],
});

let closed = false;

stream.on('data', console.log);
stream.on('close', () => console.log('CLOSE'));
stream.on('end', err => {
  console.log('END', err);

  closed = true;
});

// for the event loop
const interval = setInterval(() => {
  if (closed) {
    clearInterval(interval);
  }
}, 100);

1reaction
highflyingcommented, Mar 31, 2020

@highflying can you please elaborate more when you say “specify the range to skip processing the blank lines”

I know the range of the sheet that has data in it, so I am specifying it manually, e.g.

  const stream = XLSX.stream.to_json(sheet, {
    range: "A1:BN3",
  });

When doing this the end event gets emitted, without it no end event happens.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Locate and reset the last cell on a worksheet - Microsoft Support
To locate the last cell that contains data or formatting, click anywhere in the worksheet, and then press CTRL+END. Note: To select the...
Read more >
Finding the Last Row in an Excel Spreadsheet From Java
In this tutorial, we'll discuss how to find the last row in an Excel spreadsheet using Java and Apache POI.
Read more >
Parse XLSX with Node and create json - Stack Overflow
Yeah, by default it doesn't process blank cells but by passing an optional param to XLSX.readFile function is a way around. var workbook...
Read more >
read-excel-file - npm
Sidenote: When converting cell values to object properties, by default, it skips all null values (skips all empty cells). That's for simplicity.
Read more >
Pandas read_excel() - Reading Excel File in Python
If the excel sheet doesn't have any header row, pass the header parameter value as None. excel_data_df = pandas.read_excel('records.xlsx', ...
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