Skip to content

Commit f8487a6

Browse files
jianxinhouhoujianxin.rupert
and
houjianxin.rupert
authored
This closes qax-os#1879, compatible with the escaped quote symbol in none formula data validation rules (qax-os#1880)
- Update dependencies module to fix vulnerabilities - Update unit tests Co-authored-by: houjianxin.rupert <[email protected]>
1 parent 3e636ae commit f8487a6

File tree

5 files changed

+36
-11
lines changed

5 files changed

+36
-11
lines changed

adjust.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -989,14 +989,14 @@ func (f *File) adjustDataValidations(ws *xlsxWorksheet, sheet string, dir adjust
989989
}
990990
worksheet.DataValidations.DataValidation[i].Sqref = ref
991991
}
992-
if worksheet.DataValidations.DataValidation[i].Formula1 != nil {
992+
if worksheet.DataValidations.DataValidation[i].Formula1.isFormula() {
993993
formula := formulaUnescaper.Replace(worksheet.DataValidations.DataValidation[i].Formula1.Content)
994994
if formula, err = f.adjustFormulaRef(sheet, sheetN, formula, false, dir, num, offset); err != nil {
995995
return err
996996
}
997997
worksheet.DataValidations.DataValidation[i].Formula1 = &xlsxInnerXML{Content: formulaEscaper.Replace(formula)}
998998
}
999-
if worksheet.DataValidations.DataValidation[i].Formula2 != nil {
999+
if worksheet.DataValidations.DataValidation[i].Formula2.isFormula() {
10001000
formula := formulaUnescaper.Replace(worksheet.DataValidations.DataValidation[i].Formula2.Content)
10011001
if formula, err = f.adjustFormulaRef(sheet, sheetN, formula, false, dir, num, offset); err != nil {
10021002
return err

adjust_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/xml"
55
"fmt"
66
"path/filepath"
7+
"strings"
78
"testing"
89

910
_ "image/jpeg"
@@ -1099,6 +1100,25 @@ func TestAdjustDataValidations(t *testing.T) {
10991100
f.Sheet.Delete("xl/worksheets/sheet1.xml")
11001101
f.Pkg.Store("xl/worksheets/sheet1.xml", MacintoshCyrillicCharset)
11011102
assert.EqualError(t, f.adjustDataValidations(nil, "Sheet1", columns, 0, 0, 1), "XML syntax error on line 1: invalid UTF-8")
1103+
1104+
t.Run("for_escaped_data_validation_rules_formula", func(t *testing.T) {
1105+
f := NewFile()
1106+
_, err := f.NewSheet("Sheet2")
1107+
assert.NoError(t, err)
1108+
dv := NewDataValidation(true)
1109+
dv.Sqref = "A1"
1110+
assert.NoError(t, dv.SetDropList([]string{"option1", strings.Repeat("\"", 4)}))
1111+
ws, ok := f.Sheet.Load("xl/worksheets/sheet1.xml")
1112+
assert.True(t, ok)
1113+
assert.NoError(t, f.AddDataValidation("Sheet1", dv))
1114+
// The double quote symbol in none formula data validation rules will be escaped in the Kingsoft WPS Office
1115+
formula := strings.ReplaceAll(fmt.Sprintf("\"option1, %s", strings.Repeat("\"", 9)), "\"", "&quot;")
1116+
ws.(*xlsxWorksheet).DataValidations.DataValidation[0].Formula1.Content = formula
1117+
f.RemoveCol("Sheet2", "A")
1118+
dvs, err := f.GetDataValidations("Sheet1")
1119+
assert.NoError(t, err)
1120+
assert.Equal(t, formula, dvs[0].Formula1)
1121+
})
11021122
}
11031123

11041124
func TestAdjustDrawings(t *testing.T) {

datavalidation.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ func squashSqref(cells [][]int) []string {
442442
return append(refs, ref)
443443
}
444444

445+
// isFormulaDataValidation returns whether the data validation rule is a formula.
446+
func (dv *xlsxInnerXML) isFormula() bool {
447+
return dv != nil && !(strings.HasPrefix(dv.Content, "&quot;") && strings.HasSuffix(dv.Content, "&quot;"))
448+
}
449+
445450
// unescapeDataValidationFormula returns unescaped data validation formula.
446451
func unescapeDataValidationFormula(val string) string {
447452
if strings.HasPrefix(val, "\"") { // Text detection

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ require (
66
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
77
github.com/richardlehane/mscfb v1.0.4
88
github.com/stretchr/testify v1.8.4
9-
github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53
9+
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d
1010
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7
11-
golang.org/x/crypto v0.21.0
11+
golang.org/x/crypto v0.22.0
1212
golang.org/x/image v0.14.0
13-
golang.org/x/net v0.22.0
13+
golang.org/x/net v0.24.0
1414
golang.org/x/text v0.14.0
1515
)
1616

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ github.com/richardlehane/msoleps v1.0.3 h1:aznSZzrwYRl3rLKRT3gUk9am7T/mLNSnJINvN
1111
github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
1212
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
1313
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
14-
github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 h1:Chd9DkqERQQuHpXjR/HSV1jLZA6uaoiwwH3vSuF3IW0=
15-
github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
14+
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
15+
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
1616
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
1717
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
18-
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
19-
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
18+
golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
19+
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
2020
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
2121
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
22-
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
23-
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
22+
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
23+
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
2424
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
2525
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
2626
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

0 commit comments

Comments
 (0)