avoid creating duplicate number format

formula
xuri 5 years ago
parent a546427fd9
commit 1aeb818235
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7

@ -1036,9 +1036,7 @@ func (f *File) sharedStringsWriter() {
// parseFormatStyleSet provides a function to parse the format settings of the // parseFormatStyleSet provides a function to parse the format settings of the
// cells and conditional formats. // cells and conditional formats.
func parseFormatStyleSet(style string) (*Style, error) { func parseFormatStyleSet(style string) (*Style, error) {
format := Style{ format := Style{}
DecimalPlaces: 2,
}
err := json.Unmarshal([]byte(style), &format) err := json.Unmarshal([]byte(style), &format)
return &format, err return &format, err
} }
@ -1920,6 +1918,9 @@ func (f *File) NewStyle(style interface{}) (int, error) {
default: default:
return cellXfsID, errors.New("invalid parameter type") return cellXfsID, errors.New("invalid parameter type")
} }
if fs.DecimalPlaces == 0 {
fs.DecimalPlaces = 2
}
s := f.stylesReader() s := f.stylesReader()
// check given style already exist. // check given style already exist.
if cellXfsID = f.getStyleID(s, fs); cellXfsID != -1 { 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{ var getXfIDFuncs = map[string]func(int, xlsxXf, *Style) bool{
"numFmt": func(numFmtID int, xf xlsxXf, style *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 return xf.NumFmtID != nil && *xf.NumFmtID == numFmtID
}, },
"font": func(fontID int, xf xlsxXf, style *Style) bool { "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 { if ss.CellXfs == nil {
return 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 { if style.CustomNumFmt != nil {
numFmtID = getCustomNumFmtID(ss, style) numFmtID = getCustomNumFmtID(ss, style)
} }
@ -2133,6 +2140,27 @@ func (f *File) newFont(style *Style) *xlsxFont {
return &fnt 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 // newNumFmt provides a function to check if number format code in the range
// of built-in values. // of built-in values.
func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int { func newNumFmt(styleSheet *xlsxStyleSheet, style *Style) int {

Loading…
Cancel
Save