diff --git a/docx-core/src/documents/elements/table.rs b/docx-core/src/documents/elements/table.rs index fdcfdab..dcc4bba 100644 --- a/docx-core/src/documents/elements/table.rs +++ b/docx-core/src/documents/elements/table.rs @@ -38,7 +38,7 @@ mod tests { let b = Table::new(vec![TableRow::new(vec![])]).build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#" + r#" diff --git a/docx-core/src/documents/elements/table_borders.rs b/docx-core/src/documents/elements/table_borders.rs index 72df3bc..65ef846 100644 --- a/docx-core/src/documents/elements/table_borders.rs +++ b/docx-core/src/documents/elements/table_borders.rs @@ -96,9 +96,9 @@ impl Default for TableBorders { top: Some(TableBorder::new(BorderPosition::Top)), left: Some(TableBorder::new(BorderPosition::Left)), bottom: Some(TableBorder::new(BorderPosition::Bottom)), - right: None, + right: Some(TableBorder::new(BorderPosition::Right)), inside_h: Some(TableBorder::new(BorderPosition::IndideH)), - inside_v: None, + inside_v: Some(TableBorder::new(BorderPosition::IndideV)), } } } @@ -161,7 +161,7 @@ mod tests { let b = TableBorders::new().build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#""# + r#""# ); } @@ -173,7 +173,7 @@ mod tests { .build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#""# + r#""# ); } } diff --git a/docx-core/src/documents/elements/table_cell.rs b/docx-core/src/documents/elements/table_cell.rs index 784ec4f..ae60cac 100644 --- a/docx-core/src/documents/elements/table_cell.rs +++ b/docx-core/src/documents/elements/table_cell.rs @@ -51,10 +51,7 @@ mod tests { #[test] fn test_cell() { let b = TableCell::new().build(); - assert_eq!( - str::from_utf8(&b).unwrap(), - r#""# - ); + assert_eq!(str::from_utf8(&b).unwrap(), r#""#); } #[test] @@ -64,7 +61,7 @@ mod tests { .build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#"Hello"# + r#"Hello"# ); } } diff --git a/docx-core/src/documents/elements/table_cell_borders.rs b/docx-core/src/documents/elements/table_cell_borders.rs index ca3e167..8e2407d 100644 --- a/docx-core/src/documents/elements/table_cell_borders.rs +++ b/docx-core/src/documents/elements/table_cell_borders.rs @@ -84,9 +84,9 @@ impl Default for TableCellBorders { top: Some(TableCellBorder::new(BorderPosition::Top)), left: Some(TableCellBorder::new(BorderPosition::Left)), bottom: Some(TableCellBorder::new(BorderPosition::Bottom)), - right: None, + right: Some(TableCellBorder::new(BorderPosition::Right)), inside_h: Some(TableCellBorder::new(BorderPosition::IndideH)), - inside_v: None, + inside_v: Some(TableCellBorder::new(BorderPosition::IndideV)), } } } @@ -135,21 +135,3 @@ impl BuildXML for TableCellBorders { .build() } } - -#[cfg(test)] -mod tests { - - use super::*; - #[cfg(test)] - use pretty_assertions::assert_eq; - use std::str; - - #[test] - fn test_table_borders() { - let b = TableCellBorders::new().build(); - assert_eq!( - str::from_utf8(&b).unwrap(), - r#""# - ); - } -} diff --git a/docx-core/src/documents/elements/table_cell_property.rs b/docx-core/src/documents/elements/table_cell_property.rs index 154595b..f5a3e5e 100644 --- a/docx-core/src/documents/elements/table_cell_property.rs +++ b/docx-core/src/documents/elements/table_cell_property.rs @@ -1,34 +1,34 @@ -use super::TableCellBorders; +use super::{TableCellBorders, TableCellWidth}; use crate::documents::BuildXML; -// use crate::types::*; +use crate::types::*; use crate::xml_builder::*; #[derive(Debug, Clone)] pub struct TableCellProperty { - // width: TableCellWidth, - borders: TableCellBorders, + width: Option, + borders: Option, } impl TableCellProperty { pub fn new() -> TableCellProperty { TableCellProperty { - // width: TableCellWidth::new(w, WidthType::DXA), - borders: TableCellBorders::new(), + width: None, + borders: None, } } - // pub fn width(mut self, v: usize) -> TableCellProperty { - // self.width = TableCellWidth::new(v, WidthType::DXA); - // self - // } + pub fn width(mut self, v: usize, t: WidthType) -> TableCellProperty { + self.width = Some(TableCellWidth::new(v, t)); + self + } } impl BuildXML for TableCellProperty { fn build(&self) -> Vec { XMLBuilder::new() .open_table_cell_property() - // .add_child(&self.width) - .add_child(&self.borders) + .add_optional_child(&self.width) + .add_optional_child(&self.borders) .close() .build() } @@ -46,9 +46,6 @@ mod tests { fn test_default() { let c = TableCellProperty::new(); let b = c.build(); - assert_eq!( - str::from_utf8(&b).unwrap(), - r#""# - ); + assert_eq!(str::from_utf8(&b).unwrap(), r#""#); } } diff --git a/docx-core/src/documents/elements/table_property.rs b/docx-core/src/documents/elements/table_property.rs index a0c1fa2..a764d19 100644 --- a/docx-core/src/documents/elements/table_property.rs +++ b/docx-core/src/documents/elements/table_property.rs @@ -15,7 +15,7 @@ pub struct TableProperty { impl Default for TableProperty { fn default() -> Self { TableProperty { - width: TableWidth::new(9638, WidthType::DXA), + width: TableWidth::new(0, WidthType::AUTO), justification: Justification::new("left"), borders: TableBorders::new(), margins: TableCellMargins::new(), @@ -34,13 +34,13 @@ impl TableProperty { self } - pub fn width(mut self, v: usize) -> TableProperty { - self.width = TableWidth::new(v, WidthType::DXA); + pub fn width(mut self, v: usize, t: WidthType) -> TableProperty { + self.width = TableWidth::new(v, t); self } - pub fn justify(mut self, v: &str) -> TableProperty { - self.justification = Justification::new(v); + pub fn align(mut self, v: TableAlignmentType) -> TableProperty { + self.justification = Justification::new(v.to_string()); self } } @@ -73,7 +73,7 @@ mod tests { let b = c.build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#" + r#" diff --git a/docx-core/src/documents/elements/table_row.rs b/docx-core/src/documents/elements/table_row.rs index 331312c..a525174 100644 --- a/docx-core/src/documents/elements/table_row.rs +++ b/docx-core/src/documents/elements/table_row.rs @@ -38,7 +38,7 @@ mod tests { let b = TableRow::new(vec![TableCell::new()]).build(); assert_eq!( str::from_utf8(&b).unwrap(), - r#""# + r#""# ); } } diff --git a/docx-core/src/documents/mod.rs b/docx-core/src/documents/mod.rs index 97a39ea..1a99abb 100644 --- a/docx-core/src/documents/mod.rs +++ b/docx-core/src/documents/mod.rs @@ -53,6 +53,11 @@ impl Docx { self } + pub fn add_table(mut self, t: Table) -> Docx { + self.document = self.document.add_table(t); + self + } + pub fn build(&self) -> XMLDocx { XMLDocx { content_type: self.content_type.build(), diff --git a/docx-core/src/types/mod.rs b/docx-core/src/types/mod.rs index 3ddca37..0090fa3 100644 --- a/docx-core/src/types/mod.rs +++ b/docx-core/src/types/mod.rs @@ -3,6 +3,7 @@ pub mod border_position; pub mod border_type; pub mod special_indent_type; pub mod style_type; +pub mod table_alignment_type; pub mod width_type; pub use alignment_type::*; @@ -10,4 +11,5 @@ pub use border_position::*; pub use border_type::*; pub use special_indent_type::*; pub use style_type::*; +pub use table_alignment_type::*; pub use width_type::*; diff --git a/docx-core/src/types/table_alignment_type.rs b/docx-core/src/types/table_alignment_type.rs new file mode 100644 index 0000000..1282bdb --- /dev/null +++ b/docx-core/src/types/table_alignment_type.rs @@ -0,0 +1,20 @@ +use std::fmt; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +#[derive(Copy, Clone, Debug)] +pub enum TableAlignmentType { + Center, + Left, + Right, +} + +impl fmt::Display for TableAlignmentType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + TableAlignmentType::Center => write!(f, "center"), + TableAlignmentType::Left => write!(f, "left"), + TableAlignmentType::Right => write!(f, "right"), + } + } +} diff --git a/docx-core/src/types/width_type.rs b/docx-core/src/types/width_type.rs index f51bcfc..10c8c4d 100644 --- a/docx-core/src/types/width_type.rs +++ b/docx-core/src/types/width_type.rs @@ -5,12 +5,14 @@ use wasm_bindgen::prelude::*; #[derive(Copy, Clone, Debug)] pub enum WidthType { DXA, + AUTO, } impl fmt::Display for WidthType { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { WidthType::DXA => write!(f, "dxa"), + WidthType::AUTO => write!(f, "auto"), } } } diff --git a/docx-core/tests/lib.rs b/docx-core/tests/lib.rs index f705007..47d067f 100644 --- a/docx-core/tests/lib.rs +++ b/docx-core/tests/lib.rs @@ -58,3 +58,22 @@ pub fn alignment() -> Result<(), DocxError> { .pack(file)?; Ok(()) } + +#[test] +pub fn table() -> Result<(), DocxError> { + let path = std::path::Path::new("./tests/output/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_run(Run::new("Hello"))), + TableCell::new().add_paragraph(Paragraph::new().add_run(Run::new("World"))), + ]), + TableRow::new(vec![ + TableCell::new().add_paragraph(Paragraph::new().add_run(Run::new("Foo"))), + TableCell::new().add_paragraph(Paragraph::new().add_run(Run::new("Bar"))), + ]), + ]); + Docx::new().add_table(table).build().pack(file)?; + Ok(()) +} diff --git a/fixtures/table_docx/[Content_Types].xml b/fixtures/table_docx/[Content_Types].xml new file mode 100644 index 0000000..69b0cff --- /dev/null +++ b/fixtures/table_docx/[Content_Types].xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_docx/_rels/.rels b/fixtures/table_docx/_rels/.rels new file mode 100644 index 0000000..3a89e29 --- /dev/null +++ b/fixtures/table_docx/_rels/.rels @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_docx/docProps/app.xml b/fixtures/table_docx/docProps/app.xml new file mode 100644 index 0000000..6d3e209 --- /dev/null +++ b/fixtures/table_docx/docProps/app.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_docx/docProps/core.xml b/fixtures/table_docx/docProps/core.xml new file mode 100644 index 0000000..a7e79b0 --- /dev/null +++ b/fixtures/table_docx/docProps/core.xml @@ -0,0 +1 @@ +Un-namedUn-named12019-11-13T11:40:18Z2019-11-13T11:40:18Z \ No newline at end of file diff --git a/fixtures/table_docx/table.docx b/fixtures/table_docx/table.docx new file mode 100644 index 0000000..3ad3ea0 Binary files /dev/null and b/fixtures/table_docx/table.docx differ diff --git a/fixtures/table_docx/word/_rels/document.xml.rels b/fixtures/table_docx/word/_rels/document.xml.rels new file mode 100644 index 0000000..c27eb03 --- /dev/null +++ b/fixtures/table_docx/word/_rels/document.xml.rels @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_docx/word/_rels/footer1.xml.rels b/fixtures/table_docx/word/_rels/footer1.xml.rels new file mode 100644 index 0000000..ed9d666 --- /dev/null +++ b/fixtures/table_docx/word/_rels/footer1.xml.rels @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_docx/word/_rels/header1.xml.rels b/fixtures/table_docx/word/_rels/header1.xml.rels new file mode 100644 index 0000000..ed9d666 --- /dev/null +++ b/fixtures/table_docx/word/_rels/header1.xml.rels @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_docx/word/document.xml b/fixtures/table_docx/word/document.xml new file mode 100644 index 0000000..68609ff --- /dev/null +++ b/fixtures/table_docx/word/document.xml @@ -0,0 +1,185 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Hello + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/fixtures/table_docx/word/footer1.xml b/fixtures/table_docx/word/footer1.xml new file mode 100644 index 0000000..622cdc7 --- /dev/null +++ b/fixtures/table_docx/word/footer1.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_docx/word/footnotes.xml b/fixtures/table_docx/word/footnotes.xml new file mode 100644 index 0000000..57efcdd --- /dev/null +++ b/fixtures/table_docx/word/footnotes.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_docx/word/header1.xml b/fixtures/table_docx/word/header1.xml new file mode 100644 index 0000000..09f8e4b --- /dev/null +++ b/fixtures/table_docx/word/header1.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_docx/word/numbering.xml b/fixtures/table_docx/word/numbering.xml new file mode 100644 index 0000000..7544091 --- /dev/null +++ b/fixtures/table_docx/word/numbering.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_docx/word/settings.xml b/fixtures/table_docx/word/settings.xml new file mode 100644 index 0000000..9202cef --- /dev/null +++ b/fixtures/table_docx/word/settings.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_docx/word/styles.xml b/fixtures/table_docx/word/styles.xml new file mode 100644 index 0000000..069c8e3 --- /dev/null +++ b/fixtures/table_docx/word/styles.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_libre_office/table.docx b/fixtures/table_libre_office/table.docx index a205c67..c2812a5 100644 Binary files a/fixtures/table_libre_office/table.docx and b/fixtures/table_libre_office/table.docx differ diff --git a/fixtures/table_word_online/[Content_Types].xml b/fixtures/table_word_online/[Content_Types].xml new file mode 100644 index 0000000..4d5931e --- /dev/null +++ b/fixtures/table_word_online/[Content_Types].xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_word_online/_rels/.rels b/fixtures/table_word_online/_rels/.rels new file mode 100644 index 0000000..0afe5c3 --- /dev/null +++ b/fixtures/table_word_online/_rels/.rels @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_word_online/docProps/app.xml b/fixtures/table_word_online/docProps/app.xml new file mode 100644 index 0000000..5af8832 --- /dev/null +++ b/fixtures/table_word_online/docProps/app.xml @@ -0,0 +1 @@ +Microsoft Office Word0falsefalsefalse00.0001Normal.dotmfalse \ No newline at end of file diff --git a/fixtures/table_word_online/docProps/core.xml b/fixtures/table_word_online/docProps/core.xml new file mode 100644 index 0000000..796de0d --- /dev/null +++ b/fixtures/table_word_online/docProps/core.xml @@ -0,0 +1 @@ +2019-11-13T02:50:10.0035455Z2019-11-13T02:50:30.2555956ZSatoshi UekiSatoshi Ueki \ No newline at end of file diff --git a/fixtures/table_word_online/table.docx b/fixtures/table_word_online/table.docx new file mode 100644 index 0000000..fdfaae4 Binary files /dev/null and b/fixtures/table_word_online/table.docx differ diff --git a/fixtures/table_word_online/word/_rels/document2.xml.rels b/fixtures/table_word_online/word/_rels/document2.xml.rels new file mode 100644 index 0000000..d9d3612 --- /dev/null +++ b/fixtures/table_word_online/word/_rels/document2.xml.rels @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_word_online/word/document2.xml b/fixtures/table_word_online/word/document2.xml new file mode 100644 index 0000000..d9ec951 --- /dev/null +++ b/fixtures/table_word_online/word/document2.xml @@ -0,0 +1,119 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Hello + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/fixtures/table_word_online/word/fontTable.xml b/fixtures/table_word_online/word/fontTable.xml new file mode 100644 index 0000000..c25c4bb --- /dev/null +++ b/fixtures/table_word_online/word/fontTable.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_word_online/word/settings.xml b/fixtures/table_word_online/word/settings.xml new file mode 100644 index 0000000..06c4002 --- /dev/null +++ b/fixtures/table_word_online/word/settings.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_word_online/word/styles.xml b/fixtures/table_word_online/word/styles.xml new file mode 100644 index 0000000..ab59613 --- /dev/null +++ b/fixtures/table_word_online/word/styles.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/fixtures/table_word_online/word/theme/theme1.xml b/fixtures/table_word_online/word/theme/theme1.xml new file mode 100644 index 0000000..31e6197 --- /dev/null +++ b/fixtures/table_word_online/word/theme/theme1.xml @@ -0,0 +1,259 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/fixtures/table_word_online/word/webSettings.xml b/fixtures/table_word_online/word/webSettings.xml new file mode 100644 index 0000000..be9f295 --- /dev/null +++ b/fixtures/table_word_online/word/webSettings.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file