This closes #1820, converted styleFillVariants from slice to func (#1821)

This commit is contained in:
Ed 2024-02-23 19:11:31 -06:00 committed by GitHub
parent ee2ef152d9
commit 02b84a906c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 22 deletions

View File

@ -1071,25 +1071,28 @@ var (
"gray0625", "gray0625",
} }
// styleFillVariants list all preset variants of the fill style. // styleFillVariants list all preset variants of the fill style.
styleFillVariants = []xlsxGradientFill{ styleFillVariants = func() []xlsxGradientFill {
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, return []xlsxGradientFill{
{Degree: 270, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, {Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}}, {Degree: 270, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, {Degree: 90, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Degree: 180, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, {Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}}, {Degree: 180, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, {Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Degree: 255, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, {Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}}, {Degree: 255, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, {Degree: 45, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Degree: 315, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}}, {Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}}, {Degree: 315, Stop: []*xlsxGradientFillStop{{}, {Position: 1}}},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path"}, {Degree: 135, Stop: []*xlsxGradientFillStop{{}, {Position: 0.5}, {Position: 1}}},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Left: 1, Right: 1}, {Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path"},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Top: 1}, {Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Left: 1, Right: 1},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Left: 1, Right: 1, Top: 1}, {Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Top: 1},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 0.5, Left: 0.5, Right: 0.5, Top: 0.5}, {Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 1, Left: 1, Right: 1, Top: 1},
{Stop: []*xlsxGradientFillStop{{}, {Position: 1}}, Type: "path", Bottom: 0.5, Left: 0.5, Right: 0.5, Top: 0.5},
}
} }
// getXfIDFuncs provides a function to get xfID by given style. // getXfIDFuncs provides a function to get xfID by given style.
getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{ getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{
"numFmt": func(numFmtID int, xf xlsxXf, style *Style) bool { "numFmt": func(numFmtID int, xf xlsxXf, style *Style) bool {
@ -1132,6 +1135,7 @@ var (
return reflect.DeepEqual(xf.Protection, newProtection(style)) && xf.ApplyProtection != nil && *xf.ApplyProtection return reflect.DeepEqual(xf.Protection, newProtection(style)) && xf.ApplyProtection != nil && *xf.ApplyProtection
}, },
} }
// extractStyleCondFuncs provides a function set to returns if shoudle be // extractStyleCondFuncs provides a function set to returns if shoudle be
// extract style definition by given style. // extract style definition by given style.
extractStyleCondFuncs = map[string]func(xlsxXf, *xlsxStyleSheet) bool{ extractStyleCondFuncs = map[string]func(xlsxXf, *xlsxStyleSheet) bool{
@ -1157,6 +1161,7 @@ var (
return xf.ApplyProtection == nil || (xf.ApplyProtection != nil && *xf.ApplyProtection) return xf.ApplyProtection == nil || (xf.ApplyProtection != nil && *xf.ApplyProtection)
}, },
} }
// drawContFmtFunc defines functions to create conditional formats. // drawContFmtFunc defines functions to create conditional formats.
drawContFmtFunc = map[string]func(p int, ct, ref, GUID string, fmtCond *ConditionalFormatOptions) (*xlsxCfRule, *xlsxX14CfRule){ drawContFmtFunc = map[string]func(p int, ct, ref, GUID string, fmtCond *ConditionalFormatOptions) (*xlsxCfRule, *xlsxX14CfRule){
"cellIs": drawCondFmtCellIs, "cellIs": drawCondFmtCellIs,
@ -1176,6 +1181,7 @@ var (
"expression": drawCondFmtExp, "expression": drawCondFmtExp,
"iconSet": drawCondFmtIconSet, "iconSet": drawCondFmtIconSet,
} }
// extractContFmtFunc defines functions to get conditional formats. // extractContFmtFunc defines functions to get conditional formats.
extractContFmtFunc = map[string]func(*File, *xlsxCfRule, *xlsxExtLst) ConditionalFormatOptions{ extractContFmtFunc = map[string]func(*File, *xlsxCfRule, *xlsxExtLst) ConditionalFormatOptions{
"cellIs": func(f *File, c *xlsxCfRule, extLst *xlsxExtLst) ConditionalFormatOptions { "cellIs": func(f *File, c *xlsxCfRule, extLst *xlsxExtLst) ConditionalFormatOptions {
@ -1233,6 +1239,7 @@ var (
return f.extractCondFmtIconSet(c, extLst) return f.extractCondFmtIconSet(c, extLst)
}, },
} }
// validType defined the list of valid validation types. // validType defined the list of valid validation types.
validType = map[string]string{ validType = map[string]string{
"cell": "cellIs", "cell": "cellIs",
@ -1456,7 +1463,7 @@ func (f *File) extractFills(fl *xlsxFill, s *xlsxStyleSheet, style *Style) {
var fill Fill var fill Fill
if fl.GradientFill != nil { if fl.GradientFill != nil {
fill.Type = "gradient" fill.Type = "gradient"
for shading, variants := range styleFillVariants { for shading, variants := range styleFillVariants() {
if fl.GradientFill.Bottom == variants.Bottom && if fl.GradientFill.Bottom == variants.Bottom &&
fl.GradientFill.Degree == variants.Degree && fl.GradientFill.Degree == variants.Degree &&
fl.GradientFill.Left == variants.Left && fl.GradientFill.Left == variants.Left &&
@ -2024,7 +2031,7 @@ func newFills(style *Style, fg bool) *xlsxFill {
if len(style.Fill.Color) != 2 || style.Fill.Shading < 0 || style.Fill.Shading > 16 { if len(style.Fill.Color) != 2 || style.Fill.Shading < 0 || style.Fill.Shading > 16 {
break break
} }
gradient := styleFillVariants[style.Fill.Shading] gradient := styleFillVariants()[style.Fill.Shading]
gradient.Stop[0].Color.RGB = getPaletteColor(style.Fill.Color[0]) gradient.Stop[0].Color.RGB = getPaletteColor(style.Fill.Color[0])
gradient.Stop[1].Color.RGB = getPaletteColor(style.Fill.Color[1]) gradient.Stop[1].Color.RGB = getPaletteColor(style.Fill.Color[1])
if len(gradient.Stop) == 3 { if len(gradient.Stop) == 3 {

View File

@ -341,7 +341,16 @@ func TestNewStyle(t *testing.T) {
_, err = f.NewStyle(nil) _, err = f.NewStyle(nil)
assert.NoError(t, err) assert.NoError(t, err)
// Test gradient fills
f = NewFile()
styleID1, err := f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"FFFFFF", "4E71BE"}, Shading: 1, Pattern: 1}})
assert.NoError(t, err)
styleID2, err := f.NewStyle(&Style{Fill: Fill{Type: "gradient", Color: []string{"FF0000", "4E71BE"}, Shading: 1, Pattern: 1}})
assert.NoError(t, err)
assert.NotEqual(t, styleID1, styleID2)
var exp string var exp string
f = NewFile()
_, err = f.NewStyle(&Style{CustomNumFmt: &exp}) _, err = f.NewStyle(&Style{CustomNumFmt: &exp})
assert.Equal(t, ErrCustomNumFmt, err) assert.Equal(t, ErrCustomNumFmt, err)
_, err = f.NewStyle(&Style{Font: &Font{Family: strings.Repeat("s", MaxFontFamilyLength+1)}}) _, err = f.NewStyle(&Style{Font: &Font{Family: strings.Repeat("s", MaxFontFamilyLength+1)}})
@ -356,7 +365,7 @@ func TestNewStyle(t *testing.T) {
CustomNumFmt: &numFmt, CustomNumFmt: &numFmt,
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, 2, styleID) assert.Equal(t, 1, styleID)
assert.NotNil(t, f.Styles) assert.NotNil(t, f.Styles)
assert.NotNil(t, f.Styles.CellXfs) assert.NotNil(t, f.Styles.CellXfs)
@ -371,7 +380,7 @@ func TestNewStyle(t *testing.T) {
NumFmt: 32, // must not be in currencyNumFmt NumFmt: 32, // must not be in currencyNumFmt
}) })
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, 3, styleID) assert.Equal(t, 2, styleID)
assert.NotNil(t, f.Styles) assert.NotNil(t, f.Styles)
assert.NotNil(t, f.Styles.CellXfs) assert.NotNil(t, f.Styles.CellXfs)