Skip to content

Commit ac3dce0

Browse files
committed
- Resolve qax-os#711, update docs for the GetSheetIndex
- Update unit test
1 parent d192667 commit ac3dce0

File tree

5 files changed

+54
-27
lines changed

5 files changed

+54
-27
lines changed

calc.go

+16-21
Original file line numberDiff line numberDiff line change
@@ -700,19 +700,19 @@ func formulaCriteriaParser(exp string) (fc *formulaCriteria) {
700700
fc.Type, fc.Condition = criteriaEq, match[1]
701701
return
702702
}
703-
if match := regexp.MustCompile(`^<(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
703+
if match := regexp.MustCompile(`^<=(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
704704
fc.Type, fc.Condition = criteriaLe, match[1]
705705
return
706706
}
707-
if match := regexp.MustCompile(`^>(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
707+
if match := regexp.MustCompile(`^>=(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
708708
fc.Type, fc.Condition = criteriaGe, match[1]
709709
return
710710
}
711-
if match := regexp.MustCompile(`^<=(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
711+
if match := regexp.MustCompile(`^<(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
712712
fc.Type, fc.Condition = criteriaL, match[1]
713713
return
714714
}
715-
if match := regexp.MustCompile(`^>=(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
715+
if match := regexp.MustCompile(`^>(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
716716
fc.Type, fc.Condition = criteriaG, match[1]
717717
return
718718
}
@@ -732,8 +732,11 @@ func formulaCriteriaParser(exp string) (fc *formulaCriteria) {
732732
// formulaCriteriaEval evaluate formula criteria expression.
733733
func formulaCriteriaEval(val string, criteria *formulaCriteria) (result bool, err error) {
734734
var value, expected float64
735+
var e error
735736
var prepareValue = func(val, cond string) (value float64, expected float64, err error) {
736-
value, _ = strconv.ParseFloat(val, 64)
737+
if value, err = strconv.ParseFloat(val, 64); err != nil {
738+
return
739+
}
737740
if expected, err = strconv.ParseFloat(criteria.Condition, 64); err != nil {
738741
return
739742
}
@@ -743,25 +746,17 @@ func formulaCriteriaEval(val string, criteria *formulaCriteria) (result bool, er
743746
case criteriaEq:
744747
return val == criteria.Condition, err
745748
case criteriaLe:
746-
if value, expected, err = prepareValue(val, criteria.Condition); err != nil {
747-
return
748-
}
749-
return value <= expected, err
749+
value, expected, e = prepareValue(val, criteria.Condition)
750+
return value <= expected && e == nil, err
750751
case criteriaGe:
751-
if value, expected, err = prepareValue(val, criteria.Condition); err != nil {
752-
return
753-
}
754-
return value >= expected, err
752+
value, expected, e = prepareValue(val, criteria.Condition)
753+
return value >= expected && e == nil, err
755754
case criteriaL:
756-
if value, expected, err = prepareValue(val, criteria.Condition); err != nil {
757-
return
758-
}
759-
return value < expected, err
755+
value, expected, e = prepareValue(val, criteria.Condition)
756+
return value < expected && e == nil, err
760757
case criteriaG:
761-
if value, expected, err = prepareValue(val, criteria.Condition); err != nil {
762-
return
763-
}
764-
return value > expected, err
758+
value, expected, e = prepareValue(val, criteria.Condition)
759+
return value > expected && e == nil, err
765760
case criteriaBeg:
766761
return strings.HasPrefix(val, criteria.Condition), err
767762
case criteriaEnd:

calc_test.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ func TestCalcCellValue(t *testing.T) {
339339
"=SINH(0.5)": "0.5210953054937474",
340340
"=SINH(-2)": "-3.626860407847019",
341341
// SQRT
342-
"=SQRT(4)": "2",
342+
"=SQRT(4)": "2",
343+
`=SQRT("")`: "0",
343344
// SQRTPI
344345
"=SQRTPI(5)": "3.963327297606011",
345346
"=SQRTPI(0.2)": "0.7926654595212022",
@@ -361,7 +362,15 @@ func TestCalcCellValue(t *testing.T) {
361362
"=1+SUM(SUM(1,2*3),4)*-4/2+5+(4+2)*3": "2",
362363
"=1+SUM(SUM(1,2*3),4)*4/3+5+(4+2)*3": "38.666666666666664",
363364
// SUMIF
365+
`=SUMIF(F1:F5, "")`: "0",
366+
`=SUMIF(A1:A5, "3")`: "3",
367+
`=SUMIF(F1:F5, "=36693")`: "36693",
368+
`=SUMIF(F1:F5, "<100")`: "0",
369+
`=SUMIF(F1:F5, "<=36693")`: "93233",
364370
`=SUMIF(F1:F5, ">100")`: "146554",
371+
`=SUMIF(F1:F5, ">=100")`: "146554",
372+
`=SUMIF(F1:F5, ">=text")`: "0",
373+
`=SUMIF(F1:F5, "*Jan",F2:F5)`: "0",
365374
`=SUMIF(D3:D7,"Jan",F2:F5)`: "112114",
366375
`=SUMIF(D2:D9,"Feb",F2:F9)`: "157559",
367376
`=SUMIF(E2:E9,"North 1",F2:F9)`: "66582",
@@ -706,15 +715,17 @@ func TestCalcCellValue(t *testing.T) {
706715
// ISERROR
707716
"=ISERROR()": "ISERROR requires 1 argument",
708717
// ISEVEN
709-
"=ISEVEN()": "ISEVEN requires 1 argument",
718+
"=ISEVEN()": "ISEVEN requires 1 argument",
719+
`=ISEVEN("text")`: "#VALUE!",
710720
// ISNA
711721
"=ISNA()": "ISNA requires 1 argument",
712722
// ISNONTEXT
713723
"=ISNONTEXT()": "ISNONTEXT requires 1 argument",
714724
// ISNUMBER
715725
"=ISNUMBER()": "ISNUMBER requires 1 argument",
716726
// ISODD
717-
"=ISODD()": "ISODD requires 1 argument",
727+
"=ISODD()": "ISODD requires 1 argument",
728+
`=ISODD("text")`: "#VALUE!",
718729
// NA
719730
"=NA(1)": "NA accepts no arguments",
720731
}
@@ -817,3 +828,12 @@ func TestCalcCellValueWithDefinedName(t *testing.T) {
817828
// DefinedName with scope WorkSheet takes precedence over DefinedName with scope Workbook, so we should get B1 value
818829
assert.Equal(t, "B1 value", result, "=defined_name1")
819830
}
831+
832+
func TestDet(t *testing.T) {
833+
assert.Equal(t, det([][]float64{
834+
{1, 2, 3, 4},
835+
{2, 3, 4, 5},
836+
{3, 4, 5, 6},
837+
{4, 5, 6, 7},
838+
}), float64(0))
839+
}

crypt_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,15 @@ func TestEncrypt(t *testing.T) {
2121
assert.NoError(t, err)
2222
assert.EqualError(t, f.SaveAs(filepath.Join("test", "BadEncrypt.xlsx"), Options{Password: "password"}), "not support encryption currently")
2323
}
24+
25+
func TestEncryptionMechanism(t *testing.T) {
26+
mechanism, err := encryptionMechanism([]byte{3, 0, 3, 0})
27+
assert.Equal(t, mechanism, "extensible")
28+
assert.EqualError(t, err, "unsupport encryption mechanism")
29+
_, err = encryptionMechanism([]byte{})
30+
assert.EqualError(t, err, "unknown encryption mechanism")
31+
}
32+
33+
func TestHashing(t *testing.T) {
34+
assert.Equal(t, hashing("unsupportHashAlgorithm", []byte{}), []uint8([]byte(nil)))
35+
}

rows_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ func TestGetValueFromNumber(t *testing.T) {
835835
assert.Equal(t, "2.22", val)
836836

837837
c = &xlsxC{T: "n", V: "2.220000ddsf0000000002-r"}
838-
val, err = c.getValueFrom(f, d)
838+
_, err = c.getValueFrom(f, d)
839839
assert.NotNil(t, err)
840840
assert.Equal(t, "strconv.ParseFloat: parsing \"2.220000ddsf0000000002-r\": invalid syntax", err.Error())
841841
}

sheet.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ func (f *File) getSheetID(name string) int {
360360
}
361361

362362
// GetSheetIndex provides a function to get a sheet index of the workbook by
363-
// the given sheet name. If the given sheet name is invalid, it will return an
364-
// integer type value 0.
363+
// the given sheet name. If the given sheet name is invalid or sheet doesn't
364+
// exist, it will return an integer type value -1.
365365
func (f *File) GetSheetIndex(name string) int {
366366
var idx = -1
367367
for index, sheet := range f.GetSheetList() {

0 commit comments

Comments
 (0)