Skip to content

Commit

Permalink
Merge pull request exceljs#332 from peakon/stream-read-fixes
Browse files Browse the repository at this point in the history
Assortment of fixes for streaming read
  • Loading branch information
guyonroche authored Jun 6, 2017
2 parents 5d00b62 + 50a52db commit 1a8d250
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/stream/xlsx/hyperlink-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ utils.inherits(HyperlinkReader, events.EventEmitter, {
}
if (!emitHyperlinks && !hyperlinks) {
entry.autodrain();
self.emit('finished');
return;
}

Expand Down
22 changes: 17 additions & 5 deletions lib/stream/xlsx/workbook-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ var utils = require('../../utils/utils');
var FlowControl = require('../../utils/flow-control');

var StyleManager = require('../../xlsx/xform/style/styles-xform');
var WorkbookPropertiesManager = require('../../xlsx/xform/book/workbook-properties-xform');

var WorksheetReader = require('./worksheet-reader');
var HyperlinkReader = require('./hyperlink-reader');

var WorkbookReader = module.exports = function(options) {
this.options = options = options || {};

// until we actually parse a styles.xml file, just assume we're not handling styles
// (but we do need to handle dates)
this.styles = new StyleManager.Mock();
this.styles = new StyleManager();
this.styles.init();

this.properties = new WorkbookPropertiesManager();

// worksheet readers, indexed by sheetNo
this.worksheetReaders = {};
Expand Down Expand Up @@ -73,10 +75,12 @@ utils.inherits(WorkbookReader, events.EventEmitter, {
var match, sheetNo;
switch (entry.path) {
case '_rels/.rels':
case 'xl/workbook.xml':
case 'xl/_rels/workbook.xml.rels':
entry.autodrain();
break;
case 'xl/workbook.xml':
this._parseWorkbook(entry, options);
break;
case 'xl/sharedStrings.xml':
this._parseSharedStrings(entry, options);
break;
Expand Down Expand Up @@ -107,6 +111,10 @@ utils.inherits(WorkbookReader, events.EventEmitter, {
}
});

zip.on('error', err => {
this.emit('error', err);
});

// Pipe stream into top flow-control
// this.flowControl.pipe(zip);
stream.pipe(zip);
Expand All @@ -116,6 +124,10 @@ utils.inherits(WorkbookReader, events.EventEmitter, {
this.emit('entry', payload);
}
},
_parseWorkbook: function (entry, options) {
this._emitEntry(options, {type: 'workbook'});
this.properties.parseStream(entry);
},
_parseSharedStrings: function(entry, options) {
this._emitEntry(options, {type: 'shared-strings'});
var self = this;
Expand Down Expand Up @@ -166,7 +178,7 @@ utils.inherits(WorkbookReader, events.EventEmitter, {
return;
}
this.styles = new StyleManager();
this.styles.parse(entry);
this.styles.parseStream(entry);
},
_getReader: function(Type, collection, sheetNo) {
var self = this;
Expand Down
20 changes: 17 additions & 3 deletions lib/stream/xlsx/worksheet-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,14 @@ utils.inherits(WorksheetReader, events.EventEmitter, {
}
if (!emitSheet && !emitHyperlinks && !hyperlinks) {
entry.autodrain();
this.emit('finished');
return;
}

// references
var sharedStrings = this.workbook.sharedStrings;
var styles = this.workbook.styles;
var properties = this.workbook.properties;

// xml position
var inCols = false;
Expand Down Expand Up @@ -169,7 +171,10 @@ utils.inherits(WorksheetReader, events.EventEmitter, {
}
if (node.attributes.s) {
var styleId = parseInt(node.attributes.s, 10);
row.style = styles.getStyleModel(styleId);
var style = styles.getStyleModel(styleId);
if (style) {
row.style = style;
}
}
}
break;
Expand Down Expand Up @@ -257,7 +262,10 @@ utils.inherits(WorksheetReader, events.EventEmitter, {
var address = colCache.decodeAddress(c.ref);
var cell = row.getCell(address.col);
if (c.s) {
cell.style = this.workbook.styles.getStyleModel(c.s);
var style = styles.getStyleModel(c.s);
if (style) {
cell.style = style;
}
}

if (c.f) {
Expand Down Expand Up @@ -287,9 +295,15 @@ utils.inherits(WorksheetReader, events.EventEmitter, {
case 'str':
cell.value = utils.xmlDecode(c.v.text);
break;
case 'e':
cell.value = { error: c.v.text };
break;
case 'b':
cell.value = parseInt(c.v.text, 10) !== 0;
break;
default:
if (utils.isDateFmt(cell.numFmt)) {
cell.value = utils.excelToDate(parseFloat(c.v.text));
cell.value = utils.excelToDate(parseFloat(c.v.text), properties.model.date1904);
} else {
cell.value = parseFloat(c.v.text);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/xlsx/xform/style/styles-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,14 @@ utils.inherits(StylesXform, BaseXform, {
// given a styleId (i.e. s="n"), get the cell's style model
// objects are shared where possible.
getStyleModel: function(id) {
// have we built this model before?
var model = this.index.model[id];
if (model) return model;

// if the style doesn't exist return null
var style = this.model.styles[id];
if (!style) return null;

// have we built this model before?
var model = this.index.model[id];
if (model) return model;

// build a new model
model = this.index.model[id] = {};

Expand Down

0 comments on commit 1a8d250

Please sign in to comment.