Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Commit

Permalink
Add column limit support, similar to exceljs#541
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Apr 3, 2019
1 parent 5437312 commit 7e8517d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/xlsx/xform/sheet/row-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var BaseXform = require('../base-xform');

var CellXform = require('./cell-xform');

var RowXform = module.exports = function() {
var RowXform = module.exports = function(options) {
this.maxItems = options && options.maxItems;
this.map = {
c: new CellXform()
};
Expand Down Expand Up @@ -119,6 +120,9 @@ utils.inherits(RowXform, BaseXform, {
if (this.parser) {
if (!this.parser.parseClose(name)) {
this.model.cells.push(this.parser.model);
if (this.maxItems && this.model.cells.length > this.maxItems) {
throw new Error('Max column count exceeded');
}
this.parser = undefined;
}
return true;
Expand Down
3 changes: 2 additions & 1 deletion lib/xlsx/xform/sheet/worksheet-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ var RowBreaksXform = require('./row-breaks-xform');

var WorkSheetXform = module.exports = function(options) {
var maxRows = options && options.maxRows;
var maxCols = options && options.maxCols;
this.map = {
sheetPr: new SheetPropertiesXform(),
dimension: new DimensionXform(),
sheetViews: new ListXform({tag: 'sheetViews', count: false, childXform: new SheetViewXform()}),
sheetFormatPr: new SheetFormatPropertiesXform(),
cols: new ListXform({tag: 'cols', count: false, childXform: new ColXform()}),
sheetData: new ListXform({tag: 'sheetData', count: false, empty: true, childXform: new RowXform(), maxItems: maxRows}),
sheetData: new ListXform({tag: 'sheetData', count: false, empty: true, childXform: new RowXform({maxItems: maxCols}), maxItems: maxRows}),
autoFilter: new AutoFilterXform(),
mergeCells: new ListXform({tag: 'mergeCells', count: true, childXform: new MergeCellXform()}),
rowBreaks: new RowBreaksXform(),
Expand Down
Binary file added spec/integration/data/many-columns.xlsx
Binary file not shown.
29 changes: 29 additions & 0 deletions spec/integration/workbook-xlsx-reader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ describe('WorkbookReader', function() {
return workbook.xlsx.readFile('./spec/integration/data/fibonacci.xlsx', {maxRows: 20});
});
});

describe('Column limit', function() {
it('should bail out if the file contains more cells than the limit', function() {
const workbook = new Excel.Workbook();
// The many-columns sheet has 20 columns in row 2
return workbook.xlsx.readFile('./spec/integration/data/many-columns.xlsx', {maxCols: 15})
.then(function() {
throw new Error('Promise unexpectedly fulfilled');
}, function(err) {
expect(err.message).to.equal('Max column count exceeded');
});
});

it('should fail fast on a huge file', function() {
this.timeout(20000);
const workbook = new Excel.Workbook();
return workbook.xlsx.readFile('./spec/integration/data/huge.xlsx', {maxCols: 10})
.then(function() {
throw new Error('Promise unexpectedly fulfilled');
}, function(err) {
expect(err.message).to.equal('Max column count exceeded');
});
});

it('should parse fine if the limit is not exceeded', function() {
const workbook = new Excel.Workbook();
return workbook.xlsx.readFile('./spec/integration/data/many-columns.xlsx', {maxCols: 40});
});
});
});

describe('#read', function() {
Expand Down

0 comments on commit 7e8517d

Please sign in to comment.