Documentation ¶
Index ¶
- Constants
- func CopyZipFile(from *zip.File, to *zip.Writer) error
- func ErrorUnknownPackage(p interface{}) error
- func GetLettersFn(rune rune) rune
- func GetNumbersFn(rune rune) rune
- func IsEmptyValue(v reflect.Value) bool
- func MarshalZipFile(fileName string, source interface{}, to *zip.Writer) error
- func Open(f interface{}, docFactory DocumentFactoryFn) (interface{}, error)
- func UniqueName(name string, names []string, nameLimit int) string
- func UnmarshalZipFile(f *zip.File, target interface{}) error
- type ContentTypes
- type DocumentFactoryFn
- type DocumentValidatorFn
- type MarshalFixation
- type MarshalPreparation
- type Package
- type PackageFile
- func (pf *PackageFile) FileName() string
- func (pf *PackageFile) IsNew() bool
- func (pf *PackageFile) LoadIfRequired(callback func())
- func (pf *PackageFile) MarkAsUpdated()
- func (pf *PackageFile) ReadStream() (*StreamFileReader, error)
- func (pf *PackageFile) WriteStream(finalize StreamWriterCallback) (*StreamFileWriter, error)
- type PackageInfo
- func (pkg *PackageInfo) Add(fileName string, content interface{})
- func (pkg *PackageInfo) Close() error
- func (pkg *PackageInfo) ContentTypes() *ContentTypes
- func (pkg *PackageInfo) File(fileName string) interface{}
- func (pkg *PackageInfo) FileName() string
- func (pkg *PackageInfo) Files(pattern *regexp.Regexp) map[string]interface{}
- func (pkg *PackageInfo) IsNew() bool
- func (pkg *PackageInfo) Relationships() *Relationships
- func (pkg *PackageInfo) Remove(fileName string)
- func (pkg *PackageInfo) Save() error
- func (pkg *PackageInfo) SaveAs(fileName string) error
- func (pkg *PackageInfo) SavePackage(f io.Writer) error
- type Relationships
- func (rels *Relationships) AddFile(t ml.RelationType, target string) (int, ml.RID)
- func (rels *Relationships) AddLink(t ml.RelationType, target string) (int, ml.RID)
- func (rels *Relationships) FileName() string
- func (rels *Relationships) GetIdByTarget(target string) ml.RID
- func (rels *Relationships) GetTargetById(id string) string
- func (rels *Relationships) GetTargetByType(t ml.RelationType) string
- func (rels *Relationships) Remove(rid ml.RID)
- func (rels *Relationships) Total() int
- type StreamFileReader
- type StreamFileWriter
- type StreamReader
- type StreamReaderCallback
- type StreamReaderIterator
- type StreamWriter
- type StreamWriterCallback
Examples ¶
Constants ¶
const ( ContentTypeVmlDrawing ml.ContentType = "application/vnd.openxmlformats-officedocument.vmlDrawing" ContentTypeRelationships ml.ContentType = "application/vnd.openxmlformats-package.relationships+xml" )
List of all supported ContentType for any office file - excel, word, powerpoint
Variables ¶
This section is empty.
Functions ¶
func CopyZipFile ¶
CopyZipFile copies a zip file as is to a new zip
func ErrorUnknownPackage ¶
func ErrorUnknownPackage(p interface{}) error
ErrorUnknownPackage returns a common error if DocumentFactoryFn returned invalid result
func GetLettersFn ¶
GetLettersFn is a strings.Map walker to return [a-zA-Z] runes from string
func GetNumbersFn ¶
GetNumbersFn is a strings.Map walker to return [0-9] runes from string
func IsEmptyValue ¶
IsEmptyValue returns true if reflected value is 'empty'
func MarshalZipFile ¶
MarshalZipFile add a file with content of marshaled source to zip
func Open ¶
func Open(f interface{}, docFactory DocumentFactoryFn) (interface{}, error)
Open opens a file with fileName or io.Reader and returns an instance of document
func UniqueName ¶
UniqueName returns next valid unique name with a valid length
func UnmarshalZipFile ¶
UnmarshalZipFile unpacks a zip file into target object
Types ¶
type ContentTypes ¶
type ContentTypes struct {
// contains filtered or unexported fields
}
ContentTypes is helper object that implements some functionality for content types that is a required part of any OOXML document
func (*ContentTypes) CountTypes ¶
func (ct *ContentTypes) CountTypes(contentType ml.ContentType) int
CountTypes returns total number of contentType items
func (*ContentTypes) RegisterContent ¶
func (ct *ContentTypes) RegisterContent(fileName string, contentType ml.ContentType)
RegisterContent adds information about a new content with fileName of contentType
func (*ContentTypes) RegisterType ¶
func (ct *ContentTypes) RegisterType(extension string, contentType ml.ContentType)
RegisterType adds information about a new type of content if there is no such type already
func (*ContentTypes) RemoveContent ¶
func (ct *ContentTypes) RemoveContent(fileName string)
RemoveContent removes information about a content with fileName
type DocumentFactoryFn ¶
type DocumentFactoryFn func(pkg *PackageInfo) (interface{}, error)
DocumentFactoryFn is factory to in a OOXML type specific type
type DocumentValidatorFn ¶
type DocumentValidatorFn func() error
DocumentValidatorFn is callback to validate OOXML document. Using right before saving
type MarshalFixation ¶
MarshalFixation is interface that must be implemented by complex data if it requires some after marshal fixation
type MarshalPreparation ¶
type MarshalPreparation interface {
BeforeMarshalXML() interface{}
}
MarshalPreparation is interface that must be implemented by complex data if it requires some preparation steps before marshaling
type PackageFile ¶
type PackageFile struct {
// contains filtered or unexported fields
}
PackageFile is helper object that implements common functionality for any file of package. E.g. lazy loading, marking as updated.
func NewPackageFile ¶
func NewPackageFile(pkg *PackageInfo, f interface{}, target interface{}, source interface{}) *PackageFile
NewPackageFile creates and returns package file that attached target via file f with source of information to save
func (*PackageFile) FileName ¶
func (pf *PackageFile) FileName() string
FileName returns name of file
func (*PackageFile) IsNew ¶
func (pf *PackageFile) IsNew() bool
IsNew returns true if this file is a new file or false in other case
func (*PackageFile) LoadIfRequired ¶
func (pf *PackageFile) LoadIfRequired(callback func())
LoadIfRequired lazy loads whole content of file into target and call required callback if there is any
func (*PackageFile) MarkAsUpdated ¶
func (pf *PackageFile) MarkAsUpdated()
MarkAsUpdated marks file as updated, so content will be replaced with source's content during packing document. Works only with new files or files that where fully loaded (via LoadIfRequired).
func (*PackageFile) ReadStream ¶
func (pf *PackageFile) ReadStream() (*StreamFileReader, error)
ReadStream opens a zip file for manual reading as stream and return *StreamFileReader for it Files that were opened as stream can't be marked as updated via MarkAsUpdated and will be saved as is Files that were opened as stream must be manually closed via calling Close() to prevent memory leaks
func (*PackageFile) WriteStream ¶
func (pf *PackageFile) WriteStream(finalize StreamWriterCallback) (*StreamFileWriter, error)
WriteStream creates a zip file for manual writing as stream and return StreamFileWriter for it File can be created as stream only once, any further requests will return previously created stream
type PackageInfo ¶
type PackageInfo struct { Validator DocumentValidatorFn // contains filtered or unexported fields }
PackageInfo holds all required information for OOXML package
func NewPackage ¶
func NewPackage(reader interface{}) *PackageInfo
NewPackage returns a new package with zip reader if there is any
func (*PackageInfo) Add ¶
func (pkg *PackageInfo) Add(fileName string, content interface{})
Add is a private method that adds a file to a package
func (*PackageInfo) Close ¶
func (pkg *PackageInfo) Close() error
Close closes current OOXML package
func (*PackageInfo) ContentTypes ¶
func (pkg *PackageInfo) ContentTypes() *ContentTypes
ContentTypes is a getter that returns content types of package
func (*PackageInfo) File ¶
func (pkg *PackageInfo) File(fileName string) interface{}
File is a private method that returns file with required name
func (*PackageInfo) FileName ¶
func (pkg *PackageInfo) FileName() string
FileName is a private method that returns filename of opened file
func (*PackageInfo) Files ¶
func (pkg *PackageInfo) Files(pattern *regexp.Regexp) map[string]interface{}
Files is a private method to get list of all files inside of package, using regexp pattern if required
func (*PackageInfo) IsNew ¶
func (pkg *PackageInfo) IsNew() bool
IsNew returns true if package is a new one or false in other case
func (*PackageInfo) Relationships ¶
func (pkg *PackageInfo) Relationships() *Relationships
Relationships is a getter that returns top-level relationships of package
func (*PackageInfo) Remove ¶
func (pkg *PackageInfo) Remove(fileName string)
Remove is a private method that removes file from a package
func (*PackageInfo) SaveAs ¶
func (pkg *PackageInfo) SaveAs(fileName string) error
SaveAs saves current OOXML package with fileName
func (*PackageInfo) SavePackage ¶
func (pkg *PackageInfo) SavePackage(f io.Writer) error
SavePackage is private method with implementation of saving OOXML document to file
type Relationships ¶
type Relationships struct {
// contains filtered or unexported fields
}
Relationships is a higher level object that wraps OOXML relationships with functionality
func NewRelationships ¶
func NewRelationships(f interface{}, pkg *PackageInfo) *Relationships
NewRelationships creates and returns relationships
func (*Relationships) AddFile ¶
func (rels *Relationships) AddFile(t ml.RelationType, target string) (int, ml.RID)
AddFile adds a new relation of type t to internal target, i.e. file inside of package. For simplicity - use absolute paths.
func (*Relationships) AddLink ¶
func (rels *Relationships) AddLink(t ml.RelationType, target string) (int, ml.RID)
AddLink adds a new relation of type t to external target - e.g.: url
func (*Relationships) FileName ¶
func (rels *Relationships) FileName() string
FileName returns file name of relations
func (*Relationships) GetIdByTarget ¶
func (rels *Relationships) GetIdByTarget(target string) ml.RID
GetIdByTarget returns id of first relation for provided target
func (*Relationships) GetTargetById ¶
func (rels *Relationships) GetTargetById(id string) string
GetTargetById returns target of relation for provided id
func (*Relationships) GetTargetByType ¶
func (rels *Relationships) GetTargetByType(t ml.RelationType) string
GetTargetByType returns target of first relation for provided type
func (*Relationships) Remove ¶
func (rels *Relationships) Remove(rid ml.RID)
Remove removes relation with provided rid
func (*Relationships) Total ¶
func (rels *Relationships) Total() int
Total returns total number of relationships
type StreamFileReader ¶
type StreamFileReader struct { *StreamReader // contains filtered or unexported fields }
StreamFileReader is stream reader for *zip.File
Example ¶
package main import ( "archive/zip" "encoding/xml" "fmt" "github.com/plandem/ooxml" "github.com/plandem/ooxml/ml" "regexp" ) func main() { type OOXmlDoc struct { *ooxml.PackageInfo } //Cell is a direct mapping of XSD CT_Cell type Cell struct { Formula *ml.Reserved `xml:"f,omitempty"` Value string `xml:"v,omitempty"` InlineStr *ml.Reserved `xml:"is,omitempty"` ExtLst *ml.Reserved `xml:"extLst,omitempty"` Ref string `xml:"r,attr"` Style int `xml:"s,attr,omitempty"` Type string `xml:"t,attr,omitempty"` Cm ml.OptionalIndex `xml:"cm,attr,omitempty"` Vm ml.OptionalIndex `xml:"vm,attr,omitempty"` Ph bool `xml:"ph,attr,omitempty"` } type Row struct { Cells []*Cell `xml:"c"` ExtLst *ml.Reserved `xml:"extLst,omitempty"` Ref int `xml:"r,attr,omitempty"` //1-based index Spans string `xml:"spans,attr,omitempty"` Style int `xml:"s,attr,omitempty"` CustomFormat bool `xml:"customFormat,attr,omitempty"` Height float32 `xml:"ht,attr,omitempty"` Hidden bool `xml:"hidden,attr,omitempty"` CustomHeight bool `xml:"customHeight,attr,omitempty"` OutlineLevel uint8 `xml:"outlineLevel,attr,omitempty"` Collapsed bool `xml:"collapsed,attr,omitempty"` ThickTop bool `xml:"thickTop,attr,omitempty"` ThickBot bool `xml:"thickBot,attr,omitempty"` Phonetic bool `xml:"ph,attr,omitempty"` } factory := func(pkg *ooxml.PackageInfo) (interface{}, error) { return &OOXmlDoc{pkg}, nil } //ok for zip.ReadCloser doc, _ := ooxml.Open("./test_files/example_simple.xlsx", factory) odoc, _ := doc.(*OOXmlDoc) defer odoc.Close() var sheetStream *ooxml.StreamFileReader reSheet := regexp.MustCompile(`xl/worksheets/[[:alpha:]]+[\d]+\.xml`) files := odoc.PackageInfo.Files(nil) for _, file := range files { if f, ok := file.(*zip.File); ok { if reSheet.MatchString(f.Name) { sheetStream, _ = ooxml.NewStreamFileReader(f) break } } } var rowIterator ooxml.StreamReaderIterator var hasNextRow bool for next, hasNext := sheetStream.StartIterator(nil); hasNext; { hasNext = next(func(decoder *xml.Decoder, start *xml.StartElement) bool { switch start.Name.Local { case "row": rowIterator, hasNextRow = sheetStream.StartIterator(start) return false } return true }) } for hasNextRow { hasNextRow = rowIterator(func(decoder *xml.Decoder, start *xml.StartElement) bool { if start != nil && start.Name.Local == "row" { var row Row if decoder.DecodeElement(&row, start) == nil { for _, c := range row.Cells { if c.Type == "s" || c.Value == "" { continue } fmt.Printf("%+v\n", c.Value) } return true } } return false }) } }
Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
func NewStreamFileReader ¶
func NewStreamFileReader(f *zip.File) (*StreamFileReader, error)
NewStreamFileReader returns a StreamFileReader for *zip.File f
func (*StreamFileReader) Close ¶
func (s *StreamFileReader) Close() error
Close closes previously opened file for reading
type StreamFileWriter ¶
type StreamFileWriter struct { *StreamWriter // contains filtered or unexported fields }
StreamFileWriter is stream writer for *zip.File
func NewStreamFileWriter ¶
func NewStreamFileWriter(f string, cb StreamWriterCallback) *StreamFileWriter
NewStreamFileWriter returns a StreamFileWriter for fileName f
type StreamReader ¶
StreamReader is a generic stream reader
Example ¶
package main import ( "bytes" "encoding/xml" "fmt" "github.com/plandem/ooxml" ) func main() { type Email struct { Where string `xml:"where,attr"` Addr string } type Address struct { City, State string } type Result struct { XMLName xml.Name `xml:"personNamespace Person"` Name string `xml:"FullName"` Phone string Email []Email Groups []string `xml:"Group>Value"` Address } data := ` <Person> <FullName>Grace R. Emlin</FullName> <Company>Example Inc.</Company> <Email where="home"> <Addr>gre@example.com</Addr> </Email> <Email where='work'> <Addr>gre@work.com</Addr> </Email> <Group> <Value>Friends</Value> <Value>Squash</Value> </Group> <City>Hanga Roa</City> <State>Easter Island</State> </Person> ` rs := ooxml.StreamReader{Decoder: xml.NewDecoder(bytes.NewReader([]byte(data)))} for next, hasNext := rs.StartIterator(nil); hasNext; { hasNext = next(func(decoder *xml.Decoder, start *xml.StartElement) bool { fmt.Println(start.Name.Local) return true }) } }
Output: Person FullName Company Email Addr Email Addr Group Value Value City State
func (*StreamReader) StartIterator ¶
func (sr *StreamReader) StartIterator(start *xml.StartElement) (StreamReaderIterator, bool)
StartIterator returns iterator for all start elements
type StreamReaderCallback ¶
type StreamReaderCallback func(decoder *xml.Decoder, start *xml.StartElement) bool
StreamReaderCallback is type that defines a callback that will be called for each iteration of StreamReaderIterator
type StreamReaderIterator ¶
type StreamReaderIterator func(cb StreamReaderCallback) bool
StreamReaderIterator is type that defines a read stream iterator for all xml.StartElement elements
type StreamWriter ¶
StreamWriter is a generic stream writer
type StreamWriterCallback ¶
StreamWriterCallback is type that defines a callback that will be called to get postponed updates