forked from plandem/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet_writestream.go
255 lines (200 loc) · 5.49 KB
/
sheet_writestream.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
package xlsx
import (
"encoding/xml"
"github.com/plandem/ooxml"
"github.com/plandem/xlsx/format/conditional"
"github.com/plandem/xlsx/internal/ml"
"github.com/plandem/xlsx/types"
"github.com/plandem/xlsx/types/options/sheet"
)
type sheetWriteStream struct {
*sheetInfo
stream *ooxml.StreamFileWriter
worksheet xml.StartElement
sheetData xml.StartElement
currentRow *ml.Row
}
var _ Sheet = (*sheetWriteStream)(nil)
//expandIfRequired expands grid to required dimension
func (s *sheetWriteStream) expandIfRequired(colIndex, rowIndex int) {
curWidth, curHeight := s.Dimension()
nextWidth, nextHeight := colIndex+1, rowIndex+1
if nextWidth < curWidth {
nextWidth = curWidth
}
if nextHeight < curHeight {
nextHeight = curHeight
}
//expand current row, if required
if s.currentRow != nil && len(s.currentRow.Cells) < nextWidth {
s.currentRow.Cells = append(s.currentRow.Cells, make([]*ml.Cell, nextWidth-curWidth)...)
}
//if size is fit to current, then ignore
if curWidth >= nextWidth && curHeight >= nextHeight {
return
}
s.ml.Dimension = &ml.SheetDimension{Bounds: types.BoundsFromIndexes(0, 0, nextWidth-1, nextHeight-1)}
}
func (s *sheetWriteStream) Cell(colIndex, rowIndex int) *Cell {
s.expandIfRequired(colIndex, rowIndex)
var data *ml.Cell
row := s.Row(rowIndex)
data = row.ml.Cells[colIndex]
//if there is no any data for this cell, then create it
if data == nil {
data = &ml.Cell{
Ref: types.CellRefFromIndexes(colIndex, rowIndex),
}
row.ml.Cells[colIndex] = data
}
return &Cell{ml: data, sheet: s.sheetInfo}
}
func (s *sheetWriteStream) emptyDataRow(indexRef int) *ml.Row {
width, _ := s.Dimension()
return &ml.Row{
Ref: indexRef,
Cells: make([]*ml.Cell, width),
}
}
func (s *sheetWriteStream) saveCurrentRow() {
hasCells := false
for i, c := range s.currentRow.Cells {
if isCellEmpty(c) {
s.currentRow.Cells[i] = nil
} else {
hasCells = true
}
}
if !hasCells {
s.currentRow.Cells = nil
}
if !isRowEmpty(s.currentRow) {
if err := s.stream.EncodeElement(s.currentRow, xml.StartElement{Name: xml.Name{Local: "row"}}); err != nil {
panic(err)
}
}
}
func (s *sheetWriteStream) Row(index int) *Row {
s.expandIfRequired(0, index)
indexRef := index + 1
var data *ml.Row
if s.currentRow == nil {
data = s.emptyDataRow(indexRef)
s.currentRow = data
} else {
if indexRef < s.currentRow.Ref {
//return empty row, if request previous row
data = s.emptyDataRow(indexRef)
} else if indexRef == s.currentRow.Ref {
//return current row, if same index
data = s.currentRow
} else {
s.saveCurrentRow()
//create a new row
data = s.emptyDataRow(indexRef)
s.currentRow = data
}
}
return &Row{
data,
newRange(s, 0, len(data.Cells)-1, index, index),
}
}
func (s *sheetWriteStream) HasRow(index int) bool {
panic(errorNotSupportedWrite)
}
//afterCreate initializes a new sheet
func (s *sheetWriteStream) afterCreate(name string) {
//register file
s.sheetInfo.afterCreate(name)
//open worksheet
s.worksheet = xml.StartElement{Name: xml.Name{
Space: "http://schemas.openxmlformats.org/spreadsheetml/2006/main",
Local: "worksheet",
}}
var err error
if s.stream, err = s.file.WriteStream(true, s.finalize); err == nil {
if err = s.stream.EncodeToken(s.worksheet); err == nil {
s.sheetData = xml.StartElement{Name: xml.Name{
Local: "sheetData",
}}
err = s.stream.EncodeToken(s.sheetData)
}
}
if err != nil {
panic(err)
}
}
func (s *sheetWriteStream) finalize() error {
s.saveCurrentRow()
//close sheetData
if err := s.stream.EncodeToken(s.sheetData.End()); err != nil {
return err
}
//close worksheet
if err := s.stream.EncodeToken(s.worksheet.End()); err != nil {
return err
}
return nil
}
func (s *sheetWriteStream) Rows() RowIterator {
panic(errorNotSupported)
}
//not allowed methods for stream reading mode
func (s *sheetWriteStream) Col(index int) *Col {
panic(errorNotSupported)
}
func (s *sheetWriteStream) Cols() ColIterator {
panic(errorNotSupported)
}
func (s *sheetWriteStream) InsertCol(index int) *Col {
panic(errorNotSupported)
}
func (s *sheetWriteStream) InsertRow(index int) *Row {
panic(errorNotSupported)
}
func (s *sheetWriteStream) DeleteRow(index int) {
panic(errorNotSupported)
}
func (s *sheetWriteStream) DeleteCol(index int) {
panic(errorNotSupported)
}
func (s *sheetWriteStream) SetDimension(cols, rows int) {
panic(errorNotSupported)
}
func (s *sheetWriteStream) SetActive() {
panic(errorNotSupported)
}
func (s *sheetWriteStream) SetOptions(o *options.Info) {
panic(errorNotSupported)
}
func (s *sheetWriteStream) SetName(name string) {
panic(errorNotSupported)
}
func (s *sheetWriteStream) MergeRows(fromIndex, toIndex int) error {
panic(errorNotSupported)
}
func (s *sheetWriteStream) MergeCols(fromIndex, toIndex int) error {
panic(errorNotSupported)
}
func (s *sheetWriteStream) SplitRows(fromIndex, toIndex int) {
panic(errorNotSupported)
}
func (s *sheetWriteStream) SplitCols(fromIndex, toIndex int) {
panic(errorNotSupported)
}
func (s *sheetWriteStream) AddConditional(conditional *conditional.Info, refs ...types.Ref) error {
panic(errorNotSupported)
}
func (s *sheetWriteStream) DeleteConditional(refs ...types.Ref) {
panic(errorNotSupported)
}
func (s *sheetWriteStream) AutoFilter(ref types.Ref, settings ...interface{}) {
panic(errorNotSupported)
}
func (s *sheetWriteStream) AddFilter(colIndex int, settings ...interface{}) error {
panic(errorNotSupported)
}
func (s *sheetWriteStream) DeleteFilter(colIndex int) {
panic(errorNotSupported)
}