From 604d4c22aed5238520d2a632f197d9d47ad533fb Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Fri, 11 May 2018 10:47:37 +0200 Subject: [PATCH 1/2] Fix misplaced it(...) in test --- spec/unit/utils/col-cache.spec.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/unit/utils/col-cache.spec.js b/spec/unit/utils/col-cache.spec.js index f504cae7e..1e7cf314c 100644 --- a/spec/unit/utils/col-cache.spec.js +++ b/spec/unit/utils/col-cache.spec.js @@ -66,13 +66,13 @@ 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'}); + }); - 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('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() { From 00a01f8f1fbec3c88d5af26a73130dbf2f709e03 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Fri, 11 May 2018 10:53:06 +0200 Subject: [PATCH 2/2] colCache.decodeAddress: Don't error on a malformed address --- lib/utils/col-cache.js | 23 ++++++++++++++++------- spec/unit/utils/col-cache.spec.js | 10 ++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/utils/col-cache.js b/lib/utils/col-cache.js index 9ab737c1f..e6590c399 100644 --- a/lib/utils/col-cache.js +++ b/lib/utils/col-cache.js @@ -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 diff --git a/spec/unit/utils/col-cache.spec.js b/spec/unit/utils/col-cache.spec.js index 1e7cf314c..9c2584e9e 100644 --- a/spec/unit/utils/col-cache.spec.js +++ b/spec/unit/utils/col-cache.spec.js @@ -68,6 +68,16 @@ describe('colCache', function() { 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('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'});