Skip to content

Commit 4917cff

Browse files
authored
This closes #2254, fixx duplicate style creation when using default font or fill (#2255)
1 parent 38eb7c1 commit 4917cff

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

styles.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,13 +1115,13 @@ var (
11151115
return xf.NumFmtID != nil && *xf.NumFmtID == numFmtID
11161116
},
11171117
"font": func(fontID int, xf xlsxXf, style *Style) bool {
1118-
if style.Font == nil {
1118+
if style.Font == nil || fontID == 0 {
11191119
return (xf.FontID == nil || *xf.FontID == 0) && (xf.ApplyFont == nil || !*xf.ApplyFont)
11201120
}
11211121
return xf.FontID != nil && *xf.FontID == fontID && xf.ApplyFont != nil && *xf.ApplyFont
11221122
},
11231123
"fill": func(fillID int, xf xlsxXf, style *Style) bool {
1124-
if style.Fill.Type == "" {
1124+
if style.Fill.Type == "" || fillID == 0 {
11251125
return (xf.FillID == nil || *xf.FillID == 0) && (xf.ApplyFill == nil || !*xf.ApplyFill)
11261126
}
11271127
return xf.FillID != nil && *xf.FillID == fillID && xf.ApplyFill != nil && *xf.ApplyFill

styles_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,15 @@ func TestNewStyle(t *testing.T) {
544544
assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", styleID))
545545
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetFontCharset.xlsx")))
546546
})
547+
548+
t.Run("recreate_default_style", func(t *testing.T) {
549+
f := NewFile()
550+
style2, err := f.GetStyle(0) // Get the default style
551+
assert.NoError(t, err)
552+
styleId, err := f.NewStyle(style2) // Try to recreate the same style. Should return style ID 0
553+
assert.NoError(t, err)
554+
assert.Equal(t, 0, styleId)
555+
})
547556
}
548557

549558
func TestConditionalStyle(t *testing.T) {

0 commit comments

Comments
 (0)