Skip to content

Commit 1c7c417

Browse files
committed
This closes qax-os#1677, fix the incorrect custom number format ID allocated
- Improve compatibility with empty custom number format code
1 parent f85770f commit 1c7c417

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

styles.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -1694,20 +1694,18 @@ func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int {
16941694

16951695
// setCustomNumFmt provides a function to set custom number format code.
16961696
func setCustomNumFmt(styleSheet *xlsxStyleSheet, style *Style) int {
1697-
nf := xlsxNumFmt{FormatCode: *style.CustomNumFmt}
1698-
1699-
if styleSheet.NumFmts != nil {
1700-
nf.NumFmtID = styleSheet.NumFmts.NumFmt[len(styleSheet.NumFmts.NumFmt)-1].NumFmtID + 1
1701-
styleSheet.NumFmts.NumFmt = append(styleSheet.NumFmts.NumFmt, &nf)
1702-
styleSheet.NumFmts.Count++
1703-
} else {
1704-
nf.NumFmtID = 164
1705-
numFmts := xlsxNumFmts{
1706-
NumFmt: []*xlsxNumFmt{&nf},
1707-
Count: 1,
1697+
nf := xlsxNumFmt{NumFmtID: 163, FormatCode: *style.CustomNumFmt}
1698+
if styleSheet.NumFmts == nil {
1699+
styleSheet.NumFmts = &xlsxNumFmts{}
1700+
}
1701+
for _, numFmt := range styleSheet.NumFmts.NumFmt {
1702+
if numFmt != nil && nf.NumFmtID < numFmt.NumFmtID {
1703+
nf.NumFmtID = numFmt.NumFmtID
17081704
}
1709-
styleSheet.NumFmts = &numFmts
17101705
}
1706+
nf.NumFmtID++
1707+
styleSheet.NumFmts.NumFmt = append(styleSheet.NumFmts.NumFmt, &nf)
1708+
styleSheet.NumFmts.Count = len(styleSheet.NumFmts.NumFmt)
17111709
return nf.NumFmtID
17121710
}
17131711

xmlStyles.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ type xlsxNumFmts struct {
296296
// of a cell.
297297
type xlsxNumFmt struct {
298298
NumFmtID int `xml:"numFmtId,attr"`
299-
FormatCode string `xml:"formatCode,attr,omitempty"`
299+
FormatCode string `xml:"formatCode,attr"`
300300
FormatCode16 string `xml:"http://schemas.microsoft.com/office/spreadsheetml/2015/02/main formatCode16,attr,omitempty"`
301301
}
302302

0 commit comments

Comments
 (0)