forked from plandem/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spreadsheet.go
218 lines (182 loc) · 5.66 KB
/
spreadsheet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2017 Andrey Gayvoronsky <[email protected]>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package xlsx
import (
"archive/zip"
"errors"
"fmt"
"github.com/plandem/ooxml"
"github.com/plandem/xlsx/format/styles"
"regexp"
)
//Spreadsheet is a higher level object that wraps OOXML package with XLSX functionality
type Spreadsheet struct {
ooxml.Package
pkg *ooxml.PackageInfo
workbook *workbook
sheets []*sheetInfo
relationships *ooxml.Relationships
sharedStrings *sharedStrings
styleSheet *styleSheet
}
//newSpreadsheet creates an object that implements XLSX functionality
func newSpreadsheet(pkg *ooxml.PackageInfo) (interface{}, error) {
xlDoc := &Spreadsheet{
pkg: pkg,
Package: pkg,
}
pkg.Validator = xlDoc.IsValid
if pkg.IsNew() {
xlDoc.createSpreadsheet()
} else {
xlDoc.readSpreadsheet()
}
return xlDoc, nil
}
//SheetNames returns a names of all sheets
func (xl *Spreadsheet) SheetNames() []string {
sheetNames := make([]string, len(xl.sheets))
for id := range xl.sheets {
sheetNames[id] = xl.workbook.ml.Sheets[id].Name
}
return sheetNames
}
//SheetByName returns a sheet by name with required open mode options
func (xl *Spreadsheet) SheetByName(name string, options ...SheetMode) Sheet {
for id := range xl.sheets {
if name == xl.workbook.ml.Sheets[id].Name {
return xl.Sheet(id, options...)
}
}
return nil
}
//Sheet returns a sheet by 0-based index with required open mode options
func (xl *Spreadsheet) Sheet(i int, options ...SheetMode) Sheet {
if i >= len(xl.sheets) {
return nil
}
mode := sheetModeRead
for _, m := range options {
mode |= m
}
prevMode := xl.sheets[i].sheetMode
//stream mode
if (mode & SheetModeStream) != 0 {
//stream can be opened only if sheet was not opened in normal mode before
if (prevMode & sheetModeRead) != 0 {
panic("You can't open sheet in stream mode after it was opened in normal mode.")
}
//for stream mode we create a copy of sheetInfo to prevent pollution
si := *xl.sheets[i]
sheet := &sheetReadStream{sheetInfo: &si}
si.sheet = sheet
si.sheetMode = mode
sheet.afterOpen()
return sheet
}
//normal mode
if prevMode == sheetModeUnknown {
//to prevent mess with opening same sheet with different modes, we always use same mode as used first time
prevMode = mode | sheetModeWrite
}
si := xl.sheets[i]
sheet := &sheetReadWrite{si}
si.sheet = sheet
si.sheetMode = prevMode
sheet.afterOpen()
return sheet
}
//AddSheet adds a new sheet with name to document
func (xl *Spreadsheet) AddSheet(name string, options ...SheetMode) Sheet {
mode := sheetModeWrite
for _, m := range options {
mode |= m
}
if si := newSheetInfo(fmt.Sprintf("xl/worksheets/sheet%d.xml", len(xl.workbook.ml.Sheets)+1), xl); si != nil {
if (mode & SheetModeStream) != 0 {
sheet := &sheetWriteStream{sheetInfo: si}
si.sheet = sheet
si.sheetMode = mode
sheet.afterCreate(name)
return sheet
}
sheet := &sheetReadWrite{si}
si.sheet = sheet
si.sheetMode = mode | sheetModeRead
sheet.afterCreate(name)
return sheet
}
return nil
}
//Sheets returns iterator for all sheets of Spreadsheet
func (xl *Spreadsheet) Sheets() SheetIterator {
return newSheetIterator(xl)
}
//DeleteSheet deletes the sheet with required 0-based index
func (xl *Spreadsheet) DeleteSheet(i int) {
if i < len(xl.sheets) {
sheet := xl.sheets[i]
if sheet != nil {
rid := xl.workbook.ml.Sheets[i].RID
//remove from document
xl.sheets = append(xl.sheets[:i], xl.sheets[i+1:]...)
//remove from workbook
xl.workbook.ml.Sheets = append(xl.workbook.ml.Sheets[:i], xl.workbook.ml.Sheets[i+1:]...)
xl.workbook.file.MarkAsUpdated()
//remove relation
xl.relationships.Remove(rid)
//remove file
xl.pkg.Remove(sheet.file.FileName())
}
}
}
//AddStyles adds a new style formatting to document and return related ID that can be used lately
func (xl *Spreadsheet) AddStyles(style *styles.Info) styles.DirectStyleID {
return xl.styleSheet.addStyle(style)
}
//ResolveStyles returns style formatting for styleID or nil if there is no any styles with such styleID
func (xl *Spreadsheet) ResolveStyles(styleID styles.DirectStyleID) *styles.Info {
return xl.workbook.doc.styleSheet.resolveDirectStyle(styleID)
}
//IsValid validates document and return error if there is any error. Using right before saving.
func (xl *Spreadsheet) IsValid() error {
if len(xl.sheets) == 0 {
return errors.New("spreadsheet requires at least one worksheet")
}
return nil
}
//readSpreadsheet reads required information from XLSX
func (xl *Spreadsheet) readSpreadsheet() {
files := xl.pkg.Files(nil)
for _, file := range files {
if f, ok := file.(*zip.File); ok {
switch {
case f.Name == "xl/workbook.xml":
xl.workbook = newWorkbook(f, xl)
case f.Name == "xl/_rels/workbook.xml.rels":
xl.relationships = ooxml.NewRelationships(f, xl.pkg)
case f.Name == "xl/sharedStrings.xml":
xl.sharedStrings = newSharedStrings(f, xl)
case f.Name == "xl/styles.xml":
xl.styleSheet = newStyleSheet(f, xl)
}
}
}
//we need populated 'relationships' to resolve index for sheet
reSheet := regexp.MustCompile(`xl/worksheets/[[:alpha:]]+[\d]+\.xml`)
for _, file := range files {
if f, ok := file.(*zip.File); ok {
if reSheet.MatchString(f.Name) {
newSheetInfo(f, xl)
}
}
}
}
//createSpreadsheet initialize a new XLSX document
func (xl *Spreadsheet) createSpreadsheet() {
xl.relationships = ooxml.NewRelationships("xl/_rels/workbook.xml.rels", xl.pkg)
xl.workbook = newWorkbook("xl/workbook.xml", xl)
xl.sharedStrings = newSharedStrings("xl/sharedStrings.xml", xl)
xl.styleSheet = newStyleSheet("xl/styles.xml", xl)
}