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

Commit c7c16d7

Browse files
committed
Add column limit support, similar to exceljs#541
(cherry picked from commit 7e8517d)
1 parent 04ee4c4 commit c7c16d7

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

lib/xlsx/xform/sheet/row-xform.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ var BaseXform = require('../base-xform');
1111

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

14-
var RowXform = module.exports = function() {
14+
var RowXform = module.exports = function(options) {
15+
this.maxItems = options && options.maxItems;
1516
this.map = {
1617
c: new CellXform()
1718
};
@@ -125,6 +126,9 @@ utils.inherits(RowXform, BaseXform, {
125126
if (this.parser) {
126127
if (!this.parser.parseClose(name)) {
127128
this.model.cells.push(this.parser.model);
129+
if (this.maxItems && this.model.cells.length > this.maxItems) {
130+
throw new Error('Max column count exceeded');
131+
}
128132
this.parser = undefined;
129133
}
130134
return true;

lib/xlsx/xform/sheet/worksheet-xform.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ var RowBreaksXform = require('./row-breaks-xform');
3636

3737
var WorkSheetXform = module.exports = function(options) {
3838
var maxRows = options && options.maxRows;
39+
var maxCols = options && options.maxCols;
3940
this.map = {
4041
sheetPr: new SheetPropertiesXform(),
4142
dimension: new DimensionXform(),
4243
sheetViews: new ListXform({tag: 'sheetViews', count: false, childXform: new SheetViewXform()}),
4344
sheetFormatPr: new SheetFormatPropertiesXform(),
4445
cols: new ListXform({tag: 'cols', count: false, childXform: new ColXform()}),
45-
sheetData: new ListXform({tag: 'sheetData', count: false, empty: true, childXform: new RowXform(), maxItems: maxRows}),
46+
sheetData: new ListXform({tag: 'sheetData', count: false, empty: true, childXform: new RowXform({maxItems: maxCols}), maxItems: maxRows}),
4647
autoFilter: new AutoFilterXform(),
4748
mergeCells: new ListXform({tag: 'mergeCells', count: true, childXform: new MergeCellXform()}),
4849
rowBreaks: new RowBreaksXform(),
8.68 KB
Binary file not shown.

spec/integration/workbook-xlsx-reader.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,35 @@ describe('WorkbookReader', function() {
5353
return workbook.xlsx.readFile('./spec/integration/data/fibonacci.xlsx', {maxRows: 20});
5454
});
5555
});
56+
57+
describe('Column limit', function() {
58+
it('should bail out if the file contains more cells than the limit', function() {
59+
const workbook = new Excel.Workbook();
60+
// The many-columns sheet has 20 columns in row 2
61+
return workbook.xlsx.readFile('./spec/integration/data/many-columns.xlsx', {maxCols: 15})
62+
.then(function() {
63+
throw new Error('Promise unexpectedly fulfilled');
64+
}, function(err) {
65+
expect(err.message).to.equal('Max column count exceeded');
66+
});
67+
});
68+
69+
it('should fail fast on a huge file', function() {
70+
this.timeout(20000);
71+
const workbook = new Excel.Workbook();
72+
return workbook.xlsx.readFile('./spec/integration/data/huge.xlsx', {maxCols: 10})
73+
.then(function() {
74+
throw new Error('Promise unexpectedly fulfilled');
75+
}, function(err) {
76+
expect(err.message).to.equal('Max column count exceeded');
77+
});
78+
});
79+
80+
it('should parse fine if the limit is not exceeded', function() {
81+
const workbook = new Excel.Workbook();
82+
return workbook.xlsx.readFile('./spec/integration/data/many-columns.xlsx', {maxCols: 40});
83+
});
84+
});
5685
});
5786

5887
describe('#read', function() {

0 commit comments

Comments
 (0)