From 700af6a5298010dfaf56abdbb6aa66fa85c2784d Mon Sep 17 00:00:00 2001 From: xuri Date: Mon, 3 Jul 2023 00:05:26 +0800 Subject: [PATCH] This fixed #1564, apply all of its arguments that meet multiple criteria --- calc.go | 6 ++---- calc_test.go | 16 +++++++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/calc.go b/calc.go index 8e37bb97..f3892fca 100644 --- a/calc.go +++ b/calc.go @@ -7812,6 +7812,7 @@ func formulaIfsMatch(args []formulaArg) (cellRefs []cellRef) { } } } else { + match = []cellRef{} for _, ref := range cellRefs { value := matrix[ref.Row][ref.Col] if ok, _ := formulaCriteriaEval(value.Value(), criteria); ok { @@ -7819,9 +7820,6 @@ func formulaIfsMatch(args []formulaArg) (cellRefs []cellRef) { } } } - if len(match) == 0 { - return - } cellRefs = match[:] } return @@ -14397,7 +14395,7 @@ func (fn *formulaFuncs) MATCH(argsList *list.List) formulaArg { } switch lookupArrayArg.Type { case ArgMatrix: - if len(lookupArrayArg.Matrix[0]) != 1 { + if len(lookupArrayArg.Matrix) != 1 && len(lookupArrayArg.Matrix[0]) != 1 { return newErrorFormulaArg(formulaErrorNA, lookupArrayErr) } lookupArray = lookupArrayArg.ToList() diff --git a/calc_test.go b/calc_test.go index a706f3de..607289f2 100644 --- a/calc_test.go +++ b/calc_test.go @@ -3828,7 +3828,8 @@ func TestCalcCellValue(t *testing.T) { "=MATCH(0,A1:A1,0,0)": {"#VALUE!", "MATCH requires 1 or 2 arguments"}, "=MATCH(0,A1:A1,\"x\")": {"#VALUE!", "MATCH requires numeric match_type argument"}, "=MATCH(0,A1)": {"#N/A", "MATCH arguments lookup_array should be one-dimensional array"}, - "=MATCH(0,A1:B1)": {"#N/A", "MATCH arguments lookup_array should be one-dimensional array"}, + "=MATCH(0,A1:B2)": {"#N/A", "MATCH arguments lookup_array should be one-dimensional array"}, + "=MATCH(0,A1:B1)": {"#N/A", "#N/A"}, // TRANSPOSE "=TRANSPOSE()": {"#VALUE!", "TRANSPOSE requires 1 argument"}, // HYPERLINK @@ -5131,10 +5132,14 @@ func TestCalcSUMIFSAndAVERAGEIFS(t *testing.T) { } f := prepareCalcData(cellData) formulaList := map[string]string{ - "=AVERAGEIFS(D2:D13,A2:A13,1,B2:B13,\"North\")": "174000", - "=AVERAGEIFS(D2:D13,A2:A13,\">2\",C2:C13,\"Jeff\")": "285500", - "=SUMIFS(D2:D13,A2:A13,1,B2:B13,\"North\")": "348000", - "=SUMIFS(D2:D13,A2:A13,\">2\",C2:C13,\"Jeff\")": "571000", + "=AVERAGEIFS(D2:D13,A2:A13,1,B2:B13,\"North\")": "174000", + "=AVERAGEIFS(D2:D13,A2:A13,\">2\",C2:C13,\"Jeff\")": "285500", + "=SUMIFS(D2:D13,A2:A13,1,B2:B13,\"North\")": "348000", + "=SUMIFS(D2:D13,A2:A13,\">2\",C2:C13,\"Jeff\")": "571000", + "=SUMIFS(D2:D13,A2:A13,1,D2:D13,125000)": "125000", + "=SUMIFS(D2:D13,A2:A13,1,D2:D13,\">100000\",C2:C13,\"Chris\")": "125000", + "=SUMIFS(D2:D13,A2:A13,1,D2:D13,\"<40000\",C2:C13,\"Chris\")": "0", + "=SUMIFS(D2:D13,A2:A13,1,A2:A13,2)": "0", } for formula, expected := range formulaList { assert.NoError(t, f.SetCellFormula("Sheet1", "E1", formula)) @@ -5147,6 +5152,7 @@ func TestCalcSUMIFSAndAVERAGEIFS(t *testing.T) { "=AVERAGEIFS(H1,\"\")": {"#VALUE!", "AVERAGEIFS requires at least 3 arguments"}, "=AVERAGEIFS(H1,\"\",TRUE,1)": {"#N/A", "#N/A"}, "=AVERAGEIFS(H1,\"\",TRUE)": {"#DIV/0!", "AVERAGEIF divide by zero"}, + "=AVERAGEIFS(D2:D13,A2:A13,1,A2:A13,2)": {"#DIV/0!", "AVERAGEIF divide by zero"}, "=SUMIFS()": {"#VALUE!", "SUMIFS requires at least 3 arguments"}, "=SUMIFS(D2:D13,A2:A13,1,B2:B13)": {"#N/A", "#N/A"}, "=SUMIFS(D20:D23,A2:A13,\">2\",C2:C13,\"Jeff\")": {"#VALUE!", "#VALUE!"},