Skip to content

Commit

Permalink
This closes qax-os#1572
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
xuri committed Jul 8, 2023
1 parent f5fe6d3 commit 8418bd7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
4 changes: 2 additions & 2 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
50 changes: 20 additions & 30 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1291,49 +1291,39 @@ 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 {
return customNumFmtID
}
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
Expand Down
2 changes: 1 addition & 1 deletion xmlStyles.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ type Style struct {
Alignment *Alignment
Protection *Protection
NumFmt int
DecimalPlaces int
DecimalPlaces *int
CustomNumFmt *string
NegRed bool
}

0 comments on commit 8418bd7

Please sign in to comment.