fix: cells added below a rowSpan cell take its formatting and paragraph alignment (#1219) (#3784)

This commit is contained in:
Dolan
2026-10-17 16:39:04 +01:00
committed by GitHub
parent c08ec0ae8a
commit 8f0a8532e4
4 changed files with 124 additions and 3 deletions
+2
View File
@@ -674,6 +674,8 @@ const doc = new Document({
When cell rows are merged, it counts as multiple rows, so be sure to remove excess cells. It is similar to how HTML's `rowspan` works.
https://www.w3schools.com/tags/att_td_rowspan.asp
docx adds a cell to each row below the first for the merged cell, as Word writes a merge. Those cells take the merged cell's width, borders, shading, margins, text direction and vertical alignment, and when the merged cell ends with a paragraph rather than a table, their paragraphs take that paragraph's alignment, so Word treats the merged cell as one when it lays it out and when you select it.
```ts
const cell = new TableCell({
...,
+13
View File
@@ -110,17 +110,20 @@ export type IParagraphOptions = {
*/
export class Paragraph extends FileChild {
private readonly properties: ParagraphProperties;
private readonly alignment: Pick<IParagraphOptions, "alignment" | "bidirectional">;
public constructor(options: string | IParagraphOptions) {
super("w:p");
if (typeof options === "string") {
this.alignment = {};
this.properties = new ParagraphProperties({});
this.root.push(this.properties);
this.root.push(new TextRun(options));
return this;
}
this.alignment = { alignment: options.alignment, bidirectional: options.bidirectional };
this.properties = new ParagraphProperties(options);
this.root.push(this.properties);
@@ -185,6 +188,16 @@ export class Paragraph extends FileChild {
this.properties.removeSectionProperties(sectionProperties);
}
/**
* The paragraph's alignment and direction, which a table gives the empty paragraphs of the cells it adds below a
* cell merged down rows (`rowSpan`), so that the merged cell is aligned as one.
*
* @internal
*/
public get Alignment(): Pick<IParagraphOptions, "alignment" | "bidirectional"> {
return this.alignment;
}
public addRunToFront(run: Run): Paragraph {
this.root.splice(1, 0, run);
return this;
+98 -1
View File
@@ -6,10 +6,12 @@ import { BorderStyle } from "@file/border";
import { PageOrientation } from "@file/document/body/section-properties/properties/page-size";
import { File } from "@file/file";
import { Header } from "@file/header";
import { ShadingType } from "@file/shading";
import { VerticalAlignTable } from "@file/vertical-align";
import { AlignmentType, Paragraph } from "../paragraph";
import { Table } from "./table";
import { TableCell } from "./table-cell";
import { TableCell, TextDirection } from "./table-cell";
import { RelativeHorizontalPosition, RelativeVerticalPosition, TableAnchorType } from "./table-properties";
import { TableLayoutType } from "./table-properties/table-layout";
import { TableRow } from "./table-row";
@@ -484,6 +486,101 @@ describe("Table", () => {
});
});
describe("#rowSpan", () => {
// The first cell of each row of a table, as formatted
const firstCellsOf = (table: Table): readonly any[] => {
const tree = new Formatter().format(table);
return tree["w:tbl"]
.filter((child: any) => child["w:tr"])
.map((row: any) => row["w:tr"].find((child: any) => child["w:tc"])["w:tc"]);
};
const mergedDownThreeRows = (cell: TableCell): Table =>
new Table({
rows: [
new TableRow({ children: [cell, new TableCell({ children: [new Paragraph("1")] })] }),
new TableRow({ children: [new TableCell({ children: [new Paragraph("2")] })] }),
new TableRow({ children: [new TableCell({ children: [new Paragraph("3")] })] }),
],
});
it("formats the cells it adds below a merged cell as the merged cell, all the way down", () => {
const cells = firstCellsOf(
mergedDownThreeRows(
new TableCell({
rowSpan: 3,
width: { size: 3000, type: WidthType.DXA },
borders: { left: { style: BorderStyle.DOUBLE, size: 6, color: "FF0000" } },
shading: { type: ShadingType.CLEAR, fill: "DDEEFF", color: "auto" },
margins: { top: 400, bottom: 400 },
textDirection: TextDirection.BOTTOM_TO_TOP_LEFT_TO_RIGHT,
verticalAlign: VerticalAlignTable.CENTER,
children: [new Paragraph("Merged")],
}),
),
);
// The same properties, but the merge's start
const properties = cells[0][0]["w:tcPr"].filter((property: any) => !property["w:vMerge"]);
for (const cell of cells.slice(1)) {
expect(cell[0]["w:tcPr"]).to.deep.equal([
properties[0],
{ "w:vMerge": { _attr: { "w:val": "continue" } } },
...properties.slice(1),
]);
}
expect(properties.map((property: any) => Object.keys(property)[0])).to.deep.equal([
"w:tcW",
"w:tcBorders",
"w:shd",
"w:tcMar",
"w:textDirection",
"w:vAlign",
]);
});
it("aligns the paragraphs of the cells it adds below a merged cell as the merged cell's last paragraph (#1219)", () => {
const cells = firstCellsOf(
mergedDownThreeRows(
new TableCell({
rowSpan: 3,
children: [new Paragraph("Title"), new Paragraph({ text: "Merged", alignment: AlignmentType.CENTER })],
}),
),
);
expect(cells[1].slice(1)).to.deep.equal([{ "w:p": [{ "w:pPr": [{ "w:jc": { _attr: { "w:val": "center" } } }] }] }]);
expect(cells[2].slice(1)).to.deep.equal(cells[1].slice(1));
});
it("keeps the side of the page a right-to-left merged cell is aligned to", () => {
const cells = firstCellsOf(
mergedDownThreeRows(
new TableCell({
rowSpan: 2,
children: [new Paragraph({ text: "Merged", bidirectional: true, alignment: AlignmentType.LEFT })],
}),
),
);
// A right-to-left paragraph aligned to the left of the page is written as aligned to its end
expect(cells[1].slice(1)).to.deep.equal([
{ "w:p": [{ "w:pPr": [{ "w:bidi": {} }, { "w:jc": { _attr: { "w:val": "right" } } }] }] },
]);
});
it("leaves the paragraphs of the cells it adds unaligned when the merged cell's last paragraph is, or it ends with a table", () => {
const nested = new Table({ rows: [new TableRow({ children: [new TableCell({ children: [new Paragraph("Nested")] })] })] });
for (const children of [
[new Paragraph("Merged")],
[new Paragraph({ text: "Merged", alignment: AlignmentType.CENTER }), nested],
]) {
const cells = firstCellsOf(mergedDownThreeRows(new TableCell({ rowSpan: 2, children })));
expect(cells[1]).to.deep.equal([{ "w:tcPr": [{ "w:vMerge": { _attr: { "w:val": "continue" } } }] }, { "w:p": {} }]);
}
});
});
describe("#keepNext", () => {
it("keeps the cells inserted below a cell merged down rows with the next row, in rows kept with the next", () => {
// A group of three rows with its name merged down the first column, kept on one page
+11 -2
View File
@@ -9,7 +9,7 @@ import { Body } from "@file/document/body/body";
import { FileChild } from "@file/file-child";
import type { IContext, IXmlableObject, XmlComponent } from "@file/xml-components";
import type { AlignmentType } from "../paragraph";
import { type AlignmentType, Paragraph } from "../paragraph";
import { DEFAULT_AVAILABLE_WIDTH, resolveColumnWidths, resolvePreferredWidth } from "./column-widths";
import { type ITableGridChangeOptions, TableGrid } from "./grid";
import { TableCell, VerticalMergeType } from "./table-cell";
@@ -150,12 +150,21 @@ export class Table extends FileChild {
// 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) {
// The inserted cell is part of the merged cell, so it is formatted as the merged cell is, as Word
// writes the cells of a merge. Word sizes the rows below by its margins, and selecting the merged
// cell selects its paragraph too, which is aligned as the merged cell's last paragraph
const lastChild = cell.options.children[cell.options.children.length - 1];
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,
width: cell.options.width,
borders: cell.options.borders,
children: [],
shading: cell.options.shading,
margins: cell.options.margins,
textDirection: cell.options.textDirection,
verticalAlign: cell.options.verticalAlign,
children: lastChild instanceof Paragraph ? [new Paragraph(lastChild.Alignment)] : [],
verticalMerge: VerticalMergeType.CONTINUE,
});
rows[rowIndex + 1].addCellToColumnIndex(continueCell, columnIndex);