- 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
This commit is contained in:
xuri 2023-07-08 18:36:35 +08:00
parent f5fe6d3fc9
commit 8418bd7afd
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 23 additions and 33 deletions

View File

@ -803,11 +803,11 @@ func TestSetCellStyleCurrencyNumberFormat(t *testing.T) {
assert.NoError(t, f.SetCellValue("Sheet1", "A1", 56)) assert.NoError(t, f.SetCellValue("Sheet1", "A1", 56))
assert.NoError(t, f.SetCellValue("Sheet1", "A2", -32.3)) assert.NoError(t, f.SetCellValue("Sheet1", "A2", -32.3))
var style int 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, err)
assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", style)) 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, err)
assert.NoError(t, f.SetCellStyle("Sheet1", "A2", "A2", style)) assert.NoError(t, f.SetCellStyle("Sheet1", "A2", "A2", style))

View File

@ -977,8 +977,8 @@ func (f *File) NewStyle(style *Style) (int, error) {
if err != nil { if err != nil {
return cellXfsID, err return cellXfsID, err
} }
if fs.DecimalPlaces == 0 { if fs.DecimalPlaces != nil && (*fs.DecimalPlaces < 0 || *fs.DecimalPlaces > 30) {
fs.DecimalPlaces = 2 fs.DecimalPlaces = intPtr(2)
} }
f.mu.Lock() f.mu.Lock()
s, err := f.stylesReader() s, err := f.stylesReader()
@ -1037,7 +1037,7 @@ var getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{
if style.CustomNumFmt == nil && numFmtID == -1 { if style.CustomNumFmt == nil && numFmtID == -1 {
return xf.NumFmtID != nil && *xf.NumFmtID == 0 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 false
} }
return xf.NumFmtID != nil && *xf.NumFmtID == numFmtID 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 // newNumFmt provides a function to check if number format code in the range
// of built-in values. // of built-in values.
func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int { func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int {
dp := "0." dp, numFmtID := "0", 164 // Default custom number format code from 164.
numFmtID := 164 // Default custom number format code from 164. if style.DecimalPlaces != nil && *style.DecimalPlaces > 0 {
if style.DecimalPlaces < 0 || style.DecimalPlaces > 30 { dp += "."
style.DecimalPlaces = 2 for i := 0; i < *style.DecimalPlaces; i++ {
} dp += "0"
for i := 0; i < style.DecimalPlaces; i++ { }
dp += "0"
} }
if style.CustomNumFmt != nil { if style.CustomNumFmt != nil {
if customNumFmtID := getCustomNumFmtID(styleSheet, style); customNumFmtID != -1 { if customNumFmtID := getCustomNumFmtID(styleSheet, style); customNumFmtID != -1 {
@ -1305,35 +1304,26 @@ func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int {
} }
return setCustomNumFmt(styleSheet, style) return setCustomNumFmt(styleSheet, style)
} }
_, ok := builtInNumFmt[style.NumFmt] if _, ok := builtInNumFmt[style.NumFmt]; !ok {
if !ok {
fc, currency := currencyNumFmt[style.NumFmt] fc, currency := currencyNumFmt[style.NumFmt]
if !currency { if !currency {
return setLangNumFmt(style) return setLangNumFmt(style)
} }
fc = strings.ReplaceAll(fc, "0.00", dp) if style.DecimalPlaces != nil {
fc = strings.ReplaceAll(fc, "0.00", dp)
}
if style.NegRed { if style.NegRed {
fc = fc + ";[Red]" + fc fc = fc + ";[Red]" + fc
} }
if styleSheet.NumFmts != nil { if styleSheet.NumFmts == nil {
numFmtID = styleSheet.NumFmts.NumFmt[len(styleSheet.NumFmts.NumFmt)-1].NumFmtID + 1 styleSheet.NumFmts = &xlsxNumFmts{NumFmt: []*xlsxNumFmt{}}
nf := xlsxNumFmt{
FormatCode: fc,
NumFmtID: numFmtID,
}
styleSheet.NumFmts.NumFmt = append(styleSheet.NumFmts.NumFmt, &nf)
styleSheet.NumFmts.Count++
} else { } else {
nf := xlsxNumFmt{ numFmtID = styleSheet.NumFmts.NumFmt[len(styleSheet.NumFmts.NumFmt)-1].NumFmtID + 1
FormatCode: fc,
NumFmtID: numFmtID,
}
numFmts := xlsxNumFmts{
NumFmt: []*xlsxNumFmt{&nf},
Count: 1,
}
styleSheet.NumFmts = &numFmts
} }
styleSheet.NumFmts.NumFmt = append(styleSheet.NumFmts.NumFmt, &xlsxNumFmt{
FormatCode: fc, NumFmtID: numFmtID,
})
styleSheet.NumFmts.Count++
return numFmtID return numFmtID
} }
return style.NumFmt return style.NumFmt

View File

@ -369,7 +369,7 @@ type Style struct {
Alignment *Alignment Alignment *Alignment
Protection *Protection Protection *Protection
NumFmt int NumFmt int
DecimalPlaces int DecimalPlaces *int
CustomNumFmt *string CustomNumFmt *string
NegRed bool NegRed bool
} }