Compare commits

...

5 Commits
5.2.1 ... 5.2.2

Author SHA1 Message Date
b8232f7a02 Version bump 2020-07-11 19:34:29 +01:00
49eadb0efc Merge pull request #559 from wangfengming/master
:fix: `rowSpan` can't work when column index out of range
2020-07-09 01:29:06 +01:00
40dc90e585 :fix: insert the continue cell properly 2020-07-08 12:32:01 +08:00
0de302d192 :typo: update comments 2020-07-08 11:26:24 +08:00
80bab95f6c :fix: rowSpan can't work when column index out of range 2020-07-08 10:55:15 +08:00
6 changed files with 67 additions and 30 deletions

View File

@ -287,6 +287,7 @@ const table7 = new Table({
}),
new TableCell({
children: [new Paragraph("0,3")],
rowSpan: 3,
}),
],
}),
@ -296,9 +297,6 @@ const table7 = new Table({
children: [new Paragraph("1,0")],
columnSpan: 2,
}),
new TableCell({
children: [new Paragraph("1,3")],
}),
],
}),
new TableRow({
@ -311,9 +309,6 @@ const table7 = new Table({
children: [new Paragraph("2,2")],
rowSpan: 2,
}),
new TableCell({
children: [new Paragraph("2,3")],
}),
],
}),
new TableRow({
@ -336,6 +331,41 @@ const table7 = new Table({
},
});
const table8 = new Table({
rows: [
new TableRow({
children: [
new TableCell({ children: [new Paragraph("1,1")] }),
new TableCell({ children: [new Paragraph("1,2")] }),
new TableCell({ children: [new Paragraph("1,3")] }),
new TableCell({ children: [new Paragraph("1,4")], rowSpan: 4, borders }),
],
}),
new TableRow({
children: [
new TableCell({ children: [new Paragraph("2,1")] }),
new TableCell({ children: [new Paragraph("2,2")] }),
new TableCell({ children: [new Paragraph("2,3")], rowSpan: 3 }),
],
}),
new TableRow({
children: [
new TableCell({ children: [new Paragraph("3,1")] }),
new TableCell({ children: [new Paragraph("3,2")], rowSpan: 2 }),
],
}),
new TableRow({
children: [
new TableCell({ children: [new Paragraph("4,1")] }),
],
}),
],
width: {
size: 100,
type: WidthType.PERCENTAGE,
},
});
doc.addSection({
children: [
table,
@ -357,6 +387,8 @@ doc.addSection({
table6,
new Paragraph("Merging columns 4"),
table7,
new Paragraph("Merging columns 5"),
table8,
],
});

View File

@ -1,6 +1,6 @@
{
"name": "docx",
"version": "5.2.1",
"version": "5.2.2",
"description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.",
"main": "build/index.js",
"scripts": {

View File

@ -70,6 +70,9 @@ export class TableCell extends XmlComponent {
if (options.verticalMerge) {
this.properties.addVerticalMerge(options.verticalMerge);
} else if (options.rowSpan && options.rowSpan > 1) {
// if cell already have a `verticalMerge`, don't handle `rowSpan`
this.properties.addVerticalMerge(VerticalMergeType.RESTART);
}
if (options.margins) {
@ -84,10 +87,6 @@ export class TableCell extends XmlComponent {
this.properties.addGridSpan(options.columnSpan);
}
if (options.rowSpan && options.rowSpan > 1) {
this.properties.addVerticalMerge(VerticalMergeType.RESTART);
}
if (options.width) {
this.properties.setWidth(options.width.size, options.width.type);
}

View File

@ -271,7 +271,8 @@ describe("TableRow", () => {
});
expect(tableRow.columnIndexToRootIndex(8, true)).to.equal(5);
expect(() => tableRow.columnIndexToRootIndex(9, true)).to.throw(`cell 'columnIndex' should not great than 8`);
// for column 10, just place the new cell at the end of row
expect(tableRow.columnIndexToRootIndex(10, true)).to.equal(5);
});
});
});

View File

@ -83,10 +83,14 @@ export class TableRow extends XmlComponent {
let colIdx = 0;
// Offset because properties is also in root.
let rootIdx = 1;
const endRootIndex = allowEndNewCell ? this.root.length : this.root.length - 1;
while (colIdx <= columnIndex) {
if (rootIdx > endRootIndex) {
throw new Error(`cell 'columnIndex' should not great than ${colIdx - 1}`);
if (rootIdx >= this.root.length) {
if (allowEndNewCell) {
// for inserting verticalMerge CONTINUE cell at end of row
return this.root.length;
} else {
throw new Error(`cell 'columnIndex' should not great than ${colIdx - 1}`);
}
}
const cell = this.root[rootIdx] as TableCell;
rootIdx += 1;

View File

@ -79,25 +79,26 @@ export class Table extends XmlComponent {
}
rows.forEach((row, rowIndex) => {
row.cells.forEach((cell, cellIndex) => {
if (rowIndex === rows.length - 1) {
// don't process the end row
return;
}
let columnIndex = 0;
row.cells.forEach((cell) => {
// Row Span has to be added in this method and not the constructor because it needs to know information about the column which happens after Table Cell construction
// Row Span of 1 will crash word as it will add RESTART and not a corresponding CONTINUE
if (cell.options.rowSpan && cell.options.rowSpan > 1) {
const columnIndex = row.rootIndexToColumnIndex(cellIndex + 1);
const startRowIndex = rowIndex + 1;
const endRowIndex = rowIndex + (cell.options.rowSpan - 1);
for (let i = startRowIndex; i <= endRowIndex; i++) {
rows[i].addCellToColumnIndex(
new TableCell({
columnSpan: cell.options.columnSpan,
borders: cell.options.borders,
children: [],
verticalMerge: VerticalMergeType.CONTINUE,
}),
columnIndex,
);
}
const continueCell = new TableCell({
// the inserted CONTINUE cell has rowSpan, and will be handled when process the next row
rowSpan: cell.options.rowSpan - 1,
columnSpan: cell.options.columnSpan,
borders: cell.options.borders,
children: [],
verticalMerge: VerticalMergeType.CONTINUE,
});
rows[rowIndex + 1].addCellToColumnIndex(continueCell, columnIndex);
}
columnIndex += cell.options.columnSpan || 1;
});
});