浏览代码

- Resolve #711, update docs for the GetSheetIndex
- Update unit test

xuri 5 年之前
父节点
当前提交
ac3dce0bea
共有 5 个文件被更改,包括 54 次插入27 次删除
  1. 16 21
      calc.go
  2. 23 3
      calc_test.go
  3. 12 0
      crypt_test.go
  4. 1 1
      rows_test.go
  5. 2 2
      sheet.go

+ 16 - 21
calc.go

@@ -700,19 +700,19 @@ func formulaCriteriaParser(exp string) (fc *formulaCriteria) {
 		fc.Type, fc.Condition = criteriaEq, match[1]
 		return
 	}
-	if match := regexp.MustCompile(`^<(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
+	if match := regexp.MustCompile(`^<=(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
 		fc.Type, fc.Condition = criteriaLe, match[1]
 		return
 	}
-	if match := regexp.MustCompile(`^>(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
+	if match := regexp.MustCompile(`^>=(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
 		fc.Type, fc.Condition = criteriaGe, match[1]
 		return
 	}
-	if match := regexp.MustCompile(`^<=(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
+	if match := regexp.MustCompile(`^<(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
 		fc.Type, fc.Condition = criteriaL, match[1]
 		return
 	}
-	if match := regexp.MustCompile(`^>=(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
+	if match := regexp.MustCompile(`^>(.*)$`).FindStringSubmatch(exp); len(match) > 1 {
 		fc.Type, fc.Condition = criteriaG, match[1]
 		return
 	}
@@ -732,8 +732,11 @@ func formulaCriteriaParser(exp string) (fc *formulaCriteria) {
 // formulaCriteriaEval evaluate formula criteria expression.
 func formulaCriteriaEval(val string, criteria *formulaCriteria) (result bool, err error) {
 	var value, expected float64
+	var e error
 	var prepareValue = func(val, cond string) (value float64, expected float64, err error) {
-		value, _ = strconv.ParseFloat(val, 64)
+		if value, err = strconv.ParseFloat(val, 64); err != nil {
+			return
+		}
 		if expected, err = strconv.ParseFloat(criteria.Condition, 64); err != nil {
 			return
 		}
@@ -743,25 +746,17 @@ func formulaCriteriaEval(val string, criteria *formulaCriteria) (result bool, er
 	case criteriaEq:
 		return val == criteria.Condition, err
 	case criteriaLe:
-		if value, expected, err = prepareValue(val, criteria.Condition); err != nil {
-			return
-		}
-		return value <= expected, err
+		value, expected, e = prepareValue(val, criteria.Condition)
+		return value <= expected && e == nil, err
 	case criteriaGe:
-		if value, expected, err = prepareValue(val, criteria.Condition); err != nil {
-			return
-		}
-		return value >= expected, err
+		value, expected, e = prepareValue(val, criteria.Condition)
+		return value >= expected && e == nil, err
 	case criteriaL:
-		if value, expected, err = prepareValue(val, criteria.Condition); err != nil {
-			return
-		}
-		return value < expected, err
+		value, expected, e = prepareValue(val, criteria.Condition)
+		return value < expected && e == nil, err
 	case criteriaG:
-		if value, expected, err = prepareValue(val, criteria.Condition); err != nil {
-			return
-		}
-		return value > expected, err
+		value, expected, e = prepareValue(val, criteria.Condition)
+		return value > expected && e == nil, err
 	case criteriaBeg:
 		return strings.HasPrefix(val, criteria.Condition), err
 	case criteriaEnd:

+ 23 - 3
calc_test.go

@@ -339,7 +339,8 @@ func TestCalcCellValue(t *testing.T) {
 		"=SINH(0.5)": "0.5210953054937474",
 		"=SINH(-2)":  "-3.626860407847019",
 		// SQRT
-		"=SQRT(4)": "2",
+		"=SQRT(4)":  "2",
+		`=SQRT("")`: "0",
 		// SQRTPI
 		"=SQRTPI(5)":   "3.963327297606011",
 		"=SQRTPI(0.2)": "0.7926654595212022",
@@ -361,7 +362,15 @@ func TestCalcCellValue(t *testing.T) {
 		"=1+SUM(SUM(1,2*3),4)*-4/2+5+(4+2)*3": "2",
 		"=1+SUM(SUM(1,2*3),4)*4/3+5+(4+2)*3":  "38.666666666666664",
 		// SUMIF
+		`=SUMIF(F1:F5, "")`:             "0",
+		`=SUMIF(A1:A5, "3")`:            "3",
+		`=SUMIF(F1:F5, "=36693")`:       "36693",
+		`=SUMIF(F1:F5, "<100")`:         "0",
+		`=SUMIF(F1:F5, "<=36693")`:      "93233",
 		`=SUMIF(F1:F5, ">100")`:         "146554",
+		`=SUMIF(F1:F5, ">=100")`:        "146554",
+		`=SUMIF(F1:F5, ">=text")`:       "0",
+		`=SUMIF(F1:F5, "*Jan",F2:F5)`:   "0",
 		`=SUMIF(D3:D7,"Jan",F2:F5)`:     "112114",
 		`=SUMIF(D2:D9,"Feb",F2:F9)`:     "157559",
 		`=SUMIF(E2:E9,"North 1",F2:F9)`: "66582",
@@ -706,7 +715,8 @@ func TestCalcCellValue(t *testing.T) {
 		// ISERROR
 		"=ISERROR()": "ISERROR requires 1 argument",
 		// ISEVEN
-		"=ISEVEN()": "ISEVEN requires 1 argument",
+		"=ISEVEN()":       "ISEVEN requires 1 argument",
+		`=ISEVEN("text")`: "#VALUE!",
 		// ISNA
 		"=ISNA()": "ISNA requires 1 argument",
 		// ISNONTEXT
@@ -714,7 +724,8 @@ func TestCalcCellValue(t *testing.T) {
 		// ISNUMBER
 		"=ISNUMBER()": "ISNUMBER requires 1 argument",
 		// ISODD
-		"=ISODD()": "ISODD requires 1 argument",
+		"=ISODD()":       "ISODD requires 1 argument",
+		`=ISODD("text")`: "#VALUE!",
 		// NA
 		"=NA(1)": "NA accepts no arguments",
 	}
@@ -817,3 +828,12 @@ func TestCalcCellValueWithDefinedName(t *testing.T) {
 	// DefinedName with scope WorkSheet takes precedence over DefinedName with scope Workbook, so we should get B1 value
 	assert.Equal(t, "B1 value", result, "=defined_name1")
 }
+
+func TestDet(t *testing.T) {
+	assert.Equal(t, det([][]float64{
+		{1, 2, 3, 4},
+		{2, 3, 4, 5},
+		{3, 4, 5, 6},
+		{4, 5, 6, 7},
+	}), float64(0))
+}

+ 12 - 0
crypt_test.go

@@ -21,3 +21,15 @@ func TestEncrypt(t *testing.T) {
 	assert.NoError(t, err)
 	assert.EqualError(t, f.SaveAs(filepath.Join("test", "BadEncrypt.xlsx"), Options{Password: "password"}), "not support encryption currently")
 }
+
+func TestEncryptionMechanism(t *testing.T) {
+	mechanism, err := encryptionMechanism([]byte{3, 0, 3, 0})
+	assert.Equal(t, mechanism, "extensible")
+	assert.EqualError(t, err, "unsupport encryption mechanism")
+	_, err = encryptionMechanism([]byte{})
+	assert.EqualError(t, err, "unknown encryption mechanism")
+}
+
+func TestHashing(t *testing.T) {
+	assert.Equal(t, hashing("unsupportHashAlgorithm", []byte{}), []uint8([]byte(nil)))
+}

+ 1 - 1
rows_test.go

@@ -835,7 +835,7 @@ func TestGetValueFromNumber(t *testing.T) {
 	assert.Equal(t, "2.22", val)
 
 	c = &xlsxC{T: "n", V: "2.220000ddsf0000000002-r"}
-	val, err = c.getValueFrom(f, d)
+	_, err = c.getValueFrom(f, d)
 	assert.NotNil(t, err)
 	assert.Equal(t, "strconv.ParseFloat: parsing \"2.220000ddsf0000000002-r\": invalid syntax", err.Error())
 }

+ 2 - 2
sheet.go

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