Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Too few data or empty worksheet generate malformed excel file #99

Closed
bluedge opened this issue Apr 2, 2016 · 1 comment
Closed

Too few data or empty worksheet generate malformed excel file #99

bluedge opened this issue Apr 2, 2016 · 1 comment

Comments

@bluedge
Copy link

bluedge commented Apr 2, 2016

In streaming mode I came across this bug. If you add only one row to a worksheet OR add a worksheet that you do not fill the Excel file will be malformed. See the exemple below, uncomment the second line of push() to see the bug vanish.
Note: removing the worksheet columns header produce the same bug.

var stream      = require('stream');
var util            = require('util');
var Excel           = require('exceljs');
var fs          = require('fs');

var writableStream  = fs.createWriteStream('./streamed1.xlsx');

var ExcelTransform = function(options) {
    stream.Transform.call(this, {
        writableObjectMode: true,
        readableObjectMode: false
    });
        this.workbook = options.workbook;
    var self = this;

    this.workbook.stream.on('readable', function() {
        var chunk = workbook.stream.read();
        self.push(chunk);
    });
    this.worksheet = options.worksheet;
}
util.inherits(ExcelTransform, stream.Transform);

ExcelTransform.prototype._transform = function(doc, encoding, callback) {
    this.worksheet.addRow({
        name: doc.name
    }).commit();
    callback();
};

ExcelTransform.prototype._flush = function(callback) {
    this.workbook.commit(); // final commit
};

// it's better to provide the workbook as a parameter to the ExcelTransform
var workbook = new Excel.stream.xlsx.WorkbookWriter();
var worksheet = workbook.addWorksheet('sheet 1');
worksheet.columns = [{
    header: 'Name',
    key: 'name'
}];

var rs = new stream.Readable({ objectMode: true });
rs.push({ name: 'one' });
// rs.push({ name: 'two' }); // uncomment me to see the bug vanish
rs.push(null);

rs.pipe(new ExcelTransform({
    workbook: workbook,
    worksheet: worksheet
})).pipe(writableStream);
guyonroche pushed a commit that referenced this issue Feb 12, 2017
@guyonroche
Copy link
Collaborator

Fixed in 0.2.43

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants