-
Notifications
You must be signed in to change notification settings - Fork 0
/
workbook.go
276 lines (227 loc) · 6.88 KB
/
workbook.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package xlsx
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"github.com/jhoonb/archivex"
"github.com/pkg/errors"
)
// Workbook represents a spreadsheet workbook.
type Workbook struct {
FilePath string
worksheets []*Worksheet
tempDirsCreated bool
tempRootDir string
tempDirs *workbookTempDirs
relationships []*relationship
committed bool
}
// WorksheetOptions has options used when creating a new worksheet.
type WorksheetOptions struct {
Name string
}
type workbookTempDirs struct {
rels string
xl string
xlRels string
xlWorksheets string
}
type relationship struct {
relType string
target string
}
// AddWorksheet adds a new worksheet to the workbook.
func (wb *Workbook) AddWorksheet(opts *WorksheetOptions) *Worksheet {
id := len(wb.worksheets) + 1
var fileName bytes.Buffer
fileName.WriteString("sheet")
fileName.WriteString(fmt.Sprint(id))
fileName.WriteString(".xml")
ws := &Worksheet{
workbook: wb,
id: id,
name: opts.Name,
fileName: fileName.String(),
}
wb.worksheets = append(wb.worksheets, ws)
wb.relationships = append(wb.relationships, &relationship{
relType: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
target: path.Join("worksheets", ws.fileName),
})
return ws
}
// HasPendingWorksheets indicates whether or not the workbook has worksheets yet to be committed.
func (wb *Workbook) HasPendingWorksheets() bool {
for i := 0; i < len(wb.worksheets); i++ {
if !wb.worksheets[i].committed {
return true
}
}
return false
}
func (wb *Workbook) createContentTypes() error {
ctFilePath := path.Join(wb.tempRootDir, "[Content_Types].xml")
f, err := os.Create(ctFilePath)
if err != nil {
return errors.Wrapf(err, "failed to create %s", ctFilePath)
}
defer func() {
f.Close()
}()
_, err = f.WriteString(startContentTypes)
if err != nil {
return errors.Wrapf(err, "failed to append START_CONTENT_TYPES to %s", ctFilePath)
}
for i := 0; i < len(wb.worksheets); i++ {
wsFileName := wb.worksheets[i].fileName
_, err = f.WriteString(overrideWorksheetFormat(wsFileName))
if err != nil {
return errors.Wrapf(err, "failed to append override for %s to %s", wsFileName, ctFilePath)
}
}
_, err = f.WriteString(endContentTypes)
if err != nil {
return errors.Wrapf(err, "failed to append END_CONTENT_TYPES to %s", ctFilePath)
}
return nil
}
func (wb *Workbook) createRootRelationships() error {
filePath := path.Join(wb.tempDirs.rels, ".rels")
f, err := os.Create(filePath)
if err != nil {
return errors.Wrapf(err, "failed to create %s", filePath)
}
defer f.Close()
_, err = f.WriteString(rels)
if err != nil {
return errors.Wrapf(err, "failed to write to %s", filePath)
}
return nil
}
func (wb *Workbook) createWorkbookRelationships() error {
filePath := path.Join(wb.tempDirs.xlRels, "workbook.xml.rels")
f, err := os.Create(filePath)
if err != nil {
return errors.Wrapf(err, "failed to create %s", filePath)
}
defer f.Close()
_, err = f.WriteString(startWorkbookRels)
if err != nil {
return errors.Wrapf(err, "failed to append START_WORKBOOK_RELS to %s", filePath)
}
for i := 0; i < len(wb.relationships); i++ {
r := wb.relationships[i]
_, err = f.WriteString(relationshipFormat(i+1, r.relType, r.target))
if err != nil {
return errors.Wrapf(err, "failed to append the relationship with the target %s to %s", r.target, filePath)
}
}
_, err = f.WriteString(endWorkbookRels)
if err != nil {
return errors.Wrapf(err, "failed to append END_WORKBOOK_RELS to %s", filePath)
}
return nil
}
func (wb *Workbook) createMain() error {
filePath := path.Join(wb.tempDirs.xl, "workbook.xml")
f, err := os.Create(filePath)
if err != nil {
return errors.Wrapf(err, "failed to create %s", filePath)
}
defer f.Close()
_, err = f.WriteString(startWorkbook)
if err != nil {
return errors.Wrapf(err, "failed to append START_WORKBOOK to %s", filePath)
}
for i := 0; i < len(wb.worksheets); i++ {
ws := wb.worksheets[i]
_, err = f.WriteString(sheetFormat(ws.name, ws.id))
}
_, err = f.WriteString(endWorkbook)
if err != nil {
return errors.Wrapf(err, "failed to append END_WORKBOOK to %s", filePath)
}
return nil
}
// Commit commits the workbook persisting the data to the specified file.
func (wb *Workbook) Commit() error {
if wb.committed {
return errors.New("can't commit a committed workbook")
}
if len(wb.worksheets) == 0 {
return errors.New("a workbook needs at least one worksheet")
}
if wb.HasPendingWorksheets() {
return errors.New("can't commit if there are still pending worksheets")
}
err := wb.createContentTypes()
if err != nil {
return errors.Wrap(err, "failed to create the content types file")
}
err = wb.createRootRelationships()
if err != nil {
return errors.Wrap(err, "failed to create the root relationships file")
}
err = wb.createWorkbookRelationships()
if err != nil {
return errors.Wrap(err, "failed to create the workbook relationships file")
}
err = wb.createMain()
if err != nil {
return errors.Wrap(err, "failed to create the main file")
}
zip := new(archivex.ZipFile)
zip.Create(strings.TrimSuffix(wb.FilePath, path.Ext(wb.FilePath)))
zip.AddAll(wb.tempRootDir, false)
err = zip.Close()
if err != nil {
return errors.Wrap(err, "failed to close the zip file")
}
curPath := fmt.Sprintf("%s.zip", strings.TrimSuffix(wb.FilePath, path.Ext(wb.FilePath)))
newPath := wb.FilePath
err = os.Rename(curPath, newPath)
if err != nil {
return errors.Wrapf(err, "failed to rename file %s to %s", curPath, newPath)
}
wb.committed = true
return nil
}
func (wb *Workbook) createTempDirs() error {
if wb.tempDirsCreated {
return errors.New("temporary directories already created")
}
tempRootDir, err := ioutil.TempDir("", "xlsx-")
if err != nil {
return errors.Wrap(err, "failed to create a temporary directory for the workbook files")
}
wb.tempRootDir = tempRootDir
// /_rels
wb.tempDirs.rels = path.Join(wb.tempRootDir, "_rels")
err = os.Mkdir(wb.tempDirs.rels, os.ModeDir|os.ModePerm)
if err != nil {
return errors.Wrapf(err, "failed to create temporary directory \"%s\"", wb.tempDirs.rels)
}
// /xl
wb.tempDirs.xl = path.Join(wb.tempRootDir, "xl")
err = os.Mkdir(wb.tempDirs.xl, os.ModeDir|os.ModePerm)
if err != nil {
return errors.Wrapf(err, "failed to create temporary directory \"%s\"", wb.tempDirs.xl)
}
// /xl/_rels
wb.tempDirs.xlRels = path.Join(wb.tempDirs.xl, "_rels")
err = os.Mkdir(wb.tempDirs.xlRels, os.ModeDir|os.ModePerm)
if err != nil {
return errors.Wrapf(err, "failed to create temporary directory \"%s\"", wb.tempDirs.xlRels)
}
// /xl/worksheets
wb.tempDirs.xlWorksheets = path.Join(wb.tempDirs.xl, "worksheets")
err = os.Mkdir(wb.tempDirs.xlWorksheets, os.ModeDir|os.ModePerm)
if err != nil {
return errors.Wrapf(err, "failed to create temporary directory \"%s\"", wb.tempDirs.xlWorksheets)
}
wb.tempDirsCreated = true
return nil
}