This closes #1677, fix the incorrect custom number format ID allocated

- Improve compatibility with empty custom number format code
This commit is contained in:
xuri 2023-10-02 00:06:38 +08:00
parent f85770f4c9
commit 1c7c417c70
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
2 changed files with 12 additions and 14 deletions

View File

@ -1694,20 +1694,18 @@ func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int {
// setCustomNumFmt provides a function to set custom number format code.
func setCustomNumFmt(styleSheet *xlsxStyleSheet, style *Style) int {
nf := xlsxNumFmt{FormatCode: *style.CustomNumFmt}
if styleSheet.NumFmts != nil {
nf.NumFmtID = styleSheet.NumFmts.NumFmt[len(styleSheet.NumFmts.NumFmt)-1].NumFmtID + 1
styleSheet.NumFmts.NumFmt = append(styleSheet.NumFmts.NumFmt, &nf)
styleSheet.NumFmts.Count++
} else {
nf.NumFmtID = 164
numFmts := xlsxNumFmts{
NumFmt: []*xlsxNumFmt{&nf},
Count: 1,
}
styleSheet.NumFmts = &numFmts
nf := xlsxNumFmt{NumFmtID: 163, FormatCode: *style.CustomNumFmt}
if styleSheet.NumFmts == nil {
styleSheet.NumFmts = &xlsxNumFmts{}
}
for _, numFmt := range styleSheet.NumFmts.NumFmt {
if numFmt != nil && nf.NumFmtID < numFmt.NumFmtID {
nf.NumFmtID = numFmt.NumFmtID
}
}
nf.NumFmtID++
styleSheet.NumFmts.NumFmt = append(styleSheet.NumFmts.NumFmt, &nf)
styleSheet.NumFmts.Count = len(styleSheet.NumFmts.NumFmt)
return nf.NumFmtID
}

View File

@ -296,7 +296,7 @@ type xlsxNumFmts struct {
// of a cell.
type xlsxNumFmt struct {
NumFmtID int `xml:"numFmtId,attr"`
FormatCode string `xml:"formatCode,attr,omitempty"`
FormatCode string `xml:"formatCode,attr"`
FormatCode16 string `xml:"http://schemas.microsoft.com/office/spreadsheetml/2015/02/main formatCode16,attr,omitempty"`
}