Skip to content

Fix misinterpreted ranges from <definedName> #636

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

Merged
merged 3 commits into from
Feb 7, 2019
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
18 changes: 15 additions & 3 deletions lib/xlsx/xform/book/defined-name-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

var utils = require('../../../utils/utils');
var BaseXform = require('../base-xform');
var colCache = require('../../../utils/col-cache');

var DefinedNamesXform = module.exports = function() {
};
Expand Down Expand Up @@ -51,6 +52,15 @@ utils.inherits(DefinedNamesXform, BaseXform, {
}
});

function isValidRange(range) {
try {
colCache.decodeEx(range);
return true;
} catch (err) {
return false;
}
}

function extractRanges(parsedText) {
var ranges = [];
var quotesOpened = false;
Expand All @@ -64,18 +74,20 @@ function extractRanges(parsedText) {
if (!quotes) {
if (quotesOpened) {
last += item + ',';
} else {
} else if (isValidRange(item)) {
ranges.push(item);
}
return;
}
var quotesEven = quotes % 2 === 0;

if (!quotesOpened && quotesEven) {
if (!quotesOpened && quotesEven && isValidRange(item)) {
ranges.push(item);
} else if (quotesOpened && !quotesEven) {
quotesOpened = false;
ranges.push(last + item);
if (isValidRange(last + item)) {
ranges.push(last + item);
}
last = '';
} else {
quotesOpened = true;
Expand Down
Binary file added spec/integration/data/bogus-defined-name.xlsx
Binary file not shown.
7 changes: 7 additions & 0 deletions spec/integration/workbook-xlsx-reader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,11 @@ describe('WorkbookReader', function() {
expect(cell.value).to.equal('_x000D_');
});
});

describe('with a spreadsheet containing a defined name that kinda looks like it contains a range', function() {
it('should not crash', function() {
var workbook = new Excel.Workbook();
return workbook.xlsx.read(fs.createReadStream('./spec/integration/data/bogus-defined-name.xlsx'));
});
});
});
10 changes: 9 additions & 1 deletion spec/unit/xlsx/xform/book/defined-name-xform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ var expectations = [
xml: '<definedName name="_xlnm.Print_Area" localSheetId="0">bar!$A$1:$C$10</definedName>',
parsedModel: {name: '_xlnm.Print_Area', localSheetId: 0, ranges: ['bar!$A$1:$C$10']},
tests: ['render', 'renderIn', 'parse']
}
},
{
title: 'String with something that looks like a range',
create: function() { return new DefinedNameXform(); },
preparedModel: {name: 'foo', ranges: []},
xml: '<definedName name="foo">"OFFSET($A$10;0;0;0;1)"</definedName>',
parsedModel: {name: 'foo', ranges: []},
tests: ['parse']
},
];

describe('DefinedNameXform', function() {
Expand Down