forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
style.go
203 lines (181 loc) · 4.44 KB
/
style.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
package xlsx
import "strconv"
// Several popular font names that can be used to create fonts
const (
Helvetica = "Helvetica"
Baskerville = "Baskerville Old Face"
TimesNewRoman = "Times New Roman"
Bodoni = "Bodoni MT"
GillSans = "Gill Sans MT"
Courier = "Courier"
)
const (
RGB_Light_Green = "FFC6EFCE"
RGB_Dark_Green = "FF006100"
RGB_Light_Red = "FFFFC7CE"
RGB_Dark_Red = "FF9C0006"
RGB_White = "00000000"
RGB_Black = "FFFFFFFF"
)
const (
Solid_Cell_Fill = "solid"
)
// Style is a high level structure intended to provide user access to
// the contents of Style within an XLSX file.
type Style struct {
Border Border
Fill Fill
Font Font
ApplyBorder bool
ApplyFill bool
ApplyFont bool
ApplyAlignment bool
Alignment Alignment
NamedStyleIndex *int
}
// Return a new Style structure initialised with the default values.
func NewStyle() *Style {
return &Style{
Alignment: *DefaultAlignment(),
Border: *DefaultBorder(),
Fill: *DefaultFill(),
Font: *DefaultFont(),
}
}
// Generate the underlying XLSX style elements that correspond to the Style.
func (style *Style) makeXLSXStyleElements() (xFont xlsxFont, xFill xlsxFill, xBorder xlsxBorder, xCellXf xlsxXf) {
xFont = xlsxFont{}
xFill = xlsxFill{}
xBorder = xlsxBorder{}
xCellXf = xlsxXf{}
xFont.Sz.Val = strconv.Itoa(style.Font.Size)
xFont.Name.Val = style.Font.Name
xFont.Family.Val = strconv.Itoa(style.Font.Family)
xFont.Charset.Val = strconv.Itoa(style.Font.Charset)
xFont.Color.RGB = style.Font.Color
if style.Font.Bold {
xFont.B = &xlsxVal{}
} else {
xFont.B = nil
}
if style.Font.Italic {
xFont.I = &xlsxVal{}
} else {
xFont.I = nil
}
if style.Font.Underline {
xFont.U = &xlsxVal{}
} else {
xFont.U = nil
}
xPatternFill := xlsxPatternFill{}
xPatternFill.PatternType = style.Fill.PatternType
xPatternFill.FgColor.RGB = style.Fill.FgColor
xPatternFill.BgColor.RGB = style.Fill.BgColor
xFill.PatternFill = xPatternFill
xBorder.Left = xlsxLine{
Style: style.Border.Left,
Color: xlsxColor{RGB: style.Border.LeftColor},
}
xBorder.Right = xlsxLine{
Style: style.Border.Right,
Color: xlsxColor{RGB: style.Border.RightColor},
}
xBorder.Top = xlsxLine{
Style: style.Border.Top,
Color: xlsxColor{RGB: style.Border.TopColor},
}
xBorder.Bottom = xlsxLine{
Style: style.Border.Bottom,
Color: xlsxColor{RGB: style.Border.BottomColor},
}
xCellXf = makeXLSXCellElement()
xCellXf.ApplyBorder = style.ApplyBorder
xCellXf.ApplyFill = style.ApplyFill
xCellXf.ApplyFont = style.ApplyFont
xCellXf.ApplyAlignment = style.ApplyAlignment
if style.NamedStyleIndex != nil {
xCellXf.XfId = style.NamedStyleIndex
}
return
}
func makeXLSXCellElement() (xCellXf xlsxXf) {
xCellXf.NumFmtId = 0
return
}
// Border is a high level structure intended to provide user access to
// the contents of Border Style within an Sheet.
type Border struct {
Left string
LeftColor string
Right string
RightColor string
Top string
TopColor string
Bottom string
BottomColor string
}
func NewBorder(left, right, top, bottom string) *Border {
return &Border{
Left: left,
Right: right,
Top: top,
Bottom: bottom,
}
}
// Fill is a high level structure intended to provide user access to
// the contents of background and foreground color index within an Sheet.
type Fill struct {
PatternType string
BgColor string
FgColor string
}
func NewFill(patternType, fgColor, bgColor string) *Fill {
return &Fill{
PatternType: patternType,
FgColor: fgColor,
BgColor: bgColor,
}
}
type Font struct {
Size int
Name string
Family int
Charset int
Color string
Bold bool
Italic bool
Underline bool
}
func NewFont(size int, name string) *Font {
return &Font{Size: size, Name: name}
}
type Alignment struct {
Horizontal string
Indent int
ShrinkToFit bool
TextRotation int
Vertical string
WrapText bool
}
var defaultFontSize = 12
var defaultFontName = "Verdana"
func SetDefaultFont(size int, name string) {
defaultFontSize = size
defaultFontName = name
}
func DefaultFont() *Font {
return NewFont(defaultFontSize, defaultFontName)
}
func DefaultFill() *Fill {
return NewFill("none", "", "")
}
func DefaultBorder() *Border {
return NewBorder("none", "none", "none", "none")
}
func DefaultAlignment() *Alignment {
return &Alignment{
Horizontal: "general",
Vertical: "bottom",
}
}