Skip to content

Commit

Permalink
Re-add support for page break move and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
timo92 committed Aug 9, 2024
1 parent 7b8bb08 commit 40ecb38
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
69 changes: 35 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1707,50 +1707,51 @@ class Workbook {

const formulaParts = self.splitDefinedNameText(name.text, self.sheet.name);

formulaParts.forEach(function (part) {
if (!part.isRef || !part.ref.includes(self.sheet.name)) return;

if (self.isRange(part.ref)) {
const namedRange = self.splitRange(part.ref);
const namedRangeStart = self.splitRef(namedRange.start);
if (self.option && self.option.pushDownPageBreakOnTableSubstitution && name.attrib.name === "_xlnm.Print_Area") {
if (self.isRange(name.text)) {
const namedRange = self.splitRange(name.text);
const namedRangeEnd = self.splitRef(namedRange.end);

if (namedRangeStart.row > currentRow) {
namedRangeEnd.row += numRows;
name.text = self.joinRange({
start: namedRange.start,
end: self.joinRef(namedRangeEnd),
})
} else {
console.error("Illegal PageBreak, skipping adjustment");
}
} else {
formulaParts.forEach(function (part) {
if (!part.isRef || !part.ref.includes(self.sheet.name)) return;

namedRangeStart.row += numRows;
namedRangeEnd.row += numRows;
if (self.isRange(part.ref)) {
const namedRange = self.splitRange(part.ref);
const namedRangeStart = self.splitRef(namedRange.start);
const namedRangeEnd = self.splitRef(namedRange.end);

part.ref = self.joinRange({
start: self.joinRef(namedRangeStart),
end: self.joinRef(namedRangeEnd),
});
}
if (namedRangeStart.row > currentRow) {

/* TODO - what is it for, what does it do? Feels like this pushes the ref down a 2nd time
if (self.option && self.option.pushDownPageBreakOnTableSubstitution) {
if (self.sheet.name == part.ref.split("!")[0].replace(/'/gi, "")) {
if (namedRangeEnd.row > currentRow) {
namedRangeEnd.row += numRows;
part.ref = self.joinRange({
start: self.joinRef(namedRangeStart),
end: self.joinRef(namedRangeEnd),
});
}
namedRangeStart.row += numRows;
namedRangeEnd.row += numRows;

part.ref = self.joinRange({
start: self.joinRef(namedRangeStart),
end: self.joinRef(namedRangeEnd),
});
}
}
*/
} else {
const namedRef = self.splitRef(part.ref);
} else {
const namedRef = self.splitRef(part.ref);

if (namedRef.row > currentRow) {
namedRef.row += numRows;
if (namedRef.row > currentRow) {
namedRef.row += numRows;

part.ref = self.joinRef(namedRef);
part.ref = self.joinRef(namedRef);
}
}
}
});
});

name.text = self.joinDefinedNameText(formulaParts);
name.text = self.joinDefinedNameText(formulaParts);
}
});

// Update hyperlinks refs
Expand Down
20 changes: 12 additions & 8 deletions test/crud-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1343,19 +1343,23 @@ describe("CRUD operations", function() {
pushDownPageBreakOnTableSubstitution : true
};
var t = new XlsxTemplate(buffer, option);
var data = {
myarray : [
{name : "foo"},
{name : "john"},
{name : "doe"},
t.substitute(1, {
users: [
{
name: "John",
surname : "Smith"
},
{
name: "John",
surname : "Doe"
}
]
};
t.substitute(1, data);
});
var newData = t.generate();
var workbook = etree.parse(t.archive.file("xl/workbook.xml").asText()).getroot();

workbook.findall("definedNames/definedName").forEach(function(name) {
expect(name.text === "Feuil1!$A$1:$J$12");
expect(name.text === "Feuil1!$A$1:$J$13");
});

fs.writeFileSync("test/output/movePageBreakOption.xlsx", newData, "binary");
Expand Down
Binary file modified test/templates/test-movePageBreakOption.xlsx
Binary file not shown.

0 comments on commit 40ecb38

Please sign in to comment.