@@ -1032,6 +1032,7 @@ func (f *File) NewStyle(style *Style) (int, error) {
1032
1032
return setCellXfs (s , fontID , numFmtID , fillID , borderID , applyAlignment , applyProtection , alignment , protection )
1033
1033
}
1034
1034
1035
+ // getXfIDFuncs provides a function to get xfID by given style.
1035
1036
var getXfIDFuncs = map [string ]func (int , xlsxXf , * Style ) bool {
1036
1037
"numFmt" : func (numFmtID int , xf xlsxXf , style * Style ) bool {
1037
1038
if style .CustomNumFmt == nil && numFmtID == - 1 {
@@ -1121,7 +1122,10 @@ func (f *File) NewConditionalStyle(style *Style) (int, error) {
1121
1122
if err != nil {
1122
1123
return 0 , err
1123
1124
}
1124
- dxf := dxf {
1125
+ if fs .DecimalPlaces != nil && (* fs .DecimalPlaces < 0 || * fs .DecimalPlaces > 30 ) {
1126
+ fs .DecimalPlaces = intPtr (2 )
1127
+ }
1128
+ dxf := xlsxDxf {
1125
1129
Fill : newFills (fs , false ),
1126
1130
}
1127
1131
if fs .Alignment != nil {
@@ -1133,17 +1137,55 @@ func (f *File) NewConditionalStyle(style *Style) (int, error) {
1133
1137
if fs .Font != nil {
1134
1138
dxf .Font , _ = f .newFont (fs )
1135
1139
}
1136
- dxfStr , _ := xml .Marshal (dxf )
1140
+ if fs .Protection != nil {
1141
+ dxf .Protection = newProtection (fs )
1142
+ }
1143
+ dxf .NumFmt = newDxfNumFmt (s , style , & dxf )
1137
1144
if s .Dxfs == nil {
1138
1145
s .Dxfs = & xlsxDxfs {}
1139
1146
}
1140
1147
s .Dxfs .Count ++
1141
- s .Dxfs .Dxfs = append (s .Dxfs .Dxfs , & xlsxDxf {
1142
- Dxf : string (dxfStr [5 : len (dxfStr )- 6 ]),
1143
- })
1148
+ s .Dxfs .Dxfs = append (s .Dxfs .Dxfs , & dxf )
1144
1149
return s .Dxfs .Count - 1 , nil
1145
1150
}
1146
1151
1152
+ // newDxfNumFmt provides a function to create number format for conditional
1153
+ // format styles.
1154
+ func newDxfNumFmt (styleSheet * xlsxStyleSheet , style * Style , dxf * xlsxDxf ) * xlsxNumFmt {
1155
+ dp , numFmtID := "0" , 164 // Default custom number format code from 164.
1156
+ if style .DecimalPlaces != nil && * style .DecimalPlaces > 0 {
1157
+ dp += "."
1158
+ for i := 0 ; i < * style .DecimalPlaces ; i ++ {
1159
+ dp += "0"
1160
+ }
1161
+ }
1162
+ if style .CustomNumFmt != nil {
1163
+ if styleSheet .Dxfs != nil {
1164
+ for _ , d := range styleSheet .Dxfs .Dxfs {
1165
+ if d != nil && d .NumFmt != nil && d .NumFmt .NumFmtID > numFmtID {
1166
+ numFmtID = d .NumFmt .NumFmtID
1167
+ }
1168
+ }
1169
+ }
1170
+ return & xlsxNumFmt {NumFmtID : numFmtID + 1 , FormatCode : * style .CustomNumFmt }
1171
+ }
1172
+ numFmtCode , ok := builtInNumFmt [style .NumFmt ]
1173
+ if style .NumFmt > 0 && ok {
1174
+ return & xlsxNumFmt {NumFmtID : style .NumFmt , FormatCode : numFmtCode }
1175
+ }
1176
+ fc , currency := currencyNumFmt [style .NumFmt ]
1177
+ if ! currency {
1178
+ return nil
1179
+ }
1180
+ if style .DecimalPlaces != nil {
1181
+ fc = strings .ReplaceAll (fc , "0.00" , dp )
1182
+ }
1183
+ if style .NegRed {
1184
+ fc = fc + ";[Red]" + fc
1185
+ }
1186
+ return & xlsxNumFmt {NumFmtID : numFmtID , FormatCode : fc }
1187
+ }
1188
+
1147
1189
// GetDefaultFont provides the default font name currently set in the
1148
1190
// workbook. The spreadsheet generated by excelize default font is Calibri.
1149
1191
func (f * File ) GetDefaultFont () (string , error ) {
0 commit comments