Skip to content

Commit

Permalink
Merged line table (#188)
Browse files Browse the repository at this point in the history
* fix merged line table issue
* fix more complex merged table

---------
Co-authored-by: muyoungko <muyoungko@11stcorp.com>
  • Loading branch information
muyoungko authored Mar 9, 2024
1 parent eb603ea commit 128dfda
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,30 @@ class Workbook {
newRow.append(newCell);
}

// check merged and if yes, add merged cell with proper shape
let mergeCell = self.sheet.root.findall("mergeCells/mergeCell")
.find(c => self.splitRange(c.attrib.ref).start === cell.attrib.r)
let isMergeCell = mergeCell != null

if(isMergeCell) {
let originalMergeRange = self.splitRange(mergeCell.attrib.ref),
originalMergeStart = self.splitRef(originalMergeRange.start),
originalMergeEnd = self.splitRef(originalMergeRange.end);

for (let colnum = self.charToNum(originalMergeStart.col) + 1; colnum <= self.charToNum(originalMergeEnd.col); colnum++) {
const originalRow = self.sheet.root.find('sheetData')._children.find(f=>f.attrib.r == originalMergeStart.row)
let col = self.numToChar(colnum)
let originalCell = originalRow._children.find(f=>f.attrib.r.startsWith(col))

const addtionalCell = self.cloneElement(originalCell);
addtionalCell.attrib.r = self.joinRef({
row: newRow.attrib.r,
col: self.numToChar(colnum)
});
newRow.append(addtionalCell);
}
}

// expand named table range if necessary
parentTables.forEach(function (namedTable) {
var tableRoot = namedTable.root, autoFilter = tableRoot.find("autoFilter"), range = self.splitRange(tableRoot.attrib.ref);
Expand Down
51 changes: 51 additions & 0 deletions test/crud-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,57 @@ describe("CRUD operations", function() {

});

it("moves rows down when filling tables with merged line cell", function(done) {

fs.readFile(path.join(__dirname, "templates", "test-tables-merged-line.xlsx"), function(err, data) {
expect(err).toBeNull();

var t = new XlsxTemplate(data);

t.substitute("Tables", {
score_first: {name: "Jason", score: 1},
scores: [
{name: "John", score: 100, extra:'O'},
{name: "Bob", score: 110, extra:'O'},
{name: "Jim", score: 120, extra:'O'}
],
score_last: {name: "Fox", score: 99},

score2_first: {name: "Daddy", score: 1},
scores2: [
{name: "Son1", score: 100, extra:'O'},
{name: "Son2", score: 110, extra:'O'},
{name: "Son3", score: 120, extra:'O'},
{name: "Son4", score: 130, extra:'O'}
],
score2_last: {name: "Mom", score: 99},
});

var newData = t.generate();

var sharedStrings = etree.parse(t.archive.file("xl/sharedStrings.xml").asText()).getroot(),
sheet1 = etree.parse(t.archive.file("xl/worksheets/sheet1.xml").asText()).getroot();

expect(sheet1.find("./sheetData/row[@r='8']/c[@r='B8']").attrib.s).toEqual("3");
expect(sheet1.find("./sheetData/row[@r='8']/c[@r='B8']/v").text).toEqual("23");
expect(sheet1.find("./sheetData/row[@r='8']/c[@r='C8']").attrib.s).toEqual("4");

expect(sheet1.find("./sheetData/row[@r='8']/c[@r='D8']").attrib.s).toEqual("5");
expect(sheet1.find("./sheetData/row[@r='8']/c[@r='E8']").attrib.s).toEqual("6");
expect(sheet1.find("./sheetData/row[@r='8']/c[@r='F8']").attrib.s).toEqual("7");

expect(sheet1.find("./sheetData/row[@r='18']/c[@r='B18']").attrib.s).toEqual("3");
expect(sheet1.find("./sheetData/row[@r='18']/c[@r='C18']").attrib.s).toEqual("4");
expect(sheet1.find("./sheetData/row[@r='18']/c[@r='D18']").attrib.s).toEqual("5");

// XXX: For debugging only
fs.writeFileSync("test/output/test-tables-merged-line-out.xlsx", newData, "binary");

done();
});

});

it("replaces hyperlinks in sheet", function(done) {
fs.readFile(path.join(__dirname, "templates", "test-hyperlinks.xlsx"), function(err, data) {
expect(err).toBeNull();
Expand Down
Binary file added test/templates/test-tables-merged-line.xlsx
Binary file not shown.

0 comments on commit 128dfda

Please sign in to comment.