-
Notifications
You must be signed in to change notification settings - Fork 819
/
xmlStyle.go
1140 lines (1022 loc) · 32.4 KB
/
xmlStyle.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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// xslx is a package designed to help with reading data from
// spreadsheets stored in the XLSX format used in recent versions of
// Microsoft's Excel spreadsheet.
//
// For a concise example of how to use this library why not check out
// the source for xlsx2csv here: https://github.com/tealeg/xlsx2csv
package xlsx
import (
"bytes"
"encoding/xml"
"fmt"
"strconv"
"sync"
)
var defaultTheme int = 1
// Excel styles can reference number formats that are built-in, all of which
// have an id less than 164.
const builtinNumFmtsCount = 163
// Excel styles can reference number formats that are built-in, all of which
// have an id less than 164. This is a possibly incomplete list comprised of as
// many of them as I could find.
var builtInNumFmt = map[int]string{
0: "general",
1: "0",
2: "0.00",
3: "#,##0",
4: "#,##0.00",
9: "0%",
10: "0.00%",
11: "0.00e+00",
12: "# ?/?",
13: "# ??/??",
14: "mm-dd-yy",
15: "d-mmm-yy",
16: "d-mmm",
17: "mmm-yy",
18: "h:mm am/pm",
19: "h:mm:ss am/pm",
20: "h:mm",
21: "h:mm:ss",
22: "m/d/yy h:mm",
37: "#,##0 ;(#,##0)",
38: "#,##0 ;[red](#,##0)",
39: "#,##0.00;(#,##0.00)",
40: "#,##0.00;[red](#,##0.00)",
41: `_(* #,##0_);_(* \(#,##0\);_(* "-"_);_(@_)`,
42: `_("$"* #,##0_);_("$* \(#,##0\);_("$"* "-"_);_(@_)`,
43: `_(* #,##0.00_);_(* \(#,##0.00\);_(* "-"??_);_(@_)`,
44: `_("$"* #,##0.00_);_("$"* \(#,##0.00\);_("$"* "-"??_);_(@_)`,
45: "mm:ss",
46: "[h]:mm:ss",
47: "mmss.0",
48: "##0.0e+0",
49: "@",
}
var builtInNumFmtInv = make(map[string]int, 40)
func init() {
for k, v := range builtInNumFmt {
builtInNumFmtInv[v] = k
}
}
const (
builtInNumFmtIndex_GENERAL = int(0)
builtInNumFmtIndex_INT = int(1)
builtInNumFmtIndex_FLOAT = int(2)
builtInNumFmtIndex_DATE = int(14)
builtInNumFmtIndex_STRING = int(49)
)
// xlsx Indexed Colors
// https://github.com/ClosedXML/ClosedXML/wiki/Excel-Indexed-Colors
var xlsxIndexedColors = []string{
"FF000000",
"FFFFFFFF",
"FFFF0000",
"FF00FF00",
"FF0000FF",
"FFFFFF00",
"FFFF00FF",
"FF00FFFF",
"FF000000",
"FFFFFFFF",
"FFFF0000",
"FF00FF00",
"FF0000FF",
"FFFFFF00",
"FFFF00FF",
"FF00FFFF",
"FF800000",
"FF008000",
"FF000080",
"FF808000",
"FF800080",
"FF008080",
"FFC0C0C0",
"FF808080",
"FF9999FF",
"FF993366",
"FFFFFFCC",
"FFCCFFFF",
"FF660066",
"FFFF8080",
"FF0066CC",
"FFCCCCFF",
"FF000080",
"FFFF00FF",
"FFFFFF00",
"FF00FFFF",
"FF800080",
"FF800000",
"FF008080",
"FF0000FF",
"FF00CCFF",
"FFCCFFFF",
"FFCCFFCC",
"FFFFFF99",
"FF99CCFF",
"FFFF99CC",
"FFCC99FF",
"FFFFCC99",
"FF3366FF",
"FF33CCCC",
"FF99CC00",
"FFFFCC00",
"FFFF9900",
"FFFF6600",
"FF666699",
"FF969696",
"FF003366",
"FF339966",
"FF003300",
"FF333300",
"FF993300",
"FF993366",
"FF333399",
"FF333333",
}
// xlsxStyle directly maps the styleSheet element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxStyleSheet struct {
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main styleSheet"`
Fonts xlsxFonts `xml:"fonts,omitempty"`
Fills xlsxFills `xml:"fills,omitempty"`
Borders xlsxBorders `xml:"borders,omitempty"`
Colors *xlsxColors `xml:"colors,omitempty"`
CellStyles *xlsxCellStyles `xml:"cellStyles,omitempty"`
CellStyleXfs *xlsxCellStyleXfs `xml:"cellStyleXfs,omitempty"`
CellXfs xlsxCellXfs `xml:"cellXfs,omitempty"`
NumFmts *xlsxNumFmts `xml:"numFmts,omitempty"`
DXfs xlsxDXFs `xml:"dxfs"`
theme *theme
styleCacheMU sync.RWMutex
styleCache map[int]*Style
numFmtRefTableMU sync.RWMutex
numFmtRefTable map[int]xlsxNumFmt
parsedNumFmtTableMU sync.RWMutex
parsedNumFmtTable map[string]*parsedNumberFormat
}
func newXlsxStyleSheet(t *theme) *xlsxStyleSheet {
return &xlsxStyleSheet{
theme: t,
styleCache: make(map[int]*Style),
}
}
func (styles *xlsxStyleSheet) reset() {
styles.Fonts = xlsxFonts{}
styles.Fills = xlsxFills{}
styles.Borders = xlsxBorders{}
// Microsoft seems to want Arial 11 defined by default.
styles.addFont(
xlsxFont{
Sz: xlsxVal{"11"},
Family: xlsxVal{"2"},
Color: xlsxColor{Theme: &defaultTheme},
Name: xlsxVal{"Arial"},
Scheme: &xlsxVal{"minor"},
},
)
styles.addFill(xlsxFill{PatternFill: xlsxPatternFill{PatternType: "none"}})
styles.addFill(xlsxFill{PatternFill: xlsxPatternFill{PatternType: "gray125"}})
// Microsoft seems to want an emtpy border to start with
styles.addBorder(
xlsxBorder{
Left: xlsxLine{},
Right: xlsxLine{},
Top: xlsxLine{},
Bottom: xlsxLine{},
})
// add 0th CellStyleXf by default, as required by the standard
styles.CellStyleXfs = &xlsxCellStyleXfs{Count: 1, Xf: []xlsxXf{{}}}
// add 0th CellXf by default, as required by the standard
styles.CellXfs = xlsxCellXfs{Count: 1, Xf: []xlsxXf{{}}}
styles.NumFmts = &xlsxNumFmts{}
styles.numFmtRefTableMU.Lock()
styles.numFmtRefTable = nil
styles.numFmtRefTableMU.Unlock()
}
func (styles *xlsxStyleSheet) populateStyleFromXf(style *Style, xf xlsxXf) {
style.ApplyBorder = xf.ApplyBorder
style.ApplyFill = xf.ApplyFill
style.ApplyFont = xf.ApplyFont
style.ApplyAlignment = xf.ApplyAlignment
if xf.BorderId > -1 && xf.BorderId < styles.Borders.Count {
border := styles.Borders.Border[xf.BorderId]
style.Border.Left = border.Left.Style
style.Border.LeftColor = styles.argbValue(border.Left.Color)
style.Border.Right = border.Right.Style
style.Border.RightColor = styles.argbValue(border.Right.Color)
style.Border.Top = border.Top.Style
style.Border.TopColor = styles.argbValue(border.Top.Color)
style.Border.Bottom = border.Bottom.Style
style.Border.BottomColor = styles.argbValue(border.Bottom.Color)
}
if xf.FillId > -1 && xf.FillId < styles.Fills.Count {
xFill := styles.Fills.Fill[xf.FillId]
style.Fill.PatternType = xFill.PatternFill.PatternType
style.Fill.FgColor = styles.argbValue(xFill.PatternFill.FgColor)
style.Fill.BgColor = styles.argbValue(xFill.PatternFill.BgColor)
}
if xf.FontId > -1 && xf.FontId < styles.Fonts.Count {
xfont := styles.Fonts.Font[xf.FontId]
style.Font.Size, _ = strconv.ParseFloat(xfont.Sz.Val, 64)
style.Font.Name = xfont.Name.Val
style.Font.Family, _ = strconv.Atoi(xfont.Family.Val)
style.Font.Charset, _ = strconv.Atoi(xfont.Charset.Val)
style.Font.Color = styles.argbValue(xfont.Color)
if bold := xfont.B; bold != nil && bold.Val != "0" {
style.Font.Bold = true
}
if italic := xfont.I; italic != nil && italic.Val != "0" {
style.Font.Italic = true
}
if underline := xfont.U; underline != nil && underline.Val != "0" {
style.Font.Underline = true
}
if strike := xfont.Strike; strike != nil && strike.Val != "0" {
style.Font.Strike = true
}
}
if xf.Alignment.Horizontal != "" {
style.Alignment.Horizontal = xf.Alignment.Horizontal
}
if xf.Alignment.Vertical != "" {
style.Alignment.Vertical = xf.Alignment.Vertical
}
style.Alignment.ShrinkToFit = xf.Alignment.ShrinkToFit
style.Alignment.WrapText = xf.Alignment.WrapText
style.Alignment.TextRotation = xf.Alignment.TextRotation
if xf.Alignment.Indent != 0 {
style.Alignment.Indent = xf.Alignment.Indent
}
}
func (styles *xlsxStyleSheet) getStyle(styleIndex int) *Style {
styles.styleCacheMU.RLock()
style, ok := styles.styleCache[styleIndex]
styles.styleCacheMU.RUnlock()
if ok {
return style
}
style = &Style{}
xfCount := styles.CellXfs.Count
if styleIndex > -1 && xfCount > 0 && styleIndex < xfCount {
xf := styles.CellXfs.Xf[styleIndex]
styles.populateStyleFromXf(style, xf)
if xf.XfId != nil && styles.CellStyleXfs != nil && *xf.XfId < len(styles.CellStyleXfs.Xf) {
style.NamedStyleIndex = xf.XfId
namedStyleXf := styles.CellStyleXfs.Xf[*xf.XfId]
style.ApplyBorder = style.ApplyBorder || namedStyleXf.ApplyBorder
style.ApplyFill = style.ApplyFill || namedStyleXf.ApplyFill
style.ApplyFont = style.ApplyFont || namedStyleXf.ApplyFont
style.ApplyAlignment = style.ApplyAlignment || namedStyleXf.ApplyAlignment
}
if xf.Alignment.Vertical != "" {
style.Alignment.Vertical = xf.Alignment.Vertical
}
style.Alignment.WrapText = xf.Alignment.WrapText
style.Alignment.TextRotation = xf.Alignment.TextRotation
styles.styleCacheMU.Lock()
styles.styleCache[styleIndex] = style
styles.styleCacheMU.Unlock()
}
return style
}
func (styles *xlsxStyleSheet) argbValue(color xlsxColor) string {
if color.Theme != nil && styles.theme != nil {
return styles.theme.themeColor(int64(*color.Theme), color.Tint)
}
if color.Indexed != nil && styles.Colors != nil {
return styles.Colors.indexedColor(*color.Indexed)
}
return color.RGB
}
// Excel styles can reference number formats that are built-in, all of which
// have an id less than 164. This is a possibly incomplete list comprised of as
// many of them as I could find.
func getBuiltinNumberFormat(numFmtId int) string {
nmfmt, ok := builtInNumFmt[numFmtId]
if !ok {
return ""
}
return nmfmt
}
func (styles *xlsxStyleSheet) getNumberFormat(styleIndex int) (string, *parsedNumberFormat) {
var numberFormat string = "general"
if styles.CellXfs.Xf != nil {
if styleIndex > -1 && styleIndex < styles.CellXfs.Count {
xf := styles.CellXfs.Xf[styleIndex]
if builtin := getBuiltinNumberFormat(xf.NumFmtId); builtin != "" {
numberFormat = builtin
} else {
styles.numFmtRefTableMU.RLock()
if styles.numFmtRefTable != nil {
numFmt := styles.numFmtRefTable[xf.NumFmtId]
numberFormat = numFmt.FormatCode
}
styles.numFmtRefTableMU.RUnlock()
}
}
}
styles.parsedNumFmtTableMU.RLock()
parsedFmt, ok := styles.parsedNumFmtTable[numberFormat]
styles.parsedNumFmtTableMU.RUnlock()
if !ok {
styles.parsedNumFmtTableMU.Lock()
if styles.parsedNumFmtTable == nil {
styles.parsedNumFmtTable = map[string]*parsedNumberFormat{}
}
parsedFmt = parseFullNumberFormatString(numberFormat)
styles.parsedNumFmtTable[numberFormat] = parsedFmt
styles.parsedNumFmtTableMU.Unlock()
}
return numberFormat, parsedFmt
}
func (styles *xlsxStyleSheet) addFont(xFont xlsxFont) (index int) {
var font xlsxFont
if xFont.Name.Val == "" {
return 0
}
for index, font = range styles.Fonts.Font {
if font.Equals(xFont) {
return index
}
}
styles.Fonts.Font = append(styles.Fonts.Font, xFont)
index = styles.Fonts.Count
styles.Fonts.Count++
return
}
func (styles *xlsxStyleSheet) addFill(xFill xlsxFill) (index int) {
var fill xlsxFill
for index, fill = range styles.Fills.Fill {
if fill.Equals(xFill) {
return index
}
}
styles.Fills.Fill = append(styles.Fills.Fill, xFill)
index = styles.Fills.Count
styles.Fills.Count++
return
}
func (styles *xlsxStyleSheet) addBorder(xBorder xlsxBorder) (index int) {
var border xlsxBorder
for index, border = range styles.Borders.Border {
if border.Equals(xBorder) {
return index
}
}
styles.Borders.Border = append(styles.Borders.Border, xBorder)
index = styles.Borders.Count
styles.Borders.Count++
return
}
func (styles *xlsxStyleSheet) addCellXf(xCellXf xlsxXf) (index int) {
var cellXf xlsxXf
for index, cellXf = range styles.CellXfs.Xf {
if cellXf.Equals(xCellXf) {
return index
}
}
styles.CellXfs.Xf = append(styles.CellXfs.Xf, xCellXf)
index = styles.CellXfs.Count
styles.CellXfs.Count++
return
}
// newNumFmt generate a xlsxNumFmt according the format code. When the FormatCode is built in, it will return a xlsxNumFmt with the NumFmtId defined in ECMA document, otherwise it will generate a new NumFmtId greater than 164.
func (styles *xlsxStyleSheet) newNumFmt(formatCode string) xlsxNumFmt {
if compareFormatString(formatCode, "general") {
return xlsxNumFmt{NumFmtId: 0, FormatCode: "general"}
}
// built in NumFmts in xmlStyle.go, traverse from the const.
numFmtId, ok := builtInNumFmtInv[formatCode]
if ok {
return xlsxNumFmt{NumFmtId: numFmtId, FormatCode: formatCode}
}
// find the exist xlsxNumFmt
if styles.NumFmts != nil {
for _, numFmt := range styles.NumFmts.NumFmt {
if formatCode == numFmt.FormatCode {
return numFmt
}
}
}
// The user define NumFmtId. The one less than 164 in built in.
numFmtId = builtinNumFmtsCount + 1
for {
// get a unused NumFmtId
styles.numFmtRefTableMU.RLock()
_, ok := styles.numFmtRefTable[numFmtId]
styles.numFmtRefTableMU.RUnlock()
if ok {
numFmtId++
} else {
// addNumFmt contains locking code, so we don't lock around it.
styles.addNumFmt(xlsxNumFmt{NumFmtId: numFmtId, FormatCode: formatCode})
break
}
}
return xlsxNumFmt{NumFmtId: numFmtId, FormatCode: formatCode}
}
// addNumFmt add xlsxNumFmt if its not exist.
func (styles *xlsxStyleSheet) addNumFmt(xNumFmt xlsxNumFmt) {
// don't add built in NumFmt
if _, ok := builtInNumFmt[xNumFmt.NumFmtId]; ok {
return
}
styles.numFmtRefTableMU.RLock()
_, ok := styles.numFmtRefTable[xNumFmt.NumFmtId]
styles.numFmtRefTableMU.RUnlock()
if !ok {
if styles.numFmtRefTable == nil {
styles.numFmtRefTableMU.Lock()
styles.numFmtRefTable = make(map[int]xlsxNumFmt)
styles.numFmtRefTableMU.Unlock()
}
if styles.NumFmts == nil {
styles.NumFmts = &xlsxNumFmts{}
}
styles.NumFmts.NumFmt = append(styles.NumFmts.NumFmt, xNumFmt)
styles.numFmtRefTableMU.Lock()
styles.numFmtRefTable[xNumFmt.NumFmtId] = xNumFmt
styles.numFmtRefTableMU.Unlock()
styles.NumFmts.Count++
}
}
func (styles *xlsxStyleSheet) Marshal() (string, error) {
result := xml.Header + `<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">`
if styles.NumFmts != nil {
xNumFmts, err := styles.NumFmts.Marshal()
if err != nil {
return "", err
}
result += xNumFmts
}
outputFontMap := make(map[int]int)
xfonts, err := styles.Fonts.Marshal(outputFontMap)
if err != nil {
return "", err
}
result += xfonts
outputFillMap := make(map[int]int)
xfills, err := styles.Fills.Marshal(outputFillMap)
if err != nil {
return "", err
}
result += xfills
outputBorderMap := make(map[int]int)
xborders, err := styles.Borders.Marshal(outputBorderMap)
if err != nil {
return "", err
}
result += xborders
if styles.CellStyleXfs != nil {
xcellStyleXfs, err := styles.CellStyleXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
if err != nil {
return "", err
}
result += xcellStyleXfs
}
xcellXfs, err := styles.CellXfs.Marshal(outputBorderMap, outputFillMap, outputFontMap)
if err != nil {
return "", err
}
result += xcellXfs
if styles.CellStyles != nil && styles.CellStyleXfs != nil {
xcellStyles, err := styles.CellStyles.Marshal(styles.CellStyleXfs.Count - 1)
if err != nil {
return "", err
}
result += xcellStyles
}
return result + "</styleSheet>", nil
}
type xlsxDXFs struct {
Count int `xml:"count,attr"`
}
// xlsxNumFmts directly maps the numFmts element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxNumFmts struct {
Count int `xml:"count,attr"`
NumFmt []xlsxNumFmt `xml:"numFmt,omitempty"`
}
func (numFmts *xlsxNumFmts) Marshal() (result string, err error) {
if numFmts.Count > 0 {
result = fmt.Sprintf(`<numFmts count="%d">`, numFmts.Count)
for _, numFmt := range numFmts.NumFmt {
var xNumFmt string
xNumFmt, err = numFmt.Marshal()
if err != nil {
return
}
result += xNumFmt
}
result += `</numFmts>`
}
return
}
// xlsxNumFmt directly maps the numFmt element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxNumFmt struct {
NumFmtId int `xml:"numFmtId,attr,omitempty"`
FormatCode string `xml:"formatCode,attr,omitempty"`
}
func (numFmt *xlsxNumFmt) Marshal() (result string, err error) {
formatCode := &bytes.Buffer{}
if err := xml.EscapeText(formatCode, []byte(numFmt.FormatCode)); err != nil {
return "", err
}
return fmt.Sprintf(`<numFmt numFmtId="%d" formatCode="%s"/>`, numFmt.NumFmtId, formatCode), nil
}
// xlsxFonts directly maps the fonts element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxFonts struct {
XMLName xml.Name `xml:"fonts"`
Count int `xml:"count,attr"`
Font []xlsxFont `xml:"font,omitempty"`
}
func (fonts *xlsxFonts) addFont(font xlsxFont) {
fonts.Font = append(fonts.Font, font)
fonts.Count++
}
func (fonts *xlsxFonts) Marshal(outputFontMap map[int]int) (result string, err error) {
emittedCount := 0
subparts := ""
for i, font := range fonts.Font {
var xfont string
xfont, err = font.Marshal()
if err != nil {
return
}
if xfont != "" {
outputFontMap[i] = emittedCount
emittedCount++
subparts += xfont
}
}
if emittedCount > 0 {
result = fmt.Sprintf(`<fonts count="%d">`, fonts.Count)
result += subparts
result += `</fonts>`
}
return
}
// xlsxFont directly maps the font element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxFont struct {
Sz xlsxVal `xml:"sz,omitempty"`
Name xlsxVal `xml:"name,omitempty"`
Family xlsxVal `xml:"family,omitempty"`
Charset xlsxVal `xml:"charset,omitempty"`
Color xlsxColor `xml:"color,omitempty"`
B *xlsxVal `xml:"b,omitempty"`
I *xlsxVal `xml:"i,omitempty"`
U *xlsxVal `xml:"u,omitempty"`
Scheme *xlsxVal `xml:"scheme,omitempty"`
Strike *xlsxVal `xml:"strike,omitempty"`
}
func (font *xlsxFont) Equals(other xlsxFont) bool {
if (font.B == nil && other.B != nil) || (font.B != nil && other.B == nil) {
return false
}
if (font.I == nil && other.I != nil) || (font.I != nil && other.I == nil) {
return false
}
if (font.U == nil && other.U != nil) || (font.U != nil && other.U == nil) {
return false
}
return font.Sz.Equals(other.Sz) && font.Name.Equals(other.Name) && font.Family.Equals(other.Family) && font.Charset.Equals(other.Charset) && font.Color.Equals(other.Color)
}
func (font *xlsxFont) Marshal() (result string, err error) {
result = "<font>"
if font.Sz.Val != "" {
result += fmt.Sprintf(`<sz val="%s"/>`, font.Sz.Val)
}
if font.Name.Val != "" {
result += fmt.Sprintf(`<name val="%s"/>`, font.Name.Val)
}
if font.Family.Val != "" {
result += fmt.Sprintf(`<family val="%s"/>`, font.Family.Val)
}
if font.Charset.Val != "" {
result += fmt.Sprintf(`<charset val="%s"/>`, font.Charset.Val)
}
if font.Color.RGB != "" {
result += fmt.Sprintf(`<color rgb="%s"/>`, font.Color.RGB)
}
if font.Color.Theme != nil {
result += fmt.Sprintf(`<color theme="%d" />`, *font.Color.Theme)
}
if font.Scheme != nil && font.Scheme.Val != "" {
result += fmt.Sprintf(`<scheme val="%s"/>`, font.Scheme.Val)
}
if font.B != nil {
result += "<b/>"
}
if font.I != nil {
result += "<i/>"
}
if font.U != nil {
result += "<u/>"
}
if font.Strike != nil {
result += "<strike/>"
}
return result + "</font>", nil
}
// xlsxVal directly maps the val element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxVal struct {
Val string `xml:"val,attr,omitempty"`
}
func (val *xlsxVal) Equals(other xlsxVal) bool {
return val.Val == other.Val
}
// xlsxFills directly maps the fills element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxFills struct {
Count int `xml:"count,attr"`
Fill []xlsxFill `xml:"fill,omitempty"`
}
func (fills *xlsxFills) addFill(fill xlsxFill) {
fills.Fill = append(fills.Fill, fill)
fills.Count++
}
func (fills *xlsxFills) Marshal(outputFillMap map[int]int) (string, error) {
var subparts string
var emittedCount int
for i, fill := range fills.Fill {
xfill, err := fill.Marshal()
if err != nil {
return "", err
}
if xfill != "" {
outputFillMap[i] = emittedCount
emittedCount++
subparts += xfill
}
}
var result string
if emittedCount > 0 {
result = fmt.Sprintf(`<fills count="%d">`, emittedCount)
result += subparts
result += `</fills>`
}
return result, nil
}
// xlsxFill directly maps the fill element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxFill struct {
PatternFill xlsxPatternFill `xml:"patternFill,omitempty"`
}
func (fill *xlsxFill) Equals(other xlsxFill) bool {
return fill.PatternFill.Equals(other.PatternFill)
}
func (fill *xlsxFill) Marshal() (result string, err error) {
if fill.PatternFill.PatternType != "" {
var xpatternFill string
result = `<fill>`
xpatternFill, err = fill.PatternFill.Marshal()
if err != nil {
return
}
result += xpatternFill
result += `</fill>`
}
return
}
// xlsxPatternFill directly maps the patternFill element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxPatternFill struct {
PatternType string `xml:"patternType,attr,omitempty"`
FgColor xlsxColor `xml:"fgColor,omitempty"`
BgColor xlsxColor `xml:"bgColor,omitempty"`
}
func (patternFill *xlsxPatternFill) Equals(other xlsxPatternFill) bool {
return patternFill.PatternType == other.PatternType && patternFill.FgColor.Equals(other.FgColor) && patternFill.BgColor.Equals(other.BgColor)
}
func (patternFill *xlsxPatternFill) Marshal() (result string, err error) {
result = fmt.Sprintf(`<patternFill patternType="%s"`, patternFill.PatternType)
ending := `/>`
terminator := ""
subparts := ""
if patternFill.FgColor.RGB != "" {
ending = `>`
terminator = "</patternFill>"
subparts += fmt.Sprintf(`<fgColor rgb="%s"/>`, patternFill.FgColor.RGB)
}
if patternFill.BgColor.RGB != "" {
ending = `>`
terminator = "</patternFill>"
subparts += fmt.Sprintf(`<bgColor rgb="%s"/>`, patternFill.BgColor.RGB)
}
result += ending
result += subparts
result += terminator
return
}
// xlsxColor is a common mapping used for both the fgColor and bgColor
// elements in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxColor struct {
RGB string `xml:"rgb,attr,omitempty"`
Theme *int `xml:"theme,attr,omitempty"`
Tint float64 `xml:"tint,attr,omitempty"`
Indexed *int `xml:"indexed,attr,omitempty"`
}
func (color *xlsxColor) Equals(other xlsxColor) bool {
return color.RGB == other.RGB
}
// xlsxBorders directly maps the borders element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxBorders struct {
Count int `xml:"count,attr"`
Border []xlsxBorder `xml:"border"`
}
func (borders *xlsxBorders) addBorder(border xlsxBorder) {
borders.Border = append(borders.Border, border)
borders.Count++
}
func (borders *xlsxBorders) Marshal(outputBorderMap map[int]int) (result string, err error) {
result = ""
emittedCount := 0
subparts := ""
for i, border := range borders.Border {
var xborder string
xborder, err = border.Marshal()
if err != nil {
return
}
if xborder != "" {
outputBorderMap[i] = emittedCount
emittedCount++
subparts += xborder
}
}
if emittedCount > 0 {
result += fmt.Sprintf(`<borders count="%d">`, emittedCount)
result += subparts
result += `</borders>`
}
return
}
// xlsxBorder directly maps the border element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxBorder struct {
Left xlsxLine `xml:"left,omitempty"`
Right xlsxLine `xml:"right,omitempty"`
Top xlsxLine `xml:"top,omitempty"`
Bottom xlsxLine `xml:"bottom,omitempty"`
}
func (border *xlsxBorder) Equals(other xlsxBorder) bool {
return border.Left.Equals(other.Left) && border.Right.Equals(other.Right) && border.Top.Equals(other.Top) && border.Bottom.Equals(other.Bottom)
}
func (border *xlsxBorder) marshalBorderLine(line xlsxLine, name string) string {
if line.Style == "" {
return fmt.Sprintf("<%s/>", name)
}
subparts := ""
subparts += fmt.Sprintf(`<%s style="%s">`, name, line.Style)
if line.Color.RGB != "" {
subparts += fmt.Sprintf(`<color rgb="%s"/>`, line.Color.RGB)
}
subparts += fmt.Sprintf(`</%s>`, name)
return subparts
}
// To get borders to work correctly in Excel, you have to always start with an
// empty set of borders. There was logic in this function that would strip out
// empty elements, but unfortunately that would cause the border to fail.
func (border *xlsxBorder) Marshal() (result string, err error) {
subparts := border.marshalBorderLine(border.Left, "left")
subparts += border.marshalBorderLine(border.Right, "right")
subparts += border.marshalBorderLine(border.Top, "top")
subparts += border.marshalBorderLine(border.Bottom, "bottom")
result += `<border>`
result += subparts
result += `</border>`
return
}
// xlsxLine directly maps the line style element in the namespace
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
// currently I have not checked it for completeness - it does as much
// as I need.
type xlsxLine struct {
Style string `xml:"style,attr,omitempty"`
Color xlsxColor `xml:"color,omitempty"`
}
func (line *xlsxLine) Equals(other xlsxLine) bool {
return line.Style == other.Style && line.Color.Equals(other.Color)
}
type xlsxCellStyles struct {
XMLName xml.Name `xml:"cellStyles"`
Count int `xml:"count,attr"`
CellStyle []xlsxCellStyle `xml:"cellStyle,omitempty"`
}
func (cellStyles *xlsxCellStyles) Marshal(maxXfId int) (result string, err error) {
stylesCount := 0
for _, cellStyle := range cellStyles.CellStyle {
if cellStyle.XfId <= maxXfId {
stylesCount++
}
}
if stylesCount > 0 {
result = fmt.Sprintf(`<cellStyles count="%d">`, stylesCount)
for _, cellStyle := range cellStyles.CellStyle {
if cellStyle.XfId > maxXfId {
continue
}
var xCellStyle []byte
xCellStyle, err = xml.Marshal(cellStyle)
if err != nil {
return
}
result += string(xCellStyle)
}
result += `</cellStyles>`
}
return
}
type xlsxCellStyle struct {
XMLName xml.Name `xml:"cellStyle"`
BuiltInId *int `xml:"builtInId,attr,omitempty"`
CustomBuiltIn *bool `xml:"customBuiltIn,attr,omitempty"`
Hidden *bool `xml:"hidden,attr,omitempty"`
ILevel *bool `xml:"iLevel,attr,omitempty"`
Name string `xml:"name,attr"`
XfId int `xml:"xfId,attr"`
}
// xlsxCellStyleXfs directly maps the cellStyleXfs element in the
// namespace http://schemas.openxmlformats.org/spreadsheetml/2006/main
// - currently I have not checked it for completeness - it does as
// much as I need.
type xlsxCellStyleXfs struct {
Count int `xml:"count,attr"`
Xf []xlsxXf `xml:"xf,omitempty"`
}
func (cellStyleXfs *xlsxCellStyleXfs) addXf(Xf xlsxXf) {
cellStyleXfs.Xf = append(cellStyleXfs.Xf, Xf)
cellStyleXfs.Count++
}
func (cellStyleXfs *xlsxCellStyleXfs) Marshal(outputBorderMap, outputFillMap, outputFontMap map[int]int) (result string, err error) {
if cellStyleXfs.Count > 0 {
result = fmt.Sprintf(`<cellStyleXfs count="%d">`, cellStyleXfs.Count)
for _, xf := range cellStyleXfs.Xf {
var xxf string
xxf, err = xf.Marshal(outputBorderMap, outputFillMap, outputFontMap)
if err != nil {
return
}
result += xxf
}
result += `</cellStyleXfs>`
}
return
}