mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-05 07:40:24 +08:00
优化 para.String
This commit is contained in:
@@ -24,7 +24,7 @@ import "unsafe"
|
||||
|
||||
// AddParagraph adds a new paragraph
|
||||
func (f *Docx) AddParagraph() *Paragraph {
|
||||
f.Document.Body.Items = append(f.Document.Body.Items, Paragraph{
|
||||
f.Document.Body.Items = append(f.Document.Body.Items, &Paragraph{
|
||||
Children: make([]interface{}, 0, 64),
|
||||
file: f,
|
||||
})
|
||||
|
||||
@@ -42,7 +42,7 @@ func (f *Docx) AddTable(row int, col int) *WTable {
|
||||
TableCells: cells,
|
||||
}
|
||||
}
|
||||
f.Document.Body.Items = append(f.Document.Body.Items, WTable{
|
||||
f.Document.Body.Items = append(f.Document.Body.Items, &WTable{
|
||||
TableProperties: &WTableProperties{
|
||||
Width: &WTableWidth{Type: "auto"},
|
||||
TableBorders: &WTableBorders{
|
||||
@@ -99,7 +99,7 @@ func (f *Docx) AddTableTwips(rowHeights []int64, colWidths []int64) *WTable {
|
||||
}
|
||||
}
|
||||
}
|
||||
f.Document.Body.Items = append(f.Document.Body.Items, WTable{
|
||||
f.Document.Body.Items = append(f.Document.Body.Items, &WTable{
|
||||
TableProperties: &WTableProperties{
|
||||
Width: &WTableWidth{Type: "auto"},
|
||||
TableBorders: &WTableBorders{
|
||||
|
||||
@@ -191,23 +191,9 @@ func main() {
|
||||
}
|
||||
fmt.Println("Plain text:")
|
||||
for _, it := range doc.Document.Body.Items {
|
||||
switch para := it.(type) {
|
||||
case docx.Paragraph:
|
||||
fmt.Println(para.String())
|
||||
case docx.WTable:
|
||||
fmt.Println("------------------------------")
|
||||
for x, r := range para.TableRows {
|
||||
fmt.Printf("[%d] ", x)
|
||||
for y, c := range r.TableCells {
|
||||
if len(c.Paragraphs) > 0 && len(c.Paragraphs[0].Children) > 0 {
|
||||
fmt.Printf("<%d> %v\t", y, &c.Paragraphs[0])
|
||||
} else {
|
||||
fmt.Printf("<%d> \t\t", y)
|
||||
}
|
||||
}
|
||||
fmt.Print("\n")
|
||||
}
|
||||
fmt.Println("------------------------------")
|
||||
switch it.(type) {
|
||||
case *docx.Paragraph, *docx.WTable: // printable
|
||||
fmt.Println(it)
|
||||
}
|
||||
}
|
||||
fmt.Println("End of main")
|
||||
|
||||
@@ -75,7 +75,7 @@ func (b *Body) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
return err
|
||||
}
|
||||
value.file = b.file
|
||||
b.Items = append(b.Items, value)
|
||||
b.Items = append(b.Items, &value)
|
||||
case "tbl":
|
||||
var value WTable
|
||||
err = d.DecodeElement(&value, &tt)
|
||||
@@ -83,7 +83,7 @@ func (b *Body) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
return err
|
||||
}
|
||||
value.file = b.file
|
||||
b.Items = append(b.Items, value)
|
||||
b.Items = append(b.Items, &value)
|
||||
default:
|
||||
err = d.Skip() // skip unsupported tags
|
||||
if err != nil {
|
||||
|
||||
143
structpara.go
143
structpara.go
@@ -184,55 +184,116 @@ func (p *Paragraph) String() string {
|
||||
case *Tab:
|
||||
sb.WriteByte('\t')
|
||||
case *Drawing:
|
||||
if x.Inline != nil && x.Inline.Graphic != nil && x.Inline.Graphic.GraphicData != nil && x.Inline.Graphic.GraphicData.Pic != nil {
|
||||
sb.WriteString("
|
||||
if x.Inline.Graphic.GraphicData.Pic.BlipFill != nil {
|
||||
tgt, err := p.file.ReferTarget(x.Inline.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
|
||||
if err != nil {
|
||||
sb.WriteString(err.Error())
|
||||
} else {
|
||||
h := md5.Sum(p.file.Media(tgt[6:]).Data)
|
||||
sb.WriteString(hex.EncodeToString(h[:]))
|
||||
if x.Inline != nil && x.Inline.Graphic != nil && x.Inline.Graphic.GraphicData != nil {
|
||||
if x.Inline.Graphic.GraphicData.Pic != nil {
|
||||
sb.WriteString("
|
||||
if x.Inline.Graphic.GraphicData.Pic.BlipFill != nil {
|
||||
tgt, err := p.file.ReferTarget(x.Inline.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
|
||||
if err != nil {
|
||||
sb.WriteString(err.Error())
|
||||
} else {
|
||||
h := md5.Sum(p.file.Media(tgt[6:]).Data)
|
||||
sb.WriteString(hex.EncodeToString(h[:]))
|
||||
}
|
||||
}
|
||||
sb.WriteByte(')')
|
||||
continue
|
||||
}
|
||||
if x.Inline.Graphic.GraphicData.Shape != nil {
|
||||
sb.WriteString("
|
||||
if x.Inline.Graphic.GraphicData.Shape.SpPr != nil {
|
||||
sb.WriteString(x.Inline.Graphic.GraphicData.Shape.SpPr.PrstGeom.Prst)
|
||||
}
|
||||
sb.WriteByte(')')
|
||||
continue
|
||||
}
|
||||
if x.Inline.Graphic.GraphicData.Canvas != nil {
|
||||
sb.WriteString("![inlncv ")
|
||||
if x.Inline.DocPr != nil {
|
||||
sb.WriteString(x.Inline.DocPr.Name)
|
||||
} else {
|
||||
sb.WriteString("nil")
|
||||
}
|
||||
sb.WriteString("]()")
|
||||
continue
|
||||
}
|
||||
sb.WriteByte(')')
|
||||
continue
|
||||
}
|
||||
if x.Anchor != nil && x.Anchor.Graphic != nil && x.Anchor.Graphic.GraphicData != nil && x.Anchor.Graphic.GraphicData.Pic != nil {
|
||||
sb.WriteString("
|
||||
if x.Anchor.Graphic.GraphicData.Pic.BlipFill != nil {
|
||||
tgt, err := p.file.ReferTarget(x.Anchor.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
|
||||
if err != nil {
|
||||
sb.WriteString(err.Error())
|
||||
} else {
|
||||
h := md5.Sum(p.file.Media(tgt[6:]).Data)
|
||||
sb.WriteString(hex.EncodeToString(h[:]))
|
||||
if x.Anchor != nil && x.Anchor.Graphic != nil && x.Anchor.Graphic.GraphicData != nil {
|
||||
if x.Anchor.Graphic.GraphicData.Pic != nil {
|
||||
sb.WriteString("
|
||||
if x.Anchor.Graphic.GraphicData.Pic.BlipFill != nil {
|
||||
tgt, err := p.file.ReferTarget(x.Anchor.Graphic.GraphicData.Pic.BlipFill.Blip.Embed)
|
||||
if err != nil {
|
||||
sb.WriteString(err.Error())
|
||||
} else {
|
||||
h := md5.Sum(p.file.Media(tgt[6:]).Data)
|
||||
sb.WriteString(hex.EncodeToString(h[:]))
|
||||
}
|
||||
}
|
||||
sb.WriteByte(')')
|
||||
continue
|
||||
}
|
||||
if x.Anchor.Graphic.GraphicData.Shape != nil {
|
||||
sb.WriteString("
|
||||
if x.Anchor.Graphic.GraphicData.Shape.SpPr != nil {
|
||||
sb.WriteString(x.Anchor.Graphic.GraphicData.Shape.SpPr.PrstGeom.Prst)
|
||||
}
|
||||
sb.WriteByte(')')
|
||||
continue
|
||||
}
|
||||
if x.Anchor.Graphic.GraphicData.Canvas != nil {
|
||||
sb.WriteString("![anchcv ")
|
||||
if x.Anchor.DocPr != nil {
|
||||
sb.WriteString(x.Anchor.DocPr.Name)
|
||||
} else {
|
||||
sb.WriteString("nil")
|
||||
}
|
||||
sb.WriteString("]()")
|
||||
continue
|
||||
}
|
||||
sb.WriteByte(')')
|
||||
}
|
||||
}
|
||||
}
|
||||
case *RunProperties:
|
||||
sb.WriteString("<prop>") //TODO: implement
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -37,6 +37,30 @@ type WTable struct {
|
||||
file *Docx
|
||||
}
|
||||
|
||||
func (t *WTable) String() string {
|
||||
if len(t.TableRows) == 0 || len(t.TableRows[0].TableCells) == 0 {
|
||||
return ""
|
||||
}
|
||||
sb := strings.Builder{}
|
||||
sb.WriteString("| ")
|
||||
for i := 0; i < len(t.TableRows[0].TableCells); i++ {
|
||||
sb.WriteString(" :----: |")
|
||||
}
|
||||
for _, r := range t.TableRows {
|
||||
sb.WriteString("\n|")
|
||||
for _, c := range r.TableCells {
|
||||
if len(c.Paragraphs) > 0 && len(c.Paragraphs[0].Children) > 0 {
|
||||
sb.WriteByte(' ')
|
||||
sb.WriteString(c.Paragraphs[0].String())
|
||||
} else {
|
||||
sb.WriteString(" ")
|
||||
}
|
||||
sb.WriteString(" |")
|
||||
}
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// UnmarshalXML implements the xml.Unmarshaler interface.
|
||||
func (t *WTable) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
for {
|
||||
|
||||
Reference in New Issue
Block a user