Fix call getNumFmtID with builtInNumFmt return -1

This commit is contained in:
Dokiy 2021-12-01 19:11:51 +08:00
parent bb0eb4a42b
commit 45a1f08a2a
2 changed files with 18 additions and 3 deletions

View File

@ -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 {

View File

@ -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")))
}