Skip to content

Commit 055349d

Browse files
committed
Fix a v2.8.1 regression bug, error on duplicate rows, if conditional formatting or data validation has multiple cell range reference
- Update unit tests
1 parent f8487a6 commit 055349d

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

adjust_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ func TestAdjustDataValidations(t *testing.T) {
11141114
// The double quote symbol in none formula data validation rules will be escaped in the Kingsoft WPS Office
11151115
formula := strings.ReplaceAll(fmt.Sprintf("\"option1, %s", strings.Repeat("\"", 9)), "\"", """)
11161116
ws.(*xlsxWorksheet).DataValidations.DataValidation[0].Formula1.Content = formula
1117-
f.RemoveCol("Sheet2", "A")
1117+
assert.NoError(t, f.RemoveCol("Sheet2", "A"))
11181118
dvs, err := f.GetDataValidations("Sheet1")
11191119
assert.NoError(t, err)
11201120
assert.Equal(t, formula, dvs[0].Formula1)

cell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ func trimCellValue(value string, escape bool) (v string, ns xml.Attr) {
506506
if escape {
507507
var buf bytes.Buffer
508508
enc := xml.NewEncoder(&buf)
509-
enc.EncodeToken(xml.CharData(value))
509+
_ = enc.EncodeToken(xml.CharData(value))
510510
enc.Flush()
511511
value = buf.String()
512512
}

rows.go

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -691,26 +691,46 @@ func (f *File) DuplicateRowTo(sheet string, row, row2 int) error {
691691
return err
692692
}
693693

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+
694715
// duplicateConditionalFormat create conditional formatting for the destination
695716
// row if there are conditional formats in the copied row.
696717
func (f *File) duplicateConditionalFormat(ws *xlsxWorksheet, sheet string, row, row2 int) error {
697718
var cfs []*xlsxConditionalFormatting
698719
for _, cf := range ws.ConditionalFormatting {
699720
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 {
712725
return err
713726
}
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, " ")
714734
cfs = append(cfs, &cfCopy)
715735
}
716736
}
@@ -728,20 +748,19 @@ func (f *File) duplicateDataValidations(ws *xlsxWorksheet, sheet string, row, ro
728748
var dvs []*xlsxDataValidation
729749
for _, dv := range ws.DataValidations.DataValidation {
730750
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 {
743755
return err
744756
}
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, " ")
745764
dvs = append(dvs, &dvCopy)
746765
}
747766
}

0 commit comments

Comments
 (0)