From 8418bd7afd63fb822e5cee0c88aabf7d35029332 Mon Sep 17 00:00:00 2001 From: xuri Date: Sat, 8 Jul 2023 18:36:35 +0800 Subject: [PATCH] This closes #1572 - Breaking changes: changed the data type for the `DecimalPlaces` to pointer of integer - Fallback to default 2 zero placeholder for invalid decimal places - Update unit tests --- excelize_test.go | 4 ++-- styles.go | 50 +++++++++++++++++++----------------------------- xmlStyles.go | 2 +- 3 files changed, 23 insertions(+), 33 deletions(-) diff --git a/excelize_test.go b/excelize_test.go index 5ef9207be5..9372be5865 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -803,11 +803,11 @@ func TestSetCellStyleCurrencyNumberFormat(t *testing.T) { assert.NoError(t, f.SetCellValue("Sheet1", "A1", 56)) assert.NoError(t, f.SetCellValue("Sheet1", "A2", -32.3)) var style int - style, err = f.NewStyle(&Style{NumFmt: 188, DecimalPlaces: -1}) + style, err = f.NewStyle(&Style{NumFmt: 188, DecimalPlaces: intPtr(-1)}) assert.NoError(t, err) assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", style)) - style, err = f.NewStyle(&Style{NumFmt: 188, DecimalPlaces: 31, NegRed: true}) + style, err = f.NewStyle(&Style{NumFmt: 188, DecimalPlaces: intPtr(31), NegRed: true}) assert.NoError(t, err) assert.NoError(t, f.SetCellStyle("Sheet1", "A2", "A2", style)) diff --git a/styles.go b/styles.go index 70c11d596b..a2d021f267 100644 --- a/styles.go +++ b/styles.go @@ -977,8 +977,8 @@ func (f *File) NewStyle(style *Style) (int, error) { if err != nil { return cellXfsID, err } - if fs.DecimalPlaces == 0 { - fs.DecimalPlaces = 2 + if fs.DecimalPlaces != nil && (*fs.DecimalPlaces < 0 || *fs.DecimalPlaces > 30) { + fs.DecimalPlaces = intPtr(2) } f.mu.Lock() s, err := f.stylesReader() @@ -1037,7 +1037,7 @@ var getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{ if style.CustomNumFmt == nil && numFmtID == -1 { return xf.NumFmtID != nil && *xf.NumFmtID == 0 } - if style.NegRed || style.DecimalPlaces != 2 { + if style.NegRed || (style.DecimalPlaces != nil && *style.DecimalPlaces != 2) { return false } return xf.NumFmtID != nil && *xf.NumFmtID == numFmtID @@ -1291,13 +1291,12 @@ func getNumFmtID(styleSheet *xlsxStyleSheet, style *Style) (numFmtID int) { // newNumFmt provides a function to check if number format code in the range // of built-in values. func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int { - dp := "0." - numFmtID := 164 // Default custom number format code from 164. - if style.DecimalPlaces < 0 || style.DecimalPlaces > 30 { - style.DecimalPlaces = 2 - } - for i := 0; i < style.DecimalPlaces; i++ { - dp += "0" + dp, numFmtID := "0", 164 // Default custom number format code from 164. + if style.DecimalPlaces != nil && *style.DecimalPlaces > 0 { + dp += "." + for i := 0; i < *style.DecimalPlaces; i++ { + dp += "0" + } } if style.CustomNumFmt != nil { if customNumFmtID := getCustomNumFmtID(styleSheet, style); customNumFmtID != -1 { @@ -1305,35 +1304,26 @@ func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int { } return setCustomNumFmt(styleSheet, style) } - _, ok := builtInNumFmt[style.NumFmt] - if !ok { + if _, ok := builtInNumFmt[style.NumFmt]; !ok { fc, currency := currencyNumFmt[style.NumFmt] if !currency { return setLangNumFmt(style) } - fc = strings.ReplaceAll(fc, "0.00", dp) + if style.DecimalPlaces != nil { + fc = strings.ReplaceAll(fc, "0.00", dp) + } if style.NegRed { fc = fc + ";[Red]" + fc } - if styleSheet.NumFmts != nil { - numFmtID = styleSheet.NumFmts.NumFmt[len(styleSheet.NumFmts.NumFmt)-1].NumFmtID + 1 - nf := xlsxNumFmt{ - FormatCode: fc, - NumFmtID: numFmtID, - } - styleSheet.NumFmts.NumFmt = append(styleSheet.NumFmts.NumFmt, &nf) - styleSheet.NumFmts.Count++ + if styleSheet.NumFmts == nil { + styleSheet.NumFmts = &xlsxNumFmts{NumFmt: []*xlsxNumFmt{}} } else { - nf := xlsxNumFmt{ - FormatCode: fc, - NumFmtID: numFmtID, - } - numFmts := xlsxNumFmts{ - NumFmt: []*xlsxNumFmt{&nf}, - Count: 1, - } - styleSheet.NumFmts = &numFmts + numFmtID = styleSheet.NumFmts.NumFmt[len(styleSheet.NumFmts.NumFmt)-1].NumFmtID + 1 } + styleSheet.NumFmts.NumFmt = append(styleSheet.NumFmts.NumFmt, &xlsxNumFmt{ + FormatCode: fc, NumFmtID: numFmtID, + }) + styleSheet.NumFmts.Count++ return numFmtID } return style.NumFmt diff --git a/xmlStyles.go b/xmlStyles.go index 74b9119b16..3a56f6f618 100644 --- a/xmlStyles.go +++ b/xmlStyles.go @@ -369,7 +369,7 @@ type Style struct { Alignment *Alignment Protection *Protection NumFmt int - DecimalPlaces int + DecimalPlaces *int CustomNumFmt *string NegRed bool }