From 08207ea11033449739e500cc6d3356d105529b4d Mon Sep 17 00:00:00 2001 From: xuri Date: Mon, 2 Oct 2023 00:06:38 +0800 Subject: [PATCH] This closes #1677, fix the incorrect custom number format ID allocated - Improve compatibility with empty custom number format code --- styles.go | 22 ++++++++++------------ xmlStyles.go | 2 +- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/styles.go b/styles.go index 1de83bcb03..40505b7b9e 100644 --- a/styles.go +++ b/styles.go @@ -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, + 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 } - styleSheet.NumFmts = &numFmts } + nf.NumFmtID++ + styleSheet.NumFmts.NumFmt = append(styleSheet.NumFmts.NumFmt, &nf) + styleSheet.NumFmts.Count = len(styleSheet.NumFmts.NumFmt) return nf.NumFmtID } diff --git a/xmlStyles.go b/xmlStyles.go index 3dd61c3307..613001c8e4 100644 --- a/xmlStyles.go +++ b/xmlStyles.go @@ -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"` }