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
23 changes: 16 additions & 7 deletions lib/utils/col-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,29 @@ var colCache = module.exports = {
if (addr) {
return addr;
}

var col = value.match(/[A-Z]+/)[0];
var colNumber = this.l2n(col);
var row = value.match(/\d+/)[0];
var rowNumber = parseInt(row, 10);
var matchCol = value.match(/[A-Z]+/);
var col;
var colNumber;
if (matchCol) {
col = matchCol[0];
colNumber = this.l2n(col);
}
var matchRow = value.match(/\d+/);
var row;
var rowNumber;
if (matchRow) {
row = matchRow[0];
rowNumber = parseInt(row, 10);
}

// in case $row$col
value = col + row;
value = (col || '') + (row || '');

var address = {
address: value,
col: colNumber,
row: rowNumber,
$col$row: '$' + col + '$' + row
$col$row: '$' + (col || '') + '$' + (row || '')
};

// mem fix - cache only the tl 100x100 square
Expand Down
20 changes: 15 additions & 5 deletions spec/unit/utils/col-cache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,25 @@ describe('colCache', function() {
it('decodes addresses', function() {
expect(colCache.decodeAddress('A1')).to.deep.equal({address: 'A1', col: 1, row: 1, $col$row: '$A$1'});
expect(colCache.decodeAddress('AA11')).to.deep.equal({address: 'AA11', col: 27, row: 11, $col$row: '$AA$11'});
});

describe('with a malformed address', function() {
it('tolerates a missing row number', function() {
expect(colCache.decodeAddress('$B')).to.deep.equal({address: 'B', col: 2, row: undefined, $col$row: '$B$'});
});

it('convert [sheetName!][$]col[$]row[[$]col[$]row] into address or range structures', function() {
expect(colCache.decodeEx('Sheet1!$H$1')).to.deep.equal({$col$row: '$H$1', address: 'H1', col: 8, row: 1, sheetName: 'Sheet1'});
expect(colCache.decodeEx("'Sheet 1'!$H$1")).to.deep.equal({$col$row: '$H$1', address: 'H1', col: 8, row: 1, sheetName: 'Sheet 1'});
expect(colCache.decodeEx("'Sheet !$:1'!$H$1")).to.deep.equal({$col$row: '$H$1', address: 'H1', col: 8, row: 1, sheetName: 'Sheet !$:1'});
expect(colCache.decodeEx("'Sheet !$:1'!#REF!")).to.deep.equal({sheetName: 'Sheet !$:1', error: '#REF!'});
it('tolerates a missing column number', function() {
expect(colCache.decodeAddress('$2')).to.deep.equal({address: '2', col: undefined, row: 2, $col$row: '$$2'});
});
});

it('convert [sheetName!][$]col[$]row[[$]col[$]row] into address or range structures', function() {
expect(colCache.decodeEx('Sheet1!$H$1')).to.deep.equal({$col$row: '$H$1', address: 'H1', col: 8, row: 1, sheetName: 'Sheet1'});
expect(colCache.decodeEx("'Sheet 1'!$H$1")).to.deep.equal({$col$row: '$H$1', address: 'H1', col: 8, row: 1, sheetName: 'Sheet 1'});
expect(colCache.decodeEx("'Sheet !$:1'!$H$1")).to.deep.equal({$col$row: '$H$1', address: 'H1', col: 8, row: 1, sheetName: 'Sheet !$:1'});
expect(colCache.decodeEx("'Sheet !$:1'!#REF!")).to.deep.equal({sheetName: 'Sheet !$:1', error: '#REF!'});
});

it('gets address structures (and caches them)', function() {
var addr = colCache.getAddress('D5');
expect(addr.address).to.equal('D5');
Expand Down