summaryrefslogtreecommitdiff
path: root/styles.go
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2020-05-25 00:22:58 +0800
committerxuri <xuri.me@gmail.com>2020-05-25 00:22:58 +0800
commit1aeb8182357ae4f1415fccb586c85f26483f0d99 (patch)
tree274493bee1e84dca3e942a10badad744b88d5581 /styles.go
parenta546427fd9324af8320e3f7062ddba8d2343a3c3 (diff)
avoid creating duplicate number format
Diffstat (limited to 'styles.go')
-rw-r--r--styles.go36
1 files changed, 32 insertions, 4 deletions
diff --git a/styles.go b/styles.go
index 2f9a41e..a91aca6 100644
--- a/styles.go
+++ b/styles.go
@@ -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 {