@@ -691,26 +691,46 @@ func (f *File) DuplicateRowTo(sheet string, row, row2 int) error {
691
691
return err
692
692
}
693
693
694
+ // duplicateSQRefHelper provides a function to adjust conditional formatting and
695
+ // data validations cell reference when duplicate rows.
696
+ func duplicateSQRefHelper (row , row2 int , ref string ) (string , error ) {
697
+ if ! strings .Contains (ref , ":" ) {
698
+ ref += ":" + ref
699
+ }
700
+ abs := strings .Contains (ref , "$" )
701
+ coordinates , err := rangeRefToCoordinates (ref )
702
+ if err != nil {
703
+ return "" , err
704
+ }
705
+ x1 , y1 , x2 , y2 := coordinates [0 ], coordinates [1 ], coordinates [2 ], coordinates [3 ]
706
+ if y1 == y2 && y1 == row {
707
+ if ref , err = coordinatesToRangeRef ([]int {x1 , row2 , x2 , row2 }, abs ); err != nil {
708
+ return "" , err
709
+ }
710
+ return ref , err
711
+ }
712
+ return "" , err
713
+ }
714
+
694
715
// duplicateConditionalFormat create conditional formatting for the destination
695
716
// row if there are conditional formats in the copied row.
696
717
func (f * File ) duplicateConditionalFormat (ws * xlsxWorksheet , sheet string , row , row2 int ) error {
697
718
var cfs []* xlsxConditionalFormatting
698
719
for _ , cf := range ws .ConditionalFormatting {
699
720
if cf != nil {
700
- if ! strings .Contains (cf .SQRef , ":" ) {
701
- cf .SQRef += ":" + cf .SQRef
702
- }
703
- abs := strings .Contains (cf .SQRef , "$" )
704
- coordinates , err := rangeRefToCoordinates (cf .SQRef )
705
- if err != nil {
706
- return err
707
- }
708
- x1 , y1 , x2 , y2 := coordinates [0 ], coordinates [1 ], coordinates [2 ], coordinates [3 ]
709
- if y1 == y2 && y1 == row {
710
- cfCopy := deepcopy .Copy (* cf ).(xlsxConditionalFormatting )
711
- if cfCopy .SQRef , err = coordinatesToRangeRef ([]int {x1 , row2 , x2 , row2 }, abs ); err != nil {
721
+ var SQRef []string
722
+ for _ , ref := range strings .Split (cf .SQRef , " " ) {
723
+ coordinates , err := duplicateSQRefHelper (row , row2 , ref )
724
+ if err != nil {
712
725
return err
713
726
}
727
+ if coordinates != "" {
728
+ SQRef = append (SQRef , coordinates )
729
+ }
730
+ }
731
+ if len (SQRef ) > 0 {
732
+ cfCopy := deepcopy .Copy (* cf ).(xlsxConditionalFormatting )
733
+ cfCopy .SQRef = strings .Join (SQRef , " " )
714
734
cfs = append (cfs , & cfCopy )
715
735
}
716
736
}
@@ -728,20 +748,19 @@ func (f *File) duplicateDataValidations(ws *xlsxWorksheet, sheet string, row, ro
728
748
var dvs []* xlsxDataValidation
729
749
for _ , dv := range ws .DataValidations .DataValidation {
730
750
if dv != nil {
731
- if ! strings .Contains (dv .Sqref , ":" ) {
732
- dv .Sqref += ":" + dv .Sqref
733
- }
734
- abs := strings .Contains (dv .Sqref , "$" )
735
- coordinates , err := rangeRefToCoordinates (dv .Sqref )
736
- if err != nil {
737
- return err
738
- }
739
- x1 , y1 , x2 , y2 := coordinates [0 ], coordinates [1 ], coordinates [2 ], coordinates [3 ]
740
- if y1 == y2 && y1 == row {
741
- dvCopy := deepcopy .Copy (* dv ).(xlsxDataValidation )
742
- if dvCopy .Sqref , err = coordinatesToRangeRef ([]int {x1 , row2 , x2 , row2 }, abs ); err != nil {
751
+ var SQRef []string
752
+ for _ , ref := range strings .Split (dv .Sqref , " " ) {
753
+ coordinates , err := duplicateSQRefHelper (row , row2 , ref )
754
+ if err != nil {
743
755
return err
744
756
}
757
+ if coordinates != "" {
758
+ SQRef = append (SQRef , coordinates )
759
+ }
760
+ }
761
+ if len (SQRef ) > 0 {
762
+ dvCopy := deepcopy .Copy (* dv ).(xlsxDataValidation )
763
+ dvCopy .Sqref = strings .Join (SQRef , " " )
745
764
dvs = append (dvs , & dvCopy )
746
765
}
747
766
}
0 commit comments