unioffice/vmldrawing/container.go
Todd 3bc4675cf6 spreadsheet: add comment support
This adds comment support for sheets.  Excel requires a VML drawing with
the comment box shape for each comment to display the comment.
LibreOffice displays comments fine with or without the shape, and
creates the shape for its own comments.  For the sake of compatibility,
we create comment shapes as well.

I know of no other use for the legacy VML support other than comment
boxes...
2017-09-10 11:25:29 -05:00

81 lines
2.3 KiB
Go

// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
package vmldrawing
import (
"encoding/xml"
"baliance.com/gooxml/schema/urn/schemas_microsoft_com/vml"
)
type Container struct {
Layout *vml.OfcShapelayout
ShapeType *vml.Shapetype
Shape []*vml.Shape
}
func NewContainer() *Container {
return &Container{}
}
func (c *Container) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "xmlns:v"}, Value: "urn:schemas-microsoft-com:vml"})
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "xmlns:o"}, Value: "urn:schemas-microsoft-com:office:office"})
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "xmlns:x"}, Value: "urn:schemas-microsoft-com:office:excel"})
start.Name.Local = "xml"
e.EncodeToken(start)
if c.Layout != nil {
se := xml.StartElement{Name: xml.Name{Local: "o:shapelayout"}}
e.EncodeElement(c.Layout, se)
}
if c.ShapeType != nil {
se := xml.StartElement{Name: xml.Name{Local: "v:shapetype"}}
e.EncodeElement(c.ShapeType, se)
}
for _, s := range c.Shape {
se := xml.StartElement{Name: xml.Name{Local: "v:shape"}}
e.EncodeElement(s, se)
}
return e.EncodeToken(xml.EndElement{Name: start.Name})
}
func (c *Container) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
c.Shape = nil
outer:
for {
tok, err := d.Token()
if err != nil {
return err
}
switch el := tok.(type) {
case xml.StartElement:
switch el.Name.Local {
case "shapelayout":
c.Layout = vml.NewOfcShapelayout()
if err := d.DecodeElement(c.Layout, &el); err != nil {
return err
}
case "shapetype":
c.ShapeType = vml.NewShapetype()
if err := d.DecodeElement(c.ShapeType, &el); err != nil {
return err
}
case "shape":
shp := vml.NewShape()
if err := d.DecodeElement(shp, &el); err != nil {
return err
}
c.Shape = append(c.Shape, shp)
}
case xml.EndElement:
break outer
}
}
return nil
}