Author: yegor Date: Thu Feb 21 11:00:40 2019 New Revision: 1854037 URL: http://svn.apache.org/viewvc?rev=1854037&view=rev Log: Bug 60980: Fix parsing formulas with intersections in functions args
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/FormulaParser.java poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java Modified: poi/trunk/src/java/org/apache/poi/ss/formula/FormulaParser.java URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/FormulaParser.java?rev=1854037&r1=1854036&r2=1854037&view=diff ============================================================================== --- poi/trunk/src/java/org/apache/poi/ss/formula/FormulaParser.java (original) +++ poi/trunk/src/java/org/apache/poi/ss/formula/FormulaParser.java Thu Feb 21 11:00:40 2019 @@ -939,7 +939,7 @@ public final class FormulaParser { GetChar(); } SkipWhite(); - + return sb.toString(); } @@ -1476,7 +1476,7 @@ public final class FormulaParser { missedPrevArg = true; continue; } - temp.add(comparisonExpression()); + temp.add(intersectionExpression()); missedPrevArg = false; SkipWhite(); if (!isArgumentDelimiter(look)) { Modified: poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java?rev=1854037&r1=1854036&r2=1854037&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java Thu Feb 21 11:00:40 2019 @@ -1279,7 +1279,42 @@ public final class TestFormulaParser { ParenthesisPtg.class ); } - + + // https://bz.apache.org/bugzilla/show_bug.cgi?id=60980 + @Test + public void testIntersectionInFunctionArgs() { + confirmTokenClasses("SUM(A1:B2 B2:C3)", + MemAreaPtg.class, + AreaPtg.class, + AreaPtg.class, + IntersectionPtg.class, + AttrPtg.class + ); + } + + @Test + public void testIntersectionNamesInFunctionArgs() { + HSSFWorkbook wb = new HSSFWorkbook(); + + HSSFName name1 = wb.createName(); + name1.setNameName("foo1"); + name1.setRefersToFormula("A1:A3"); + + HSSFName name2 = wb.createName(); + name2.setNameName("foo2"); + name2.setRefersToFormula("A1:B3"); + + Ptg[] ptgs = FormulaParser.parse("SUM(foo1 foo2)", HSSFEvaluationWorkbook.create(wb), FormulaType.CELL, -1); + + confirmTokenClasses(ptgs, + MemFuncPtg.class, + NamePtg.class, + NamePtg.class, + IntersectionPtg.class, + AttrPtg.class + ); + } + @Test public void testRange_bug46643() throws IOException { String formula = "Sheet1!A1:Sheet1!B3"; Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java?rev=1854037&r1=1854036&r2=1854037&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java (original) +++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestFormulaEvaluator.java Thu Feb 21 11:00:40 2019 @@ -691,4 +691,42 @@ public abstract class BaseTestFormulaEva assertEquals(formula, a2.getCellFormula()); } } + + // setting an evaluation of function arguments with the intersect operator (space) + // see https://bz.apache.org/bugzilla/show_bug.cgi?id=60980 + @Test + public void testIntersectionInFunctionArgs_60980() throws IOException { + Workbook wb = _testDataProvider.createWorkbook(); + FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator(); + + + Name n1 = wb.createName(); + n1.setNameName("foo"); + n1.setRefersToFormula("A4:B5"); + Name n2 = wb.createName(); + n2.setNameName("bar"); + n2.setRefersToFormula("B4:C5"); + + Sheet sheet = wb.createSheet(); + Row row3 = sheet.createRow(3); + row3.createCell(0).setCellValue(1); + row3.createCell(1).setCellValue(2); + row3.createCell(2).setCellValue(3); + Row row4 = sheet.createRow(4); + row4.createCell(0).setCellValue(4); + row4.createCell(1).setCellValue(5); + row4.createCell(2).setCellValue(6); + + Cell fmla1 = row3.createCell(4); + fmla1.setCellFormula("SUM(A4:B5 B4:C5)"); + fe.evaluateFormulaCell(fmla1); + assertEquals(7, fmla1.getNumericCellValue(), 0.000); + + Cell fmla2 = row3.createCell(5); + fmla2.setCellFormula("SUM(foo bar)"); + fe.evaluateFormulaCell(fmla2); + assertEquals(7, fmla2.getNumericCellValue(), 0.000); + + wb.close(); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org For additional commands, e-mail: commits-h...@poi.apache.org