-
Notifications
You must be signed in to change notification settings - Fork 0
/
excelizeam.go
779 lines (701 loc) · 23.2 KB
/
excelizeam.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
package excelizeam
import (
"crypto/sha1"
"errors"
"fmt"
"io"
"strconv"
"strings"
"sync"
"golang.org/x/sync/errgroup"
"github.com/tomtwinkle/excelizeam/excelizestyle"
"github.com/xuri/excelize/v2"
)
var (
ErrOverrideCellValue = errors.New("override cell value")
ErrOverrideCellStyle = errors.New("override cell style")
)
type Excelizeam interface {
// Preparations in advance
// SetDefaultBorderStyle Set default cell border
// For example, use when you want to paint the cell background white
SetDefaultBorderStyle(style excelizestyle.BorderStyle, color excelizestyle.BorderColor) error
// Excelize StreamWriter Wrapper
SetPageMargins(options *excelize.PageLayoutMarginsOptions) error
SetPageLayout(options *excelize.PageLayoutOptions) error
GetPageLayout() (excelize.PageLayoutOptions, error)
SetColWidth(colIndex int, width float64) error
SetColWidthRange(colIndexMin, colIndexMax int, width float64) error
MergeCell(startColIndex, startRowIndex, endColIndex, endRowIndex int) error
// SetCellValue Set value and style to cell
SetCellValue(colIndex, rowIndex int, value interface{}, style *excelize.Style, overrideValue, overrideStyle bool) error
// SetCellValueAsync Set value and style to cell asynchronously
SetCellValueAsync(colIndex, rowIndex int, value interface{}, style *excelize.Style, overrideStyle bool)
// SetStyleCell Set style to cell
SetStyleCell(colIndex, rowIndex int, style excelize.Style, override bool) error
// SetStyleCellAsync Set style to cell asynchronously
SetStyleCellAsync(colIndex, rowIndex int, style excelize.Style, override bool)
// SetStyleCellRange Set style to cell with range
SetStyleCellRange(startColIndex, startRowIndex, endColIndex, endRowIndex int, style excelize.Style, override bool) error
// SetStyleCellRangeAsync Set style to cell with range asynchronously
SetStyleCellRangeAsync(startColIndex, startRowIndex, endColIndex, endRowIndex int, style excelize.Style, override bool)
// SetBorderRange Set border around cell range
SetBorderRange(startColIndex, startRowIndex, endColIndex, endRowIndex int, borderRange BorderRange, override bool) error
// SetBorderRangeAsync Set border around cell range asynchronously
SetBorderRangeAsync(startColIndex, startRowIndex, endColIndex, endRowIndex int, borderRange BorderRange, override bool)
// Wait
// Wait for all running asynchronous operations to finish
Wait() error
// Write StreamWriter
Write(w io.Writer) error
// File Get the original excelize.File
File() (*excelize.File, error)
// CSVRecords Make csv records
CSVRecords() ([][]string, error)
}
type excelizeam struct {
sw *excelize.StreamWriter
file *excelize.File
eg errgroup.Group
mu sync.Mutex
maxRow int
maxCol int
defaultBorder *DefaultBorders
styleStore sync.Map
cellStore sync.Map
}
type DefaultBorders struct {
StyleID int
Top excelize.Border
Bottom excelize.Border
Left excelize.Border
Right excelize.Border
}
type BorderItem struct {
Style excelizestyle.BorderStyle
Color excelizestyle.BorderColor
}
type BorderRange struct {
Top *BorderItem
Bottom *BorderItem
Left *BorderItem
Right *BorderItem
Inside *BorderItem
}
type StoredStyle struct {
StyleID int
Style *excelize.Style
}
type Cell struct {
StyleID int
Value interface{}
}
func New(sheetName string) (Excelizeam, error) {
f := excelize.NewFile()
err := f.SetSheetName("Sheet1", sheetName)
if err != nil {
return nil, err
}
sw, err := f.NewStreamWriter(sheetName)
if err != nil {
return nil, err
}
return &excelizeam{sw: sw, file: f}, nil
}
func (e *excelizeam) SetDefaultBorderStyle(style excelizestyle.BorderStyle, color excelizestyle.BorderColor) error {
db := &DefaultBorders{
Top: excelizestyle.Border(excelizestyle.BorderPositionTop, style, color),
Bottom: excelizestyle.Border(excelizestyle.BorderPositionBottom, style, color),
Left: excelizestyle.Border(excelizestyle.BorderPositionLeft, style, color),
Right: excelizestyle.Border(excelizestyle.BorderPositionRight, style, color),
}
styleID, err := e.getStyleID(&excelize.Style{
Border: []excelize.Border{
db.Top,
db.Bottom,
db.Left,
db.Right,
},
})
if err != nil {
return err
}
db.StyleID = styleID
e.defaultBorder = db
return nil
}
func (e *excelizeam) SetColWidth(colIndex int, width float64) error {
return e.sw.SetColWidth(colIndex, colIndex, width)
}
func (e *excelizeam) SetColWidthRange(colIndexMin, colIndexMax int, width float64) error {
return e.sw.SetColWidth(colIndexMin, colIndexMax, width)
}
func (e *excelizeam) SetPageMargins(options *excelize.PageLayoutMarginsOptions) error {
return e.file.SetPageMargins(
e.sw.Sheet,
options,
)
}
func (e *excelizeam) SetPageLayout(options *excelize.PageLayoutOptions) error {
return e.file.SetPageLayout(e.sw.Sheet, options)
}
func (e *excelizeam) GetPageLayout() (excelize.PageLayoutOptions, error) {
return e.file.GetPageLayout(e.sw.Sheet)
}
func (e *excelizeam) MergeCell(startColIndex, startRowIndex, endColIndex, endRowIndex int) error {
startCell, err := excelize.CoordinatesToCellName(startColIndex, startRowIndex)
if err != nil {
return err
}
endCell, err := excelize.CoordinatesToCellName(endColIndex, endRowIndex)
if err != nil {
return err
}
return e.sw.MergeCell(startCell, endCell)
}
func (e *excelizeam) SetCellValueAsync(colIndex, rowIndex int, value interface{}, style *excelize.Style, overrideStyle bool) {
e.eg.Go(func() error {
return e.setCellValue(colIndex, rowIndex, value, style, false, overrideStyle)
})
}
func (e *excelizeam) SetCellValue(colIndex, rowIndex int, value interface{}, style *excelize.Style, overrideValue bool, overrideStyle bool) error {
if err := e.eg.Wait(); err != nil {
return err
}
return e.setCellValue(colIndex, rowIndex, value, style, overrideValue, overrideStyle)
}
func (e *excelizeam) setCellValue(colIndex, rowIndex int, value interface{}, style *excelize.Style, overrideValue bool, overrideStyle bool) error {
e.checkMaxIndex(colIndex, rowIndex)
key := e.getCacheKey(colIndex, rowIndex)
styleID, err := e.getStyleID(style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: value,
}); ok {
cell := cached.(*Cell)
if cell.Value != nil && value != nil && !overrideValue {
return ErrOverrideCellValue
}
if value != nil {
cell.Value = value
}
if style != nil {
if cell.StyleID > 0 {
if !overrideStyle {
return ErrOverrideCellStyle
}
styleID, err = e.overrideStyle(cell.StyleID, *style)
if err != nil {
return err
}
}
cell.StyleID = styleID
}
}
return nil
}
func (e *excelizeam) SetStyleCellAsync(colIndex, rowIndex int, style excelize.Style, override bool) {
e.eg.Go(func() error {
err := e.setStyleCell(colIndex, rowIndex, style, override)
return err
})
}
func (e *excelizeam) SetStyleCell(colIndex, rowIndex int, style excelize.Style, override bool) error {
if err := e.eg.Wait(); err != nil {
return err
}
return e.setStyleCell(colIndex, rowIndex, style, override)
}
func (e *excelizeam) setStyleCell(colIndex, rowIndex int, style excelize.Style, override bool) error {
e.checkMaxIndex(colIndex, rowIndex)
key := e.getCacheKey(colIndex, rowIndex)
styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: nil,
}); ok {
c := cached.(*Cell)
if c.StyleID > 0 {
if !override {
return ErrOverrideCellStyle
}
styleID, err = e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
c.StyleID = styleID
}
return nil
}
return nil
}
func (e *excelizeam) SetStyleCellRangeAsync(startColIndex, startRowIndex, endColIndex, endRowIndex int, style excelize.Style, override bool) {
e.eg.Go(func() error {
err := e.setStyleCellRange(startColIndex, startRowIndex, endColIndex, endRowIndex, style, override)
return err
})
}
func (e *excelizeam) SetStyleCellRange(startColIndex, startRowIndex, endColIndex, endRowIndex int, style excelize.Style, override bool) error {
if err := e.eg.Wait(); err != nil {
return err
}
return e.setStyleCellRange(startColIndex, startRowIndex, endColIndex, endRowIndex, style, override)
}
func (e *excelizeam) setStyleCellRange(startColIndex, startRowIndex, endColIndex, endRowIndex int, style excelize.Style, override bool) error {
e.checkMaxIndex(endColIndex, endRowIndex)
for rowIdx := startRowIndex; rowIdx <= endRowIndex; rowIdx++ {
for colIdx := startColIndex; colIdx <= endColIndex; colIdx++ {
key := e.getCacheKey(colIdx, rowIdx)
styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: nil,
}); ok {
c := cached.(*Cell)
if c.StyleID > 0 {
if !override {
return ErrOverrideCellStyle
}
styleID, err = e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
}
c.StyleID = styleID
}
}
}
return nil
}
func (e *excelizeam) SetBorderRangeAsync(startColIndex, startRowIndex, endColIndex, endRowIndex int, borderRange BorderRange, override bool) {
e.eg.Go(func() error {
err := e.setBorderRange(startColIndex, startRowIndex, endColIndex, endRowIndex, borderRange, override)
return err
})
}
func (e *excelizeam) SetBorderRange(startColIndex, startRowIndex, endColIndex, endRowIndex int, borderRange BorderRange, override bool) error {
if err := e.eg.Wait(); err != nil {
return err
}
return e.setBorderRange(startColIndex, startRowIndex, endColIndex, endRowIndex, borderRange, override)
}
func (e *excelizeam) setBorderRange(startColIndex, startRowIndex, endColIndex, endRowIndex int, borderRange BorderRange, override bool) error {
e.checkMaxIndex(endColIndex, endRowIndex)
for rowIdx := startRowIndex; rowIdx <= endRowIndex; rowIdx++ {
for colIdx := startColIndex; colIdx <= endColIndex; colIdx++ {
key := e.getCacheKey(colIdx, rowIdx)
borderStyles := make([]excelize.Border, 0, 4)
switch {
case rowIdx == startRowIndex && colIdx == startColIndex: // TopLeft
if borderRange.Top != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionTop, borderRange.Top.Style, borderRange.Top.Color),
)
}
if borderRange.Left != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionLeft, borderRange.Left.Style, borderRange.Left.Color),
)
}
if borderRange.Inside != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionBottom, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionRight, borderRange.Inside.Style, borderRange.Inside.Color),
)
}
case rowIdx == startRowIndex && colIdx > startColIndex && colIdx < endColIndex: // TopMiddle
if borderRange.Top != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionTop, borderRange.Top.Style, borderRange.Top.Color),
)
}
if borderRange.Inside != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionBottom, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionLeft, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionRight, borderRange.Inside.Style, borderRange.Inside.Color),
)
}
case rowIdx == startRowIndex && colIdx == endColIndex: // TopRight
if borderRange.Top != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionTop, borderRange.Top.Style, borderRange.Top.Color),
)
}
if borderRange.Right != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionRight, borderRange.Right.Style, borderRange.Right.Color),
)
}
if borderRange.Inside != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionBottom, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionLeft, borderRange.Inside.Style, borderRange.Inside.Color),
)
}
case rowIdx > startRowIndex && rowIdx < endRowIndex && colIdx == startColIndex: // MiddleLeft
if borderRange.Left != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionLeft, borderRange.Left.Style, borderRange.Left.Color),
)
}
if borderRange.Inside != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionTop, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionBottom, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionRight, borderRange.Inside.Style, borderRange.Inside.Color),
)
}
case rowIdx == endRowIndex && colIdx == startColIndex: // BottomLeft
if borderRange.Bottom != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionBottom, borderRange.Bottom.Style, borderRange.Bottom.Color),
)
}
if borderRange.Left != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionLeft, borderRange.Left.Style, borderRange.Left.Color),
)
}
if borderRange.Inside != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionTop, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionRight, borderRange.Inside.Style, borderRange.Inside.Color),
)
}
case rowIdx == endRowIndex && colIdx > startColIndex && colIdx < endColIndex: // BottomMiddle
if borderRange.Bottom != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionBottom, borderRange.Bottom.Style, borderRange.Bottom.Color),
)
}
if borderRange.Inside != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionTop, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionLeft, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionRight, borderRange.Inside.Style, borderRange.Inside.Color),
)
}
case rowIdx == endRowIndex && colIdx == endColIndex: // BottomRight
if borderRange.Bottom != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionBottom, borderRange.Bottom.Style, borderRange.Bottom.Color),
)
}
if borderRange.Right != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionRight, borderRange.Right.Style, borderRange.Right.Color),
)
}
if borderRange.Inside != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionTop, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionLeft, borderRange.Inside.Style, borderRange.Inside.Color),
)
}
case rowIdx > startRowIndex && rowIdx < endRowIndex && colIdx == endColIndex: // MiddleRight
if borderRange.Right != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionRight, borderRange.Right.Style, borderRange.Right.Color),
)
}
if borderRange.Inside != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionTop, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionBottom, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionLeft, borderRange.Inside.Style, borderRange.Inside.Color),
)
}
default: // InsideBorder
if borderRange.Inside != nil {
borderStyles = append(
borderStyles,
excelizestyle.Border(excelizestyle.BorderPositionTop, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionBottom, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionLeft, borderRange.Inside.Style, borderRange.Inside.Color),
excelizestyle.Border(excelizestyle.BorderPositionRight, borderRange.Inside.Style, borderRange.Inside.Color),
)
}
}
if len(borderStyles) == 0 {
continue
}
style := excelize.Style{Border: borderStyles}
styleID, err := e.getStyleID(&style)
if err != nil {
return err
}
if cached, ok := e.cellStore.LoadOrStore(key, &Cell{
StyleID: styleID,
Value: nil,
}); ok {
c := cached.(*Cell)
if c.StyleID > 0 {
if !override {
return ErrOverrideCellStyle
}
styleID, err = e.overrideStyle(c.StyleID, style)
if err != nil {
return err
}
}
c.StyleID = styleID
continue
}
}
}
return nil
}
func (e *excelizeam) getStyleID(style *excelize.Style) (int, error) {
var styl excelize.Style
if style == nil {
return 0, nil
}
if style != nil {
styl = *style
}
hash := fmt.Sprintf("%x", sha1.Sum([]byte(fmt.Sprintf("%+v", styl))))
var styleID int
if s, ok := e.styleStore.Load(hash); ok {
styleID = s.(StoredStyle).StyleID
} else {
var err error
styleID, err = e.file.NewStyle(&styl)
if err != nil {
return 0, err
}
e.styleStore.Store(hash, StoredStyle{
StyleID: styleID,
Style: style,
})
}
return styleID, nil
}
func (e *excelizeam) overrideStyle(originStyleID int, overrideStyle excelize.Style) (int, error) {
var originStyle *excelize.Style
e.styleStore.Range(func(_, value any) bool {
if value.(StoredStyle).StyleID == originStyleID {
originStyle = value.(StoredStyle).Style
return false
}
return true
})
if originStyle == nil {
return e.getStyleID(&overrideStyle)
}
style := new(excelize.Style)
style.Fill = originStyle.Fill
style.Alignment = originStyle.Alignment
style.Font = originStyle.Font
style.CustomNumFmt = originStyle.CustomNumFmt
style.DecimalPlaces = originStyle.DecimalPlaces
style.NegRed = originStyle.NegRed
style.NumFmt = originStyle.NumFmt
style.Protection = originStyle.Protection
// Border
borders := make([]excelize.Border, 0)
if overrideBorder, ok := excelizestyle.FindBorder(overrideStyle.Border, excelizestyle.BorderPositionTop); ok {
borders = append(borders, *overrideBorder)
} else if originBorder, ok := excelizestyle.FindBorder(originStyle.Border, excelizestyle.BorderPositionTop); ok {
borders = append(borders, *originBorder)
}
if overrideBorder, ok := excelizestyle.FindBorder(overrideStyle.Border, excelizestyle.BorderPositionBottom); ok {
borders = append(borders, *overrideBorder)
} else if originBorder, ok := excelizestyle.FindBorder(originStyle.Border, excelizestyle.BorderPositionBottom); ok {
borders = append(borders, *originBorder)
}
if overrideBorder, ok := excelizestyle.FindBorder(overrideStyle.Border, excelizestyle.BorderPositionLeft); ok {
borders = append(borders, *overrideBorder)
} else if originBorder, ok := excelizestyle.FindBorder(originStyle.Border, excelizestyle.BorderPositionLeft); ok {
borders = append(borders, *originBorder)
}
if overrideBorder, ok := excelizestyle.FindBorder(overrideStyle.Border, excelizestyle.BorderPositionRight); ok {
borders = append(borders, *overrideBorder)
} else if originBorder, ok := excelizestyle.FindBorder(originStyle.Border, excelizestyle.BorderPositionRight); ok {
borders = append(borders, *originBorder)
}
style.Border = borders
// Fill
if overrideStyle.Fill.Type != "" {
style.Fill = overrideStyle.Fill
}
// Alignment
if overrideStyle.Alignment != nil {
style.Alignment = overrideStyle.Alignment
}
// Font
if overrideStyle.Font != nil {
style.Font = overrideStyle.Font
}
// CustomNumFmt
if overrideStyle.CustomNumFmt != nil {
style.CustomNumFmt = overrideStyle.CustomNumFmt
}
// DecimalPlaces
if overrideStyle.DecimalPlaces != nil {
style.DecimalPlaces = overrideStyle.DecimalPlaces
}
// NegRed
if overrideStyle.NegRed {
style.NegRed = overrideStyle.NegRed
}
// NumFmt
if overrideStyle.NumFmt != 0 {
style.NumFmt = overrideStyle.NumFmt
}
// Protection
if overrideStyle.Protection != nil {
style.Protection = overrideStyle.Protection
}
return e.getStyleID(style)
}
func (e *excelizeam) getCacheKey(colIndex, rowIndex int) string {
return fmt.Sprintf("%d-%d", rowIndex, colIndex)
}
func (e *excelizeam) getCacheAddress(key string) (colIndex, rowIndex int) {
indexes := strings.Split(key, "-")
if len(indexes) == 2 {
var err error
if rowIndex, err = strconv.Atoi(indexes[0]); err != nil {
return 0, 0
}
if colIndex, err = strconv.Atoi(indexes[1]); err != nil {
return 0, 0
}
}
return colIndex, rowIndex
}
func (e *excelizeam) checkMaxIndex(colIndex, rowIndex int) {
e.mu.Lock()
defer e.mu.Unlock()
if e.maxCol < colIndex {
e.maxCol = colIndex
}
if e.maxRow < rowIndex {
e.maxRow = rowIndex
}
}
func (e *excelizeam) Wait() error {
return e.eg.Wait()
}
func (e *excelizeam) Write(w io.Writer) error {
if err := e.writeStream(); err != nil {
return err
}
if err := e.sw.Flush(); err != nil {
return err
}
if err := e.file.Write(w); err != nil {
return err
}
return nil
}
func (e *excelizeam) File() (*excelize.File, error) {
if err := e.writeStream(); err != nil {
return nil, err
}
if err := e.sw.Flush(); err != nil {
return nil, err
}
return e.file, nil
}
func (e *excelizeam) CSVRecords() ([][]string, error) {
if err := e.eg.Wait(); err != nil {
return nil, err
}
records := make([][]string, e.maxRow)
for i := 0; i < e.maxRow; i++ {
records[i] = make([]string, e.maxCol)
}
e.cellStore.Range(func(k, cached any) bool {
key := k.(string)
c := cached.(*Cell)
colIdx, rowIdx := e.getCacheAddress(key)
if c.Value != nil {
records[rowIdx-1][colIdx-1] = fmt.Sprintf("%v", c.Value)
}
return true
})
return records, nil
}
func (e *excelizeam) writeStream() error {
if err := e.eg.Wait(); err != nil {
return err
}
defaultStyleCells := make([]interface{}, e.maxCol)
if e.defaultBorder != nil {
for i := 0; i < e.maxCol; i++ {
defaultStyleCells[i] = excelize.Cell{StyleID: e.defaultBorder.StyleID, Value: ""}
}
}
type writeCols struct {
Cols []interface{}
CanWrite bool
}
writeRows := make([]writeCols, e.maxRow)
for i := 0; i < e.maxRow; i++ {
rowIdx := i + 1
writeRows[i] = writeCols{
Cols: make([]interface{}, e.maxCol),
}
if e.defaultBorder != nil {
copy(writeRows[i].Cols, defaultStyleCells)
writeRows[i].CanWrite = true
}
for ii := 0; ii < e.maxCol; ii++ {
colIdx := ii + 1
cached, ok := e.cellStore.Load(e.getCacheKey(colIdx, rowIdx))
if !ok {
continue
}
c := cached.(*Cell)
writeRows[i].Cols[ii] = excelize.Cell{StyleID: c.StyleID, Value: c.Value}
writeRows[i].CanWrite = true
}
}
for i, row := range writeRows {
if !row.CanWrite {
continue
}
cell, err := excelize.CoordinatesToCellName(1, i+1)
if err != nil {
return err
}
if err := e.sw.SetRow(
cell,
row.Cols,
); err != nil {
return err
}
}
return nil
}