Skip to content

Commit

Permalink
document: support for iterating over tables
Browse files Browse the repository at this point in the history
Thanks to lishuu for the issue and fix.

Fixes unidoc#162
  • Loading branch information
tbaliance committed Mar 13, 2018
1 parent 9c067b3 commit efd47ee
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
13 changes: 13 additions & 0 deletions document/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ func (c Cell) Properties() CellProperties {
}
return CellProperties{c.x.TcPr}
}

// Paragraphs returns the paragraphs defined in the cell.
func (c Cell) Paragraphs() []Paragraph {
ret := []Paragraph{}
for _, ble := range c.x.EG_BlockLevelElts {
for _, cbc := range ble.EG_ContentBlockContent {
for _, p := range cbc.P {
ret = append(ret, Paragraph{c.d, p})
}
}
}
return ret
}
16 changes: 16 additions & 0 deletions document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@ func (d *Document) AddTable() Table {
return Table{d, tbl}
}

// Tables returns the tables defined in the document.
func (d *Document) Tables() []Table {
ret := []Table{}
if d.x.Body == nil {
return nil
}
for _, ble := range d.x.Body.EG_BlockLevelElts {
for _, c := range ble.EG_ContentBlockContent {
for _, t := range c.Tbl {
ret = append(ret, Table{d, t})
}
}
}
return ret
}

// AddParagraph adds a new paragraph to the document body.
func (d *Document) AddParagraph() Paragraph {
elts := wml.NewEG_BlockLevelElts()
Expand Down
11 changes: 11 additions & 0 deletions document/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ func (r Row) Properties() RowProperties {
}
return RowProperties{r.x.TrPr}
}

// Cells returns the cells defined in the table.
func (r Row) Cells() []Cell {
ret := []Cell{}
for _, cc := range r.x.EG_ContentCellContent {
for _, ctCell := range cc.Tc {
ret = append(ret, Cell{r.d, ctCell})
}
}
return ret
}
11 changes: 11 additions & 0 deletions document/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,14 @@ func (t Table) AddRow() Row {
c.Tr = append(c.Tr, tr)
return Row{t.d, tr}
}

// Rows returns the rows defined in the table.
func (t Table) Rows() []Row {
ret := []Row{}
for _, rc := range t.x.EG_ContentRowContent {
for _, ctRow := range rc.Tr {
ret = append(ret, Row{t.d, ctRow})
}
}
return ret
}

0 comments on commit efd47ee

Please sign in to comment.