Skip to content

Commit 8418bd7

Browse files
committed
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
1 parent f5fe6d3 commit 8418bd7

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

excelize_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,11 @@ func TestSetCellStyleCurrencyNumberFormat(t *testing.T) {
803803
assert.NoError(t, f.SetCellValue("Sheet1", "A1", 56))
804804
assert.NoError(t, f.SetCellValue("Sheet1", "A2", -32.3))
805805
var style int
806-
style, err = f.NewStyle(&Style{NumFmt: 188, DecimalPlaces: -1})
806+
style, err = f.NewStyle(&Style{NumFmt: 188, DecimalPlaces: intPtr(-1)})
807807
assert.NoError(t, err)
808808

809809
assert.NoError(t, f.SetCellStyle("Sheet1", "A1", "A1", style))
810-
style, err = f.NewStyle(&Style{NumFmt: 188, DecimalPlaces: 31, NegRed: true})
810+
style, err = f.NewStyle(&Style{NumFmt: 188, DecimalPlaces: intPtr(31), NegRed: true})
811811
assert.NoError(t, err)
812812

813813
assert.NoError(t, f.SetCellStyle("Sheet1", "A2", "A2", style))

styles.go

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -977,8 +977,8 @@ func (f *File) NewStyle(style *Style) (int, error) {
977977
if err != nil {
978978
return cellXfsID, err
979979
}
980-
if fs.DecimalPlaces == 0 {
981-
fs.DecimalPlaces = 2
980+
if fs.DecimalPlaces != nil && (*fs.DecimalPlaces < 0 || *fs.DecimalPlaces > 30) {
981+
fs.DecimalPlaces = intPtr(2)
982982
}
983983
f.mu.Lock()
984984
s, err := f.stylesReader()
@@ -1037,7 +1037,7 @@ var getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{
10371037
if style.CustomNumFmt == nil && numFmtID == -1 {
10381038
return xf.NumFmtID != nil && *xf.NumFmtID == 0
10391039
}
1040-
if style.NegRed || style.DecimalPlaces != 2 {
1040+
if style.NegRed || (style.DecimalPlaces != nil && *style.DecimalPlaces != 2) {
10411041
return false
10421042
}
10431043
return xf.NumFmtID != nil && *xf.NumFmtID == numFmtID
@@ -1291,49 +1291,39 @@ func getNumFmtID(styleSheet *xlsxStyleSheet, style *Style) (numFmtID int) {
12911291
// newNumFmt provides a function to check if number format code in the range
12921292
// of built-in values.
12931293
func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int {
1294-
dp := "0."
1295-
numFmtID := 164 // Default custom number format code from 164.
1296-
if style.DecimalPlaces < 0 || style.DecimalPlaces > 30 {
1297-
style.DecimalPlaces = 2
1298-
}
1299-
for i := 0; i < style.DecimalPlaces; i++ {
1300-
dp += "0"
1294+
dp, numFmtID := "0", 164 // Default custom number format code from 164.
1295+
if style.DecimalPlaces != nil && *style.DecimalPlaces > 0 {
1296+
dp += "."
1297+
for i := 0; i < *style.DecimalPlaces; i++ {
1298+
dp += "0"
1299+
}
13011300
}
13021301
if style.CustomNumFmt != nil {
13031302
if customNumFmtID := getCustomNumFmtID(styleSheet, style); customNumFmtID != -1 {
13041303
return customNumFmtID
13051304
}
13061305
return setCustomNumFmt(styleSheet, style)
13071306
}
1308-
_, ok := builtInNumFmt[style.NumFmt]
1309-
if !ok {
1307+
if _, ok := builtInNumFmt[style.NumFmt]; !ok {
13101308
fc, currency := currencyNumFmt[style.NumFmt]
13111309
if !currency {
13121310
return setLangNumFmt(style)
13131311
}
1314-
fc = strings.ReplaceAll(fc, "0.00", dp)
1312+
if style.DecimalPlaces != nil {
1313+
fc = strings.ReplaceAll(fc, "0.00", dp)
1314+
}
13151315
if style.NegRed {
13161316
fc = fc + ";[Red]" + fc
13171317
}
1318-
if styleSheet.NumFmts != nil {
1319-
numFmtID = styleSheet.NumFmts.NumFmt[len(styleSheet.NumFmts.NumFmt)-1].NumFmtID + 1
1320-
nf := xlsxNumFmt{
1321-
FormatCode: fc,
1322-
NumFmtID: numFmtID,
1323-
}
1324-
styleSheet.NumFmts.NumFmt = append(styleSheet.NumFmts.NumFmt, &nf)
1325-
styleSheet.NumFmts.Count++
1318+
if styleSheet.NumFmts == nil {
1319+
styleSheet.NumFmts = &xlsxNumFmts{NumFmt: []*xlsxNumFmt{}}
13261320
} else {
1327-
nf := xlsxNumFmt{
1328-
FormatCode: fc,
1329-
NumFmtID: numFmtID,
1330-
}
1331-
numFmts := xlsxNumFmts{
1332-
NumFmt: []*xlsxNumFmt{&nf},
1333-
Count: 1,
1334-
}
1335-
styleSheet.NumFmts = &numFmts
1321+
numFmtID = styleSheet.NumFmts.NumFmt[len(styleSheet.NumFmts.NumFmt)-1].NumFmtID + 1
13361322
}
1323+
styleSheet.NumFmts.NumFmt = append(styleSheet.NumFmts.NumFmt, &xlsxNumFmt{
1324+
FormatCode: fc, NumFmtID: numFmtID,
1325+
})
1326+
styleSheet.NumFmts.Count++
13371327
return numFmtID
13381328
}
13391329
return style.NumFmt

xmlStyles.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ type Style struct {
369369
Alignment *Alignment
370370
Protection *Protection
371371
NumFmt int
372-
DecimalPlaces int
372+
DecimalPlaces *int
373373
CustomNumFmt *string
374374
NegRed bool
375375
}

0 commit comments

Comments
 (0)