Table in table (#242)

* feat: Support nested table for now (comment is not supported)

* 0.0.149
main
bokuweb 2021-03-15 16:47:22 +09:00 committed by GitHub
parent ffd03e0b12
commit 1f42839787
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 117 additions and 10 deletions

View File

@ -17,6 +17,7 @@ pub struct TableCell {
#[derive(Debug, Clone, PartialEq)]
pub enum TableCellContent {
Paragraph(Paragraph),
Table(Table),
}
impl Serialize for TableCellContent {
@ -31,6 +32,12 @@ impl Serialize for TableCellContent {
t.serialize_field("data", s)?;
t.end()
}
TableCellContent::Table(ref s) => {
let mut t = serializer.serialize_struct("Table", 2)?;
t.serialize_field("type", "table")?;
t.serialize_field("data", s)?;
t.end()
}
}
}
}
@ -48,6 +55,14 @@ impl TableCell {
self
}
pub fn add_table(mut self, t: Table) -> TableCell {
if t.has_numbering {
self.has_numbering = true
}
self.children.push(TableCellContent::Table(t));
self
}
pub fn vertical_merge(mut self, t: VMergeType) -> TableCell {
self.property = self.property.vertical_merge(t);
self
@ -113,6 +128,13 @@ impl BuildXML for TableCell {
for c in &self.children {
match c {
TableCellContent::Paragraph(p) => b = b.add_child(p),
TableCellContent::Table(t) => {
b = b.add_child(t);
// INFO: We need to add empty paragraph when parent cell includes only cell.
if self.children.len() == 1 {
b = b.add_child(&Paragraph::new())
}
}
}
}
b.close().build()

View File

@ -326,6 +326,9 @@ impl Docx {
}
}
}
TableCellContent::Table(_) => {
// TODO: correct comment
}
}
}
}
@ -398,6 +401,9 @@ impl Docx {
}
}
}
TableCellContent::Table(_) => {
// TODO: correct comment
}
}
}
}
@ -509,6 +515,9 @@ impl Docx {
}
}
}
TableCellContent::Table(_) => {
// TODO: support comment
}
}
}
}
@ -570,6 +579,9 @@ impl Docx {
}
}
}
TableCellContent::Table(_) => {
// TODO: support comment
}
}
}
}

View File

@ -437,9 +437,14 @@ export class Docx {
buildCell(c: TableCell) {
let cell = wasm.createTableCell();
c.children.forEach((p) => {
const paragraph = this.buildParagraph(p);
cell = cell.add_paragraph(paragraph);
c.children.forEach((c) => {
if (c instanceof Paragraph) {
const paragraph = this.buildParagraph(c);
cell = cell.add_paragraph(paragraph);
} else if (c instanceof Table) {
const table = this.buildTable(c);
cell = cell.add_table(table);
}
});
if (c.property.verticalMerge === "continue") {

View File

@ -1,4 +1,5 @@
import { Paragraph } from "./paragraph";
import { Table } from "./table";
import { TableCellBorders, PositionKeys } from "./table-cell-borders";
import { BorderPosition, TableCellBorder } from "./table-cell-border";
import * as wasm from "./pkg";
@ -58,7 +59,7 @@ export type CellProperty = {
};
export class TableCell {
children: Paragraph[] = [];
children: (Paragraph | Table)[] = [];
hasNumberings = false;
property: CellProperty = {
borders: new TableCellBorders(),
@ -72,6 +73,14 @@ export class TableCell {
return this;
}
addTable(t: Table) {
if (t.hasNumberings) {
this.hasNumberings = true;
}
this.children.push(t);
return this;
}
verticalMerge(t: VMergeType) {
this.property.verticalMerge = t;
return this;

View File

@ -1,6 +1,6 @@
{
"name": "docx-wasm",
"version": "0.0.148",
"version": "0.0.149",
"main": "dist/node/index.js",
"browser": "dist/web/index.js",
"author": "bokuweb <bokuweb12@gmail.com>",

View File

@ -25,6 +25,13 @@ impl TableCell {
self
}
pub fn add_table(mut self, t: Table) -> TableCell {
self.0
.children
.push(docx_rs::TableCellContent::Table(t.take()));
self
}
pub fn vertical_merge(mut self, t: docx_rs::VMergeType) -> TableCell {
self.0.property = self.0.property.vertical_merge(t);
self

View File

@ -10448,6 +10448,43 @@ exports[`writer should write lvlOverride with level 3`] = `
<w:abstractNumId w:val=\\"0\\" /><w:lvlOverride w:ilvl=\\"0\\"><w:lvl w:ilvl=\\"0\\"><w:start w:val=\\"3\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%1\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /></w:pPr><w:rPr /></w:lvl></w:lvlOverride></w:num></w:numbering>"
`;
exports[`writer should write nested table 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\">
<Relationship Id=\\"rId1\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles\\" Target=\\"styles.xml\\" />
<Relationship Id=\\"rId2\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable\\" Target=\\"fontTable.xml\\" />
<Relationship Id=\\"rId3\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\\" Target=\\"settings.xml\\" />
<Relationship Id=\\"rId4\\" Type=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/header\\" Target=\\"header1.xml\\" />
<Relationship Id=\\"rId5\\" Type=\\"http://schemas.microsoft.com/office/2011/relationships/commentsExtended\\" Target=\\"commentsExtended.xml\\" />
</Relationships>"
`;
exports[`writer should write nested table 2`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?>
<w:document xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\" xmlns:w10=\\"urn:schemas-microsoft-com:office:word\\" xmlns:wp=\\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\\" xmlns:wps=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\\" xmlns:wpg=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\\" xmlns:mc=\\"http://schemas.openxmlformats.org/markup-compatibility/2006\\" xmlns:wp14=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\\" xmlns:w14=\\"http://schemas.microsoft.com/office/word/2010/wordml\\" xmlns:w15=\\"http://schemas.microsoft.com/office/word/2012/wordml\\" mc:Ignorable=\\"w14 wp14\\">
<w:body><w:tbl><w:tblPr><w:tblW w:w=\\"0\\" w:type=\\"dxa\\" /><w:jc w:val=\\"left\\" /><w:tblBorders><w:top w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:left w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:bottom w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:right w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideH w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideV w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /></w:tblBorders><w:tblCellMar>
<w:top w:w=\\"55\\" w:type=\\"dxa\\" />
<w:left w:w=\\"54\\" w:type=\\"dxa\\" />
<w:bottom w:w=\\"55\\" w:type=\\"dxa\\" />
<w:right w:w=\\"55\\" w:type=\\"dxa\\" />
</w:tblCellMar><w:tblInd w:w=\\"0\\" w:type=\\"dxa\\" /></w:tblPr><w:tblGrid /><w:tr><w:trPr /><w:tc><w:tcPr><w:tcBorders><w:top w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:left w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:bottom w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:right w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideH w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideV w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /></w:tcBorders></w:tcPr><w:tbl><w:tblPr><w:tblW w:w=\\"0\\" w:type=\\"dxa\\" /><w:jc w:val=\\"left\\" /><w:tblBorders><w:top w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:left w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:bottom w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:right w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideH w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideV w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /></w:tblBorders><w:tblCellMar>
<w:top w:w=\\"55\\" w:type=\\"dxa\\" />
<w:left w:w=\\"54\\" w:type=\\"dxa\\" />
<w:bottom w:w=\\"55\\" w:type=\\"dxa\\" />
<w:right w:w=\\"55\\" w:type=\\"dxa\\" />
</w:tblCellMar><w:tblInd w:w=\\"0\\" w:type=\\"dxa\\" /></w:tblPr><w:tblGrid /><w:tr><w:trPr /><w:tc><w:tcPr><w:tcBorders><w:top w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:left w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:bottom w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:right w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideH w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /><w:insideV w:val=\\"single\\" w:sz=\\"2\\" w:space=\\"0\\" w:color=\\"000000\\" /></w:tcBorders></w:tcPr><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr><w:rFonts /></w:rPr><w:t xml:space=\\"preserve\\">Hello world!!</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /></w:pPr></w:p></w:tc></w:tr></w:tbl><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1985\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"1701\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:headerReference w:type=\\"default\\" r:id=\\"rId4\\" /><w:cols w:space=\\"425\\" />
<w:docGrid w:type=\\"lines\\" w:linePitch=\\"360\\" />
</w:sectPr></w:body>
</w:document>"
`;
exports[`writer should write nested table 3`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?>
<w:numbering xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\"><w:abstractNum w:abstractNumId=\\"1\\"><w:lvl w:ilvl=\\"0\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%1.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"420\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"1\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%2)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"840\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"2\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%3\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"1260\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"3\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%4.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"1680\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"4\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%5)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2100\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"5\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%6\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2520\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"6\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"%7.\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"2940\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"7\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimal\\" /><w:lvlText w:val=\\"(%8)\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"3360\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl><w:lvl w:ilvl=\\"8\\"><w:start w:val=\\"1\\" /><w:numFmt w:val=\\"decimalEnclosedCircle\\" /><w:lvlText w:val=\\"%9\\" /><w:lvlJc w:val=\\"left\\" /><w:pPr><w:rPr /><w:ind w:left=\\"3780\\" w:right=\\"0\\" w:hanging=\\"420\\" /></w:pPr><w:rPr /></w:lvl></w:abstractNum><w:num w:numId=\\"1\\">
<w:abstractNumId w:val=\\"1\\" />
</w:num></w:numbering>"
`;
exports[`writer should write page margin 1`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?>
<Relationships xmlns=\\"http://schemas.openxmlformats.org/package/2006/relationships\\">
@ -10462,7 +10499,7 @@ exports[`writer should write page margin 1`] = `
exports[`writer should write page margin 2`] = `
"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?>
<w:document xmlns:o=\\"urn:schemas-microsoft-com:office:office\\" xmlns:r=\\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\\" xmlns:v=\\"urn:schemas-microsoft-com:vml\\" xmlns:w=\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\" xmlns:w10=\\"urn:schemas-microsoft-com:office:word\\" xmlns:wp=\\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\\" xmlns:wps=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\\" xmlns:wpg=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\\" xmlns:mc=\\"http://schemas.openxmlformats.org/markup-compatibility/2006\\" xmlns:wp14=\\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\\" xmlns:w14=\\"http://schemas.microsoft.com/office/word/2010/wordml\\" xmlns:w15=\\"http://schemas.microsoft.com/office/word/2012/wordml\\" mc:Ignorable=\\"w14 wp14\\">
<w:body><w:p w14:paraId=\\"00000001\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr><w:rFonts /></w:rPr><w:t xml:space=\\"preserve\\">Hello world!!</w:t></w:r></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1000\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"2000\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:headerReference w:type=\\"default\\" r:id=\\"rId4\\" /><w:cols w:space=\\"425\\" />
<w:body><w:p w14:paraId=\\"00000002\\"><w:pPr><w:rPr /></w:pPr><w:r><w:rPr><w:rFonts /></w:rPr><w:t xml:space=\\"preserve\\">Hello world!!</w:t></w:r></w:p><w:sectPr><w:pgSz w:w=\\"11906\\" w:h=\\"16838\\" /><w:pgMar w:top=\\"1000\\" w:right=\\"1701\\" w:bottom=\\"1701\\" w:left=\\"2000\\" w:header=\\"851\\" w:footer=\\"992\\" w:gutter=\\"0\\" /><w:headerReference w:type=\\"default\\" r:id=\\"rId4\\" /><w:cols w:space=\\"425\\" />
<w:docGrid w:type=\\"lines\\" w:linePitch=\\"360\\" />
</w:sectPr></w:body>
</w:document>"

View File

@ -85,6 +85,24 @@ describe("writer", () => {
}
});
test("should write nested table", () => {
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
const childTable = new w.Table().addRow(
new w.TableRow().addCell(new w.TableCell().addParagraph(p))
);
const parentTable = new w.Table().addRow(
new w.TableRow().addCell(new w.TableCell().addTable(childTable))
);
const buffer = new w.Docx().addTable(parentTable).build();
const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) {
if (e.entryName.match(/document.xml|numbering.xml/)) {
expect(z.readAsText(e)).toMatchSnapshot();
}
}
writeFileSync("../output/nested_table.docx", buffer);
});
test("should write page margin", () => {
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!"));
const buffer = new w.Docx()
@ -121,10 +139,7 @@ describe("writer", () => {
test("should write doc vars", () => {
const p = new w.Paragraph().addRun(new w.Run().addText("Hello world!!!!"));
const buffer = new w.Docx()
.addParagraph(p)
.addDocVar("foo", "bar")
.build();
const buffer = new w.Docx().addParagraph(p).addDocVar("foo", "bar").build();
writeFileSync("../output/doc_vars.docx", buffer);
const z = new Zip(Buffer.from(buffer));
for (const e of z.getEntries()) {