@@ -2760,13 +2760,9 @@ func (f *File) SetConditionalFormat(sheet, rangeRef string, opts []ConditionalFo
2760
2760
if err != nil {
2761
2761
return err
2762
2762
}
2763
- if strings .Contains (rangeRef , ":" ) {
2764
- rect , err := rangeRefToCoordinates (rangeRef )
2765
- if err != nil {
2766
- return err
2767
- }
2768
- _ = sortCoordinates (rect )
2769
- rangeRef , _ = f .coordinatesToRangeRef (rect , strings .Contains (rangeRef , "$" ))
2763
+ SQRef , mastCell , err := prepareConditionalFormatRange (rangeRef )
2764
+ if err != nil {
2765
+ return err
2770
2766
}
2771
2767
// Create a pseudo GUID for each unique rule.
2772
2768
var rules int
@@ -2796,7 +2792,7 @@ func (f *File) SetConditionalFormat(sheet, rangeRef string, opts []ConditionalFo
2796
2792
drawFunc , ok := drawContFmtFunc [vt ]
2797
2793
if ok {
2798
2794
priority := rules + i
2799
- rule , x14rule := drawFunc (priority , ct , strings . Split ( rangeRef , ":" )[ 0 ] ,
2795
+ rule , x14rule := drawFunc (priority , ct , mastCell ,
2800
2796
fmt .Sprintf ("{00000000-0000-0000-%04X-%012X}" , f .getSheetID (sheet ), priority ), & opt )
2801
2797
if rule == nil {
2802
2798
return ErrParameterInvalid
@@ -2817,12 +2813,53 @@ func (f *File) SetConditionalFormat(sheet, rangeRef string, opts []ConditionalFo
2817
2813
}
2818
2814
2819
2815
ws .ConditionalFormatting = append (ws .ConditionalFormatting , & xlsxConditionalFormatting {
2820
- SQRef : rangeRef ,
2816
+ SQRef : SQRef ,
2821
2817
CfRule : cfRule ,
2822
2818
})
2823
2819
return err
2824
2820
}
2825
2821
2822
+ // prepareConditionalFormatRange returns checked cell range and master cell
2823
+ // reference by giving conditional formatting range reference.
2824
+ func prepareConditionalFormatRange (rangeRef string ) (string , string , error ) {
2825
+ var SQRef , mastCell string
2826
+ if rangeRef == "" {
2827
+ return SQRef , mastCell , ErrParameterRequired
2828
+ }
2829
+ rangeRef = strings .ReplaceAll (rangeRef , "," , " " )
2830
+ for i , cellRange := range strings .Split (rangeRef , " " ) {
2831
+ var cellNames []string
2832
+ for j , ref := range strings .Split (cellRange , ":" ) {
2833
+ if j > 1 {
2834
+ return SQRef , mastCell , ErrParameterInvalid
2835
+ }
2836
+ cellRef , col , row , err := parseRef (ref )
2837
+ if err != nil {
2838
+ return SQRef , mastCell , err
2839
+ }
2840
+ var c , r int
2841
+ if col {
2842
+ if cellRef .Row = TotalRows ; j == 0 {
2843
+ cellRef .Row = 1
2844
+ }
2845
+ }
2846
+ if row {
2847
+ if cellRef .Col = MaxColumns ; j == 0 {
2848
+ cellRef .Col = 1
2849
+ }
2850
+ }
2851
+ c , r = cellRef .Col , cellRef .Row
2852
+ cellName , _ := CoordinatesToCellName (c , r )
2853
+ cellNames = append (cellNames , cellName )
2854
+ if i == 0 && j == 0 {
2855
+ mastCell = cellName
2856
+ }
2857
+ }
2858
+ SQRef += strings .Join (cellNames , ":" ) + " "
2859
+ }
2860
+ return strings .TrimSuffix (SQRef , " " ), mastCell , nil
2861
+ }
2862
+
2826
2863
// appendCfRule provides a function to append rules to conditional formatting.
2827
2864
func (f * File ) appendCfRule (ws * xlsxWorksheet , rule * xlsxX14CfRule ) error {
2828
2865
var (
0 commit comments