feat: Support comment for table

main
bokuweb 2019-12-05 16:00:32 +09:00
parent 9ba3ef0aea
commit 1d9625236f
12 changed files with 94 additions and 34 deletions

View File

@ -41,7 +41,10 @@ mod tests {
let b = c.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<w:commentRangeEnd w:id="mockid" />
r#"<w:r>
<w:rPr />
</w:r>
<w:commentRangeEnd w:id="mockid" />
<w:r>
<w:commentReference w:id="mockid" />
</w:r>"#

View File

@ -4,8 +4,8 @@ use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct Table<'a> {
pub(crate) rows: Vec<TableRow<'a>>,
property: TableProperty,
rows: Vec<TableRow<'a>>,
grid: Vec<usize>,
}

View File

@ -5,8 +5,8 @@ use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct TableCell<'a> {
pub(crate) contents: Vec<TableCellContent<'a>>,
property: TableCellProperty,
contents: Vec<TableCellContent<'a>>,
}
#[derive(Debug, Clone)]

View File

@ -4,8 +4,8 @@ use crate::xml_builder::*;
#[derive(Debug, Clone)]
pub struct TableRow<'a> {
pub(crate) cells: Vec<TableCell<'a>>,
property: TableRowProperty,
cells: Vec<TableCell<'a>>,
}
impl<'a> TableRow<'a> {

View File

@ -95,12 +95,13 @@ impl<'a> Docx<'a> {
}
}
// Traverse and clone comments from document and add to comments node.
fn update_comments(&mut self) {
let mut comments: Vec<Comment<'a>> = vec![];
for child in &self.document.children {
match child {
DocumentChild::Paragraph(p) => {
for child in &p.children {
DocumentChild::Paragraph(paragraph) => {
for child in &paragraph.children {
match child {
ParagraphChild::CommentStart(c) => {
comments.push(c.comment());
@ -109,6 +110,26 @@ impl<'a> Docx<'a> {
}
}
}
DocumentChild::Table(table) => {
for row in &table.rows {
for cell in &row.cells {
for content in &cell.contents {
match content {
TableCellContent::Paragraph(paragraph) => {
for child in &paragraph.children {
match child {
ParagraphChild::CommentStart(c) => {
comments.push(c.comment());
}
_ => {}
}
}
}
}
}
}
}
}
_ => {}
}
}

View File

@ -278,3 +278,39 @@ pub fn comments() -> Result<(), DocxError> {
.pack(file)?;
Ok(())
}
#[test]
pub fn comments_to_table() -> Result<(), DocxError> {
let path = std::path::Path::new("./tests/output/comments_table.docx");
let file = std::fs::File::create(&path).unwrap();
let table = Table::new(vec![TableRow::new(vec![
TableCell::new().add_paragraph(
Paragraph::new()
.add_comment_start(
Comment::new("1")
.author("bokuweb")
.date("2019-01-01T00:00:00Z")
.paragraph(Paragraph::new().add_run(Run::new().add_text("Hello"))),
)
.add_run(Run::new().add_text("Hello"))
.add_comment_end("1"),
),
TableCell::new().add_paragraph(Paragraph::new().add_run(Run::new().add_text("World"))),
])]);
Docx::new()
.add_paragraph(
Paragraph::new()
.add_comment_start(
Comment::new("ABCD-1234")
.author("bokuweb")
.date("2019-01-01T00:00:00Z")
.paragraph(Paragraph::new().add_run(Run::new().add_text("Comment!!"))),
)
.add_run(Run::new().add_text("Hello").highlight("cyan"))
.add_comment_end("ABCD-1234"),
)
.add_table(table)
.build()
.pack(file)?;
Ok(())
}

View File

@ -8,4 +8,5 @@
<Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml" />
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml" />
<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml" />
<Override PartName="/word/comments.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml" />
</Types>

View File

@ -1,11 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
<Template></Template>
<TotalTime></TotalTime>
<Application></Application>
<Pages></Pages>
<Words></Words>
<Characters></Characters>
<CharactersWithSpaces></CharactersWithSpaces>
<Paragraphs></Paragraphs>
</Properties>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes" />

View File

@ -1,12 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dcterms:created xsi:type="dcterms:W3CDTF"></dcterms:created>
<dc:creator></dc:creator>
<dc:description></dc:description>
<dc:language></dc:language>
<cp:lastModifiedBy></cp:lastModifiedBy>
<dcterms:modified xsi:type="dcterms:W3CDTF"></dcterms:modified>
<cp:revision></cp:revision>
<dc:subject></dc:subject>
<dc:title></dc:title>
<dcterms:created xsi:type="dcterms:W3CDTF">1970-01-01T00:00:00Z</dcterms:created>
<dc:creator>unknown</dc:creator>
<cp:lastModifiedBy>unknown</cp:lastModifiedBy>
<dcterms:modified xsi:type="dcterms:W3CDTF">1970-01-01T00:00:00Z</dcterms:modified>
<cp:revision>1</cp:revision>
</cp:coreProperties>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml" />
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml" />
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml" />
<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" />
</Relationships>

View File

@ -16,13 +16,25 @@
<w:pStyle w:val="Normal" />
<w:rPr />
</w:pPr>
<w:commentRangeStart w:id="1" />
<w:r>
<w:rPr>
<w:highlight w:val="cyan" />
</w:rPr>
<w:t xml:space="preserve">Hello</w:t>
</w:r>
<w:r>
<w:rPr>
<w:highlight w:val="yellow" />
</w:rPr>
<w:t xml:space="preserve"> World!</w:t>
</w:r>
<w:r>
<w:rPr />
<w:t xml:space="preserve">Hello</w:t>
<w:tab />
<w:t xml:space="preserve">World</w:t>
<w:br w:val="page" />
<w:t xml:space="preserve">Foo</w:t>
</w:r>
<w:commentRangeEnd w:id="1" />
<w:r>
<w:commentReference w:id="1" />
</w:r>
</w:p>
</w:body>

View File

@ -1 +1 @@
<w:styles xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 w15"><w:docDefaults><w:rPrDefault><w:rPr /></w:rPrDefault></w:docDefaults></w:styles>
<w:styles xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" mc:Ignorable="w14 w15"><w:docDefaults><w:rPrDefault><w:rPr /></w:rPrDefault></w:docDefaults><w:style w:type="paragraph" w:styleId="Normal"><w:name w:val="Normal" /><w:rPr /><w:pPr><w:pStyle w:val="Normal" /><w:rPr /></w:pPr><w:basedOn w:val="Normal" /><w:next w:val="Normal" /><w:qFormat /></w:style></w:styles>