Update <dimension /> when changing size of a table

This commit is contained in:
Martin Aspeli
2017-01-01 21:20:24 +00:00
parent 42dff30787
commit b58d5703ef
2 changed files with 34 additions and 2 deletions
+19 -2
View File
@@ -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 <dimension />
// Update <dimension /> 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));
+15
View File
@@ -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");