avoid creating duplicate number format

This commit is contained in:
xuri 2020-05-25 00:22:58 +08:00
parent a546427fd9
commit 1aeb818235
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
1 changed files with 32 additions and 4 deletions

View File

@ -1036,9 +1036,7 @@ func (f *File) sharedStringsWriter() {
// parseFormatStyleSet provides a function to parse the format settings of the
// cells and conditional formats.
func parseFormatStyleSet(style string) (*Style, error) {
format := Style{
DecimalPlaces: 2,
}
format := Style{}
err := json.Unmarshal([]byte(style), &format)
return &format, err
}
@ -1920,6 +1918,9 @@ func (f *File) NewStyle(style interface{}) (int, error) {
default:
return cellXfsID, errors.New("invalid parameter type")
}
if fs.DecimalPlaces == 0 {
fs.DecimalPlaces = 2
}
s := f.stylesReader()
// check given style already exist.
if cellXfsID = f.getStyleID(s, fs); cellXfsID != -1 {
@ -1966,6 +1967,12 @@ func (f *File) NewStyle(style interface{}) (int, error) {
var getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{
"numFmt": func(numFmtID int, xf xlsxXf, style *Style) bool {
if style.NumFmt == 0 && style.CustomNumFmt == nil && numFmtID == -1 {
return xf.NumFmtID != nil || *xf.NumFmtID == 0
}
if style.NegRed || style.Lang != "" || style.DecimalPlaces != 2 {
return false
}
return xf.NumFmtID != nil && *xf.NumFmtID == numFmtID
},
"font": func(fontID int, xf xlsxXf, style *Style) bool {
@ -2007,7 +2014,7 @@ func (f *File) getStyleID(ss *xlsxStyleSheet, style *Style) (styleID int) {
if ss.CellXfs == nil {
return
}
numFmtID, borderID, fillID, fontID := style.NumFmt, getBorderID(ss, style), getFillID(ss, style), f.getFontID(ss, style)
numFmtID, borderID, fillID, fontID := getNumFmtID(ss, style), getBorderID(ss, style), getFillID(ss, style), f.getFontID(ss, style)
if style.CustomNumFmt != nil {
numFmtID = getCustomNumFmtID(ss, style)
}
@ -2133,6 +2140,27 @@ func (f *File) newFont(style *Style) *xlsxFont {
return &fnt
}
// getNumFmtID provides a function to get number format code ID.
// If given number format code is not exist, will return -1.
func getNumFmtID(styleSheet *xlsxStyleSheet, style *Style) (numFmtID int) {
numFmtID = -1
if styleSheet.NumFmts == nil {
return
}
if _, ok := builtInNumFmt[style.NumFmt]; ok {
return style.NumFmt
}
if fmtCode, ok := currencyNumFmt[style.NumFmt]; ok {
for _, numFmt := range styleSheet.NumFmts.NumFmt {
if numFmt.FormatCode == fmtCode {
numFmtID = numFmt.NumFmtID
return
}
}
}
return
}
// newNumFmt provides a function to check if number format code in the range
// of built-in values.
func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int {