Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions lib/stream/xlsx/workbook-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var WorkbookPropertiesManager = require('../../xlsx/xform/book/workbook-properti
var WorksheetReader = require('./worksheet-reader');
var HyperlinkReader = require('./hyperlink-reader');

var Temp = require('temp');

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

Expand All @@ -40,6 +42,9 @@ var WorkbookReader = module.exports = function(options) {

// end of stream check
this.atEnd = false;

Temp.track();
this.waitingWorkSheets = [];
};

utils.inherits(WorkbookReader, events.EventEmitter, {
Expand Down Expand Up @@ -73,6 +78,7 @@ utils.inherits(WorkbookReader, events.EventEmitter, {

zip.on('entry', entry => {
var match, sheetNo;
// console.log(entry.path);
switch (entry.path) {
case '_rels/.rels':
case 'xl/_rels/workbook.xml.rels':
Expand All @@ -91,7 +97,13 @@ utils.inherits(WorkbookReader, events.EventEmitter, {
if (entry.path.match(/xl\/worksheets\/sheet\d+[.]xml/)) {
match = entry.path.match(/xl\/worksheets\/sheet(\d+)[.]xml/);
sheetNo = match[1];
this._parseWorksheet(entry, sheetNo, options);
if (this.sharedStrings) {
this._parseWorksheet(entry, sheetNo, options);
} else {
var stream = Temp.createWriteStream();
this.waitingWorkSheets.push({sheetNo: sheetNo, options: options, path: stream.path});
entry.pipe(stream);
}
} else if (entry.path.match(/xl\/worksheets\/_rels\/sheet\d+[.]xml.rels/)) {
match = entry.path.match(/xl\/worksheets\/_rels\/sheet(\d+)[.]xml.rels/);
sheetNo = match[1];
Expand All @@ -104,11 +116,44 @@ utils.inherits(WorkbookReader, events.EventEmitter, {
});

zip.on('close', () => {
this.emit('end');
this.atEnd = true;
if (!this.readers) {
this.emit('finished');
var self = this;
if (this.waitingWorkSheets.length) {
var currentBook = 0;

var processBooks = function () {
var worksheetInfo = self.waitingWorkSheets[currentBook];
var entry = fs.createReadStream(worksheetInfo.path);

var sheetNo = worksheetInfo.sheetNo;
var options = worksheetInfo.options;
var worksheet = self._parseWorksheet(entry, sheetNo, options);

worksheet.on('finished', function (node) {
++currentBook;
if (currentBook == self.waitingWorkSheets.length) {
Temp.cleanupSync();
// setImmediate(this.emit.bind(this), 'finished');

self.emit('end');
self.atEnd = true;
if (!self.readers) {
self.emit('finished');
}
} else {
setImmediate(processBooks);
}
})
}
setImmediate(processBooks);
} else {
this.emit('end');
this.atEnd = true;
if (!this.readers) {
this.emit('finished');
}
}


});

zip.on('error', err => {
Expand Down Expand Up @@ -204,6 +249,7 @@ utils.inherits(WorkbookReader, events.EventEmitter, {
this.emit('worksheet', worksheetReader);
}
worksheetReader.read(entry, options, this.hyperlinkReaders[sheetNo]);
return worksheetReader;
},
_parseHyperlinks: function(entry, sheetNo, options) {
this._emitEntry(options, {type: 'hyerlinks', id: sheetNo});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"moment": ">=2.19.3",
"node-unzip-2": "^0.2.7",
"promish": ">=5.0.2",
"sax": "^1.2.2"
"sax": "^1.2.2",
"temp": "0.8.3"
},
"devDependencies": {
"@types/node": "*",
Expand Down