forked from qax-os/excelize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
direct.go
269 lines (251 loc) · 7.16 KB
/
direct.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
package excelize
import (
"bytes"
"errors"
"fmt"
"io"
"strconv"
"sync"
)
// DirectWriter is a simpler and optimized version of the StreamWriter. Its primary use is sending large amount of sheet data row by row directly
// to a io.Writer. The typical use case is an API writing directly to a TCP connection, with minimal server side buffering.
type DirectWriter struct {
sync.RWMutex
File *File
Sheet string
SheetID int
cols string
worksheet *xlsxWorksheet
sheetPath string
maxBufferSize int
bytesWritten int64
buf []byte
out io.Writer
done chan bool
rowCount int
maxColLengths []int
waitMode bool
}
// NewDirectWriter return a new DirectWriter for the given sheet name. If the sheet doesn't yet exists it is created.
// Similar limitations apply as when using the StreamWriter. To enable writing an xlsx file concurrently to
// a io.Writer you must:
//
// - create a File.
//
// - create at least one DirectWriter.
//
// - launch writing using file.WriteTo() in a separate goroutine, this call will block until all direct writers are closed.
//
// - add data using AddRow, then call Close.
//
// - wait for the goroutine to return
func (f *File) NewDirectWriter(sheet string, maxBufferSize int) (*DirectWriter, error) {
_ = f.NewSheet(sheet)
sheetID := f.getSheetID(sheet)
if sheetID == -1 {
return nil, errors.New("bug: sheetID not found after call to NewSheet")
}
dw := &DirectWriter{
File: f,
Sheet: sheet,
SheetID: sheetID,
maxBufferSize: maxBufferSize,
done: make(chan bool),
}
var err error
dw.worksheet, err = f.workSheetReader(sheet)
if err != nil {
return nil, err
}
dw.sheetPath = f.sheetMap[trimSheetName(sheet)]
f.directWriters = append(f.directWriters, dw)
return dw, err
}
// SetWait enables or disables the wait mode. In wait mode nothing is flushed to writer (if any), even if the buffer grows beyond maxBufferSize.
func (dw *DirectWriter) SetWait(b bool) error {
if b {
if dw.bytesWritten > 0 {
return errors.New("Can't enable wait mode since first data already written.")
}
dw.waitMode = true
return nil
}
dw.waitMode = false
return nil
}
// AddRow is used for streaming a large data file row by row, without any gaps.
// It omits cell reference values and only accept []Cell to reduce interface{} related allocations.
// It returns the number of bytes currently in the write buffer.
func (dw *DirectWriter) AddRow(values []Cell, opts ...RowOpts) (buffered int, err error) {
dw.rowCount++
dw.buf = append(dw.buf, `<row r="`...)
dw.buf = strconv.AppendInt(dw.buf, int64(dw.rowCount), 10)
dw.buf = append(dw.buf, '"')
if len(opts) > 0 {
attrs, err := marshalRowAttrs(opts...)
if err != nil {
return len(dw.buf), err
}
dw.buf = append(dw.buf, attrs...)
}
dw.buf = append(dw.buf, '>')
if len(values) > len(dw.maxColLengths) {
l := make([]int, len(values))
copy(l, dw.maxColLengths)
dw.maxColLengths = l
}
for i, val := range values {
c := xlsxC{
S: val.StyleID,
}
if val.Formula != "" {
c.F = &xlsxF{Content: val.Formula}
}
if err := setCellValFunc(&c, val.Value); err != nil {
dw.buf = append(dw.buf, "</row>"...)
return len(dw.buf), err
}
if l := len(c.V); l > dw.maxColLengths[i] {
dw.maxColLengths[i] = l
}
dw.buf = appendCellNoRef(dw.buf, c)
}
dw.buf = append(dw.buf, "</row>"...)
if len(dw.buf) > dw.maxBufferSize && !dw.waitMode {
err := dw.tryFlush()
return len(dw.buf), err
}
return len(dw.buf), nil
}
// MaxColumnLengths returns the max lengths (in bytes as written to XML) for each column written so far.
func (dw *DirectWriter) MaxColumnLengths() []int {
return dw.maxColLengths
}
// SetColWidth provides a function to set the width of a single column or
// multiple columns for the DirectWriter. Since column definitions need to be written before sheet data, either use this
// function before the first call to AddRow, or set the writer in wait mode using SetWait.
func (dw *DirectWriter) SetColWidth(min, max int, width float64) error {
if dw.bytesWritten > 0 {
return errors.New("Can't set col width since first data already written.")
}
if min > TotalColumns || max > TotalColumns {
return ErrColumnNumber
}
if min < 1 || max < 1 {
return ErrColumnNumber
}
if width > MaxColumnWidth {
return ErrColumnWidth
}
if min > max {
min, max = max, min
}
dw.cols += fmt.Sprintf(`<col min="%d" max="%d" width="%f" customWidth="1"/>`, min, max, width)
return nil
}
// Close ends the streaming writing process.
func (dw *DirectWriter) Close() error {
dw.buf = append(dw.buf, `</sheetData>`...)
bulkAppendFields(dw, dw.worksheet, 8, 15)
bulkAppendFields(dw, dw.worksheet, 17, 38)
bulkAppendFields(dw, dw.worksheet, 40, 40)
dw.buf = append(dw.buf, `</worksheet>`...)
if err := dw.tryFlush(); err != nil {
return err
}
dw.File.Sheet.Delete(dw.sheetPath)
delete(dw.File.checked, dw.sheetPath)
dw.File.Pkg.Delete(dw.sheetPath)
close(dw.done)
return nil
}
// WriteTo writes the output of the DirectWriter to w. The call will block until the DirectWriter is closed by a call to Close.
func (dw *DirectWriter) WriteTo(w io.Writer) (int64, error) {
select {
case <-dw.done:
if dw.bytesWritten > 0 {
return 0, errors.New("Cant't write to new writer w since part of the data already been written and flushed.")
}
n, err := w.Write(dw.buildHeader())
if err != nil {
return int64(n), err
}
n2, err := w.Write(dw.buf)
return int64(n + n2), err
default:
dw.Lock()
dw.out = w
dw.Unlock()
<-dw.done
return dw.bytesWritten, nil
}
}
func (dw *DirectWriter) Write(p []byte) (n int, err error) {
dw.buf = append(dw.buf, p...)
return len(p), nil
}
func (dw *DirectWriter) buildHeader() []byte {
var header bytes.Buffer
header.WriteString(XMLHeader + `<worksheet` + templateNamespaceIDMap)
bulkAppendFields(&header, dw.worksheet, 2, 5)
if len(dw.cols) > 0 {
header.WriteString("<cols>" + dw.cols + "</cols>")
}
header.WriteString(`<sheetData>`)
return header.Bytes()
}
func (dw *DirectWriter) tryFlush() error {
dw.Lock()
if dw.out == nil {
dw.Unlock()
return nil
}
if dw.bytesWritten == 0 {
n, err := dw.out.Write(dw.buildHeader())
if err != nil {
return err
}
dw.bytesWritten += int64(n)
}
n, err := dw.out.Write(dw.buf)
dw.Unlock()
if err != nil {
return err
}
dw.bytesWritten += int64(n)
dw.buf = dw.buf[:0]
return nil
}
func appendCellNoRef(dst []byte, c xlsxC) []byte {
dst = append(dst, `<c`...)
if c.XMLSpace.Value != "" {
dst = append(dst, ` xml:`...)
dst = append(dst, c.XMLSpace.Name.Local...)
dst = append(dst, `="`...)
dst = append(dst, c.XMLSpace.Value...)
dst = append(dst, '"')
}
if c.S != 0 {
dst = append(dst, ` s="`...)
dst = strconv.AppendInt(dst, int64(c.S), 10)
dst = append(dst, '"')
}
if c.T != "" {
dst = append(dst, ` t="`...)
dst = append(dst, c.T...)
dst = append(dst, '"')
}
dst = append(dst, '>')
if c.F != nil {
dst = append(dst, `<f>`...)
dst = appendEscapedString(dst, c.F.Content, true)
dst = append(dst, `</f>`...)
}
if c.V != "" {
dst = append(dst, `<v>`...)
dst = appendEscapedString(dst, c.V, true)
dst = append(dst, `</v>`...)
}
dst = append(dst, `</c>`...)
return dst
}