@@ -3249,10 +3249,130 @@ func (f *File) UnsetConditionalFormat(sheet, rangeRef string) error {
32493249 for i , cf := range ws .ConditionalFormatting {
32503250 if cf .SQRef == rangeRef {
32513251 ws .ConditionalFormatting = append (ws .ConditionalFormatting [:i ], ws .ConditionalFormatting [i + 1 :]... )
3252+ }
3253+ }
3254+ if ws .ExtLst != nil {
3255+ var condFmtsBytes , extLstBytes []byte
3256+ decodeExtLst := new (decodeExtLst )
3257+ if err = f .xmlNewDecoder (strings .NewReader ("<extLst>" + ws .ExtLst .Ext + "</extLst>" )).
3258+ Decode (decodeExtLst ); err != nil && err != io .EOF {
32523259 return err
32533260 }
3261+ for idx := 0 ; idx < len (decodeExtLst .Ext ); idx ++ {
3262+ ext := decodeExtLst .Ext [idx ]
3263+ if ext .URI == ExtURIConditionalFormattings {
3264+ decodeCondFmts := new (decodeX14ConditionalFormattingRules )
3265+ _ = f .xmlNewDecoder (strings .NewReader (ext .Content )).Decode (decodeCondFmts )
3266+ condFmtsBytes , _ = decodeCondFmts .deleteCfRule (rangeRef )
3267+ decodeExtLst .Ext [idx ].Content = string (condFmtsBytes )
3268+ if len (decodeExtLst .Ext [idx ].Content ) == 57 { // empty x14:conditionalFormattings element
3269+ decodeExtLst .Ext = append (decodeExtLst .Ext [:idx ], decodeExtLst .Ext [idx + 1 :]... )
3270+ idx --
3271+ }
3272+ }
3273+ }
3274+ sort .Slice (decodeExtLst .Ext , func (i , j int ) bool {
3275+ return inStrSlice (worksheetExtURIPriority , decodeExtLst .Ext [i ].URI , false ) <
3276+ inStrSlice (worksheetExtURIPriority , decodeExtLst .Ext [j ].URI , false )
3277+ })
3278+ extLstBytes , err = xml .Marshal (decodeExtLst )
3279+ ws .ExtLst = & xlsxExtLst {Ext : strings .TrimSuffix (strings .TrimPrefix (string (extLstBytes ), "<extLst>" ), "</extLst>" )}
3280+ if len (ws .ExtLst .Ext ) == 0 {
3281+ ws .ExtLst = nil
3282+ }
32543283 }
3255- return nil
3284+ return err
3285+ }
3286+
3287+ // deleteCfRule provides a function to delete conditional formatting rule by
3288+ // given range reference and return the updated x14:conditionalFormattings
3289+ // element content.
3290+ func (r * decodeX14ConditionalFormattingRules ) deleteCfRule (rangeRef string ) ([]byte , error ) {
3291+ condFmts := & xlsxX14ConditionalFormattings {}
3292+ for _ , condFmt := range r .CondFmt {
3293+ if condFmt .Sqref == rangeRef {
3294+ continue
3295+ }
3296+ x14ConditionalFormatting := xlsxX14ConditionalFormatting {
3297+ XMLNSXM : NameSpaceSpreadSheetExcel2006Main .Value ,
3298+ Pivot : condFmt .Pivot ,
3299+ Sqref : condFmt .Sqref ,
3300+ ExtLst : condFmt .ExtLst ,
3301+ }
3302+ for _ , rule := range condFmt .CfRule {
3303+ x14CfRule := & xlsxX14CfRule {
3304+ Type : rule .Type ,
3305+ Priority : rule .Priority ,
3306+ StopIfTrue : rule .StopIfTrue ,
3307+ AboveAverage : rule .AboveAverage ,
3308+ Percent : rule .Percent ,
3309+ Bottom : rule .Bottom ,
3310+ Operator : rule .Operator ,
3311+ Text : rule .Text ,
3312+ TimePeriod : rule .TimePeriod ,
3313+ Rank : rule .Rank ,
3314+ StdDev : rule .StdDev ,
3315+ EqualAverage : rule .EqualAverage ,
3316+ ActivePresent : rule .ActivePresent ,
3317+ ID : rule .ID ,
3318+ F : rule .F ,
3319+ ColorScale : rule .ColorScale ,
3320+ Dxf : rule .Dxf ,
3321+ ExtLst : rule .ExtLst ,
3322+ }
3323+ if rule .DataBar != nil {
3324+ x14DataBar := & xlsx14DataBar {
3325+ MaxLength : rule .DataBar .MaxLength ,
3326+ MinLength : rule .DataBar .MinLength ,
3327+ Border : rule .DataBar .Border ,
3328+ ShowValue : rule .DataBar .ShowValue ,
3329+ Direction : rule .DataBar .Direction ,
3330+ BorderColor : rule .DataBar .BorderColor ,
3331+ NegativeFillColor : rule .DataBar .NegativeFillColor ,
3332+ AxisColor : rule .DataBar .AxisColor ,
3333+ }
3334+ if rule .DataBar .Gradient != nil {
3335+ x14DataBar .Gradient = * rule .DataBar .Gradient
3336+ }
3337+ if rule .DataBar .Cfvo != nil {
3338+ for _ , cfvo := range rule .DataBar .Cfvo {
3339+ x14DataBar .Cfvo = append (x14DataBar .Cfvo , & xlsxCfvo {
3340+ Gte : cfvo .Gte ,
3341+ Type : cfvo .Type ,
3342+ Val : cfvo .Val ,
3343+ ExtLst : cfvo .ExtLst ,
3344+ })
3345+ }
3346+ }
3347+ x14CfRule .DataBar = x14DataBar
3348+ }
3349+ if rule .IconSet != nil {
3350+ x14IconSet := & xlsx14IconSet {
3351+ IconSet : rule .IconSet .IconSet ,
3352+ ShowValue : rule .IconSet .ShowValue ,
3353+ Percent : rule .IconSet .Percent ,
3354+ Reverse : rule .IconSet .Reverse ,
3355+ Custom : rule .IconSet .Custom ,
3356+ CfIcon : rule .IconSet .CfIcon ,
3357+ }
3358+ if rule .IconSet .Cfvo != nil {
3359+ for _ , cfvo := range rule .IconSet .Cfvo {
3360+ x14IconSet .Cfvo = append (x14IconSet .Cfvo , & xlsx14Cfvo {
3361+ Type : cfvo .Type ,
3362+ Gte : cfvo .Gte ,
3363+ F : cfvo .F ,
3364+ ExtLst : cfvo .ExtLst ,
3365+ })
3366+ }
3367+ }
3368+ x14CfRule .IconSet = x14IconSet
3369+ }
3370+ x14ConditionalFormatting .CfRule = append (x14ConditionalFormatting .CfRule , x14CfRule )
3371+ }
3372+ condFmtBytes , _ := xml .Marshal (x14ConditionalFormatting )
3373+ condFmts .Content += string (condFmtBytes )
3374+ }
3375+ return xml .Marshal (condFmts )
32563376}
32573377
32583378// drawCondFmtCellIs provides a function to create conditional formatting rule
0 commit comments