Skip to content

Commit

Permalink
Fix call getNumFmtID with builtInNumFmt return -1
Browse files Browse the repository at this point in the history
  • Loading branch information
Dokiy committed Dec 1, 2021
1 parent bb0eb4a commit 45a1f08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2229,12 +2229,12 @@ func (f *File) newFont(style *Style) *xlsxFont {
// If given number format code is not exist, will return -1.
func getNumFmtID(styleSheet *xlsxStyleSheet, style *Style) (numFmtID int) {
numFmtID = -1
if styleSheet.NumFmts == nil {
return
}
if _, ok := builtInNumFmt[style.NumFmt]; ok {
return style.NumFmt
}
if styleSheet.NumFmts == nil {
return
}
if fmtCode, ok := currencyNumFmt[style.NumFmt]; ok {
for _, numFmt := range styleSheet.NumFmts.NumFmt {
if numFmt.FormatCode == fmtCode {
Expand Down
15 changes: 15 additions & 0 deletions styles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,18 @@ func TestThemeColor(t *testing.T) {
assert.Equal(t, clr[0], clr[1])
}
}

func TestGetNumFmtID(t *testing.T) {
f := NewFile()

fs1, err := parseFormatStyleSet(`{"protection":{"hidden":false,"locked":false},"number_format":10}`)
assert.NoError(t, err)
id1 := getNumFmtID(&xlsxStyleSheet{}, fs1)

fs2, err := parseFormatStyleSet(`{"protection":{"hidden":false,"locked":false},"number_format":0}`)
assert.NoError(t, err)
id2 := getNumFmtID(&xlsxStyleSheet{}, fs2)

assert.NotEqual(t, id1, id2)
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestStyleNumFmt.xlsx")))
}

0 comments on commit 45a1f08

Please sign in to comment.