Sheetjs: stream.to_json never ends

Created on 10 Sep 2018  路  3Comments  路  Source: SheetJS/sheetjs

   // If no header option provided, after emitting last item, stream do not emit `end` event

    const wb = XLSX.readFile(path);
    const ws = wb.Sheets[wb.SheetNames[0]];

    // This stream will `end`
    XLSX.stream.to_json(ws, { header: 1 });

    // But this one do not emit `end`
    XLSX.stream.to_json(ws);

Most helpful comment

@joostdebruijn @Alex0007 the row increment happens in the wrong place. Here's the patch:

--- a/bits/97_node.js
+++ b/bits/97_node.js
@@ -101,9 +101,9 @@ if(has_buf && typeof require != 'undefined') (function() {
                stream._read = function() {
                        if(R > r.e.r) return stream.push(null);
                        while(R <= r.e.r) {
-                               ++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;

Feel free to submit that as a PR.

All 3 comments

https://github.com/SheetJS/js-xlsx/blob/master/dist/xlsx.js#L20932

Problem is fixed when changing
> <=
to
>= <

But i'm not sure how to fix properly

I encountered the same problem and I think your solution is right. The statement at line 20932 is actually never hit, because the var R (which is the number of the row that is processed) will never be more than the total available rows in the sheet (which i think r.e.r. stands for).

I also noticed that the first row (after the heading row) is not streamed. However, the code at this point is not very readable so I don't feel very comfortable to make a PR for this.

@joostdebruijn @Alex0007 the row increment happens in the wrong place. Here's the patch:

--- a/bits/97_node.js
+++ b/bits/97_node.js
@@ -101,9 +101,9 @@ if(has_buf && typeof require != 'undefined') (function() {
                stream._read = function() {
                        if(R > r.e.r) return stream.push(null);
                        while(R <= r.e.r) {
-                               ++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;

Feel free to submit that as a PR.

Was this page helpful?
0 / 5 - 0 ratings