mirror of
https://github.com/unidoc/unioffice.git
synced 2025-04-27 13:48:54 +08:00
document: add validation for table row/cell contents
OSX Word is very opinionated about the contents.
This commit is contained in:
parent
b7c59b4c94
commit
c887342ed1
@ -394,12 +394,53 @@ func (d *Document) Validate() error {
|
|||||||
return errors.New("document not initialized correctly, nil base")
|
return errors.New("document not initialized correctly, nil base")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, v := range []func() error{d.validateTableCells} {
|
||||||
|
if err := v(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
if err := d.x.Validate(); err != nil {
|
if err := d.x.Validate(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Document) validateTableCells() error {
|
||||||
|
for _, elt := range d.x.Body.EG_BlockLevelElts {
|
||||||
|
for _, c := range elt.EG_ContentBlockContent {
|
||||||
|
for _, t := range c.Tbl {
|
||||||
|
for _, rc := range t.EG_ContentRowContent {
|
||||||
|
for _, row := range rc.Tr {
|
||||||
|
hasCell := false
|
||||||
|
for _, ecc := range row.EG_ContentCellContent {
|
||||||
|
cellHasPara := false
|
||||||
|
for _, cell := range ecc.Tc {
|
||||||
|
hasCell = true
|
||||||
|
for _, cellElt := range cell.EG_BlockLevelElts {
|
||||||
|
for _, cellCont := range cellElt.EG_ContentBlockContent {
|
||||||
|
if len(cellCont.P) > 0 {
|
||||||
|
cellHasPara = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !cellHasPara {
|
||||||
|
return errors.New("table cell must contain a paragraph")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// OSX Word requires this and won't open the file otherwise
|
||||||
|
if !hasCell {
|
||||||
|
return errors.New("table row must contain a cell")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// AddImage adds an image to the document package, returning a reference that
|
// AddImage adds an image to the document package, returning a reference that
|
||||||
// can be used to add the image to a run and place it in the document contents.
|
// can be used to add the image to a run and place it in the document contents.
|
||||||
func (d *Document) AddImage(i common.Image) (common.ImageRef, error) {
|
func (d *Document) AddImage(i common.Image) (common.ImageRef, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user