From b58d5703effe0539388bd9bfeff1da5f3fd98958 Mon Sep 17 00:00:00 2001 From: Martin Aspeli Date: Sun, 1 Jan 2017 21:20:24 +0000 Subject: [PATCH] Update when changing size of a table --- lib/index.js | 21 +++++++++++++++++++-- test/crud-test.js | 15 +++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 121cea4..9230bc0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -122,9 +122,11 @@ module.exports = (function() { var sheet = self.loadSheet(sheetName); - var sheetData = sheet.root.find("sheetData"), + var dimension = sheet.root.find("dimension"), + sheetData = sheet.root.find("sheetData"), currentRow = null, totalRowsInserted = 0, + totalColumnsInserted = 0, namedTables = self.loadTables(sheet.root, sheet.filename), rows = []; @@ -213,6 +215,11 @@ module.exports = (function() { // Update row spans attribute if(cellsInserted !== 0) { self.updateRowSpan(row, cellsInserted); + + if(cellsInserted > totalColumnsInserted) { + totalColumnsInserted = cellsInserted; + } + } // Add newly inserted rows @@ -232,7 +239,17 @@ module.exports = (function() { // Update placeholders in table column headers self.substituteTableColumnHeaders(namedTables, substitutions); - // TODO: Set + // Update if we added rows or columns + if(totalRowsInserted > 0 || totalColumnsInserted > 0) { + var dimensionRange = self.splitRange(dimension.attrib.ref), + dimensionEndRef = self.splitRef(dimensionRange.end); + + dimensionEndRef.row += totalRowsInserted; + dimensionEndRef.col = self.numToChar(self.charToNum(dimensionEndRef.col) + totalRowsInserted); + dimensionRange.end = self.joinRef(dimensionEndRef); + + dimension.attrib.ref = self.joinRange(dimensionRange); + } // Write back the modified XML trees self.archive.file(sheet.filename, etree.tostring(sheet.root)); diff --git a/test/crud-test.js b/test/crud-test.js index fc847fb..dcb7953 100644 --- a/test/crud-test.js +++ b/test/crud-test.js @@ -91,6 +91,9 @@ describe("CRUD operations", function() { var sharedStrings = etree.parse(t.archive.file("xl/sharedStrings.xml").asText()).getroot(), sheet1 = etree.parse(t.archive.file("xl/worksheets/sheet1.xml").asText()).getroot(); + // Dimensions should be updated + buster.expect(sheet1.find("./dimension").attrib.ref).toEqual("B2:F9"); + // extract date placeholder - interpolated into string referenced at B4 buster.expect(sheet1.find("./sheetData/row/c[@r='B4']").attrib.t).toEqual("s"); buster.expect( @@ -199,6 +202,9 @@ describe("CRUD operations", function() { var sharedStrings = etree.parse(t.archive.file("xl/sharedStrings.xml").asText()).getroot(), sheet1 = etree.parse(t.archive.file("xl/worksheets/sheet1.xml").asText()).getroot(); + // Dimensions should be updated + buster.expect(sheet1.find("./dimension").attrib.ref).toEqual("B2:F9"); + // extract date placeholder - interpolated into string referenced at B4 buster.expect(sheet1.find("./sheetData/row/c[@r='B4']").attrib.t).toEqual("s"); buster.expect( @@ -292,6 +298,9 @@ describe("CRUD operations", function() { var sharedStrings = etree.parse(t.archive.file("xl/sharedStrings.xml").asText()).getroot(), sheet1 = etree.parse(t.archive.file("xl/worksheets/sheet1.xml").asText()).getroot(); + // Dimensions should be set + buster.expect(sheet1.find("./dimension").attrib.ref).toEqual("B2:D6"); + // C4 should have moved left, and the old B4 should now be deleted buster.expect(sheet1.find("./sheetData/row/c[@r='B4']/v").text).toEqual("101"); buster.expect(sheet1.find("./sheetData/row/c[@r='C4']")).toBeNull(); @@ -344,6 +353,9 @@ describe("CRUD operations", function() { var sharedStrings = etree.parse(t.archive.file("xl/sharedStrings.xml").asText()).getroot(), sheet1 = etree.parse(t.archive.file("xl/worksheets/sheet1.xml").asText()).getroot(); + // Dimensions should be updated + buster.expect(sheet1.find("./dimension").attrib.ref).toEqual("B2:K17"); + // Marker above table hasn't moved buster.expect(sheet1.find("./sheetData/row/c[@r='B4']/v").text).toEqual("101"); @@ -485,6 +497,9 @@ describe("CRUD operations", function() { table2 = etree.parse(t.archive.file("xl/tables/table2.xml").asText()).getroot(), table3 = etree.parse(t.archive.file("xl/tables/table3.xml").asText()).getroot(); + // Dimensions should be updated + buster.expect(sheet1.find("./dimension").attrib.ref).toEqual("B2:L29"); + // Named ranges have moved buster.expect(workbook.find("./definedNames/definedName[@name='BelowTable']").text).toEqual("Tables!$B$18"); buster.expect(workbook.find("./definedNames/definedName[@name='Moving']").text).toEqual("Tables!$G$8");