Skip to content

Commit 02b84a9

Browse files
authored
This closes #1820, converted styleFillVariants from slice to func (#1821)
1 parent ee2ef15 commit 02b84a9

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

styles.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,25 +1071,28 @@ var (
10711071
"gray0625",
10721072
}
10731073
// styleFillVariants list all preset variants of the fill style.
1074-
styleFillVariants = []xlsxGradientFill{
1075-
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1076-
{Degree: 270, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1077-
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
1078-
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1079-
{Degree: 180, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1080-
{Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
1081-
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1082-
{Degree: 255, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1083-
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
1084-
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1085-
{Degree: 315, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1086-
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
1087-
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path"},
1088-
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Left: 1, Right: 1},
1089-
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Top: 1},
1090-
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Left: 1, Right: 1, Top: 1},
1091-
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 0.5, Left: 0.5, Right: 0.5, Top: 0.5},
1074+
styleFillVariants = func() []xlsxGradientFill {
1075+
return []xlsxGradientFill{
1076+
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1077+
{Degree: 270, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1078+
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
1079+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1080+
{Degree: 180, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1081+
{Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
1082+
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1083+
{Degree: 255, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1084+
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
1085+
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1086+
{Degree: 315, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
1087+
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
1088+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path"},
1089+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Left: 1, Right: 1},
1090+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Top: 1},
1091+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Left: 1, Right: 1, Top: 1},
1092+
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 0.5, Left: 0.5, Right: 0.5, Top: 0.5},
1093+
}
10921094
}
1095+
10931096
// getXfIDFuncs provides a function to get xfID by given style.
10941097
getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{
10951098
"numFmt": func(numFmtID int, xf xlsxXf, style *Style) bool {
@@ -1132,6 +1135,7 @@ var (
11321135
return reflect.DeepEqual(xf.Protection, newProtection(style)) && xf.ApplyProtection != nil && *xf.ApplyProtection
11331136
},
11341137
}
1138+
11351139
// extractStyleCondFuncs provides a function set to returns if shoudle be
11361140
// extract style definition by given style.
11371141
extractStyleCondFuncs = map[string]func(xlsxXf, *xlsxStyleSheet) bool{
@@ -1157,6 +1161,7 @@ var (
11571161
return xf.ApplyProtection == nil || (xf.ApplyProtection != nil && *xf.ApplyProtection)
11581162
},
11591163
}
1164+
11601165
// drawContFmtFunc defines functions to create conditional formats.
11611166
drawContFmtFunc = map[string]func(p int, ct, ref, GUID string, fmtCond *ConditionalFormatOptions) (*xlsxCfRule, *xlsxX14CfRule){
11621167
"cellIs": drawCondFmtCellIs,
@@ -1176,6 +1181,7 @@ var (
11761181
"expression": drawCondFmtExp,
11771182
"iconSet": drawCondFmtIconSet,
11781183
}
1184+
11791185
// extractContFmtFunc defines functions to get conditional formats.
11801186
extractContFmtFunc = map[string]func(*File, *xlsxCfRule, *xlsxExtLst) ConditionalFormatOptions{
11811187
"cellIs": func(f *File, c *xlsxCfRule, extLst *xlsxExtLst) ConditionalFormatOptions {
@@ -1233,6 +1239,7 @@ var (
12331239
return f.extractCondFmtIconSet(c, extLst)
12341240
},
12351241
}
1242+
12361243
// validType defined the list of valid validation types.
12371244
validType = map[string]string{
12381245
"cell": "cellIs",
@@ -1456,7 +1463,7 @@ func (f *File) extractFills(fl *xlsxFill, s *xlsxStyleSheet, style *Style) {
14561463
var fill Fill
14571464
if fl.GradientFill != nil {
14581465
fill.Type = "gradient"
1459-
for shading, variants := range styleFillVariants {
1466+
for shading, variants := range styleFillVariants() {
14601467
if fl.GradientFill.Bottom == variants.Bottom &&
14611468
fl.GradientFill.Degree == variants.Degree &&
14621469
fl.GradientFill.Left == variants.Left &&
@@ -2024,7 +2031,7 @@ func newFills(style *Style, fg bool) *xlsxFill {
20242031
if len(style.Fill.Color) != 2 || style.Fill.Shading < 0 || style.Fill.Shading > 16 {
20252032
break
20262033
}
2027-
gradient := styleFillVariants[style.Fill.Shading]
2034+
gradient := styleFillVariants()[style.Fill.Shading]
20282035
gradient.Stop[0].Color.RGB = getPaletteColor(style.Fill.Color[0])
20292036
gradient.Stop[1].Color.RGB = getPaletteColor(style.Fill.Color[1])
20302037
if len(gradient.Stop) == 3 {

styles_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,16 @@ func TestNewStyle(t *testing.T) {
341341
_, err = f.NewStyle(nil)
342342
assert.NoError(t, err)
343343

344+
// Test gradient fills
345+
f = NewFile()
346+
styleID1, err := f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"FFFFFF", "4E71BE"}, Shading: 1, Pattern: 1}})
347+
assert.NoError(t, err)
348+
styleID2, err := f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"FF0000", "4E71BE"}, Shading: 1, Pattern: 1}})
349+
assert.NoError(t, err)
350+
assert.NotEqual(t, styleID1, styleID2)
351+
344352
var exp string
353+
f = NewFile()
345354
_, err = f.NewStyle(&Style{CustomNumFmt: &exp})
346355
assert.Equal(t, ErrCustomNumFmt, err)
347356
_, err = f.NewStyle(&Style{Font: &Font{Family: strings.Repeat("s", MaxFontFamilyLength+1)}})
@@ -356,7 +365,7 @@ func TestNewStyle(t *testing.T) {
356365
CustomNumFmt: &numFmt,
357366
})
358367
assert.NoError(t, err)
359-
assert.Equal(t, 2, styleID)
368+
assert.Equal(t, 1, styleID)
360369

361370
assert.NotNil(t, f.Styles)
362371
assert.NotNil(t, f.Styles.CellXfs)
@@ -371,7 +380,7 @@ func TestNewStyle(t *testing.T) {
371380
NumFmt: 32, // must not be in currencyNumFmt
372381
})
373382
assert.NoError(t, err)
374-
assert.Equal(t, 3, styleID)
383+
assert.Equal(t, 2, styleID)
375384

376385
assert.NotNil(t, f.Styles)
377386
assert.NotNil(t, f.Styles.CellXfs)

0 commit comments

Comments
 (0)