feat: Add table grid

main
bokuweb 2019-11-13 14:08:00 +09:00
parent f0b33fa19b
commit 2df62a9f98
2 changed files with 55 additions and 3 deletions

View File

@ -1,4 +1,4 @@
use super::{TableProperty, TableRow};
use super::{TableGrid, TableProperty, TableRow};
use crate::documents::BuildXML;
use crate::xml_builder::*;
@ -6,20 +6,33 @@ use crate::xml_builder::*;
pub struct Table {
property: TableProperty,
rows: Vec<TableRow>,
grid: Vec<usize>,
}
impl Table {
pub fn new(rows: Vec<TableRow>) -> Table {
let property = TableProperty::new();
Self { property, rows }
let grid = vec![];
Self {
property,
rows,
grid,
}
}
pub fn set_grid(mut self, grid: Vec<usize>) -> Table {
self.grid = grid;
self
}
}
impl BuildXML for Table {
fn build(&self) -> Vec<u8> {
let grid = TableGrid::new(self.grid.clone());
let b = XMLBuilder::new()
.open_table()
.add_child(&self.property)
.add_child(&grid)
.add_children(&self.rows);
b.close().build()
}
@ -34,7 +47,7 @@ mod tests {
use std::str;
#[test]
fn test_row() {
fn test_table() {
let b = Table::new(vec![TableRow::new(vec![])]).build();
assert_eq!(
str::from_utf8(&b).unwrap(),
@ -46,4 +59,23 @@ mod tests {
</w:tblCellMar></w:tblPr><w:tr><w:trPr /></w:tr></w:tbl>"#
);
}
#[test]
fn test_table_grid() {
let b = Table::new(vec![TableRow::new(vec![])])
.set_grid(vec![100, 200])
.build();
assert_eq!(
str::from_utf8(&b).unwrap(),
r#"<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:tblPr><w:tblGrid>
<w:gridCol w:w="100" w:type="dxa" />
<w:gridCol w:w="200" w:type="dxa" />
</w:tblGrid><w:tr><w:trPr /></w:tr></w:tbl>"#
);
}
}

View File

@ -77,3 +77,23 @@ pub fn table() -> Result<(), DocxError> {
Docx::new().add_table(table).build().pack(file)?;
Ok(())
}
#[test]
pub fn table_with_grid() -> Result<(), DocxError> {
let path = std::path::Path::new("./tests/output/table_with_grid.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"))),
]),
])
.set_grid(vec![3000, 3000]);
Docx::new().add_table(table).build().pack(file)?;
Ok(())
}