diff options
author | Nick Burch <nick@apache.org> | 2014-07-25 13:59:07 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2014-07-25 13:59:07 +0000 |
commit | f19982e2455b58556e362f6c96696600b672cbe0 (patch) | |
tree | d3739c8a224a2e025b063424b75a874bc4b22b85 /src/ooxml/testcases/org/apache/poi | |
parent | 846a1f84a2ee08a731f35f66086e04aebfaf56bf (diff) | |
download | poi-f19982e2455b58556e362f6c96696600b672cbe0.tar.gz poi-f19982e2455b58556e362f6c96696600b672cbe0.zip |
Areas can have multi-sheet references too, so add FormulaParser support to these as well
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1613437 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/testcases/org/apache/poi')
-rw-r--r-- | src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaEvaluation.java | 16 | ||||
-rw-r--r-- | src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaParser.java | 56 |
2 files changed, 62 insertions, 10 deletions
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaEvaluation.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaEvaluation.java index 657739f998..4e92fda697 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaEvaluation.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaEvaluation.java @@ -201,46 +201,46 @@ public final class TestXSSFFormulaEvaluation extends BaseTestFormulaEvaluator { Cell sumF = s1.getRow(2).getCell(0); assertNotNull(sumF); assertEquals("SUM(Sheet1:Sheet3!A1)", sumF.getCellFormula()); - assertEquals("66", evaluator.evaluate(sumF).formatAsString()); + assertEquals("66.0", evaluator.evaluate(sumF).formatAsString()); // Various Stats formulas on numbers Cell avgF = s1.getRow(2).getCell(1); assertNotNull(avgF); assertEquals("AVERAGE(Sheet1:Sheet3!A1)", avgF.getCellFormula()); - assertEquals("22", evaluator.evaluate(avgF).formatAsString()); + assertEquals("22.0", evaluator.evaluate(avgF).formatAsString()); Cell minF = s1.getRow(3).getCell(1); assertNotNull(minF); assertEquals("MIX(Sheet1:Sheet3!A$1)", minF.getCellFormula()); - assertEquals("11", evaluator.evaluate(minF).formatAsString()); + assertEquals("11.0", evaluator.evaluate(minF).formatAsString()); Cell maxF = s1.getRow(4).getCell(1); assertNotNull(maxF); assertEquals("MAX(Sheet1:Sheet3!A$1)", maxF.getCellFormula()); - assertEquals("33", evaluator.evaluate(maxF).formatAsString()); + assertEquals("33.0", evaluator.evaluate(maxF).formatAsString()); Cell countF = s1.getRow(5).getCell(1); assertNotNull(countF); assertEquals("COUNT(Sheet1:Sheet3!A$1)", countF.getCellFormula()); - assertEquals("3", evaluator.evaluate(countF).formatAsString()); + assertEquals("3.0", evaluator.evaluate(countF).formatAsString()); // Various CountAs on Strings Cell countA_1F = s1.getRow(2).getCell(2); assertNotNull(countA_1F); assertEquals("COUNTA(Sheet1:Sheet3!C1)", countA_1F.getCellFormula()); - assertEquals("3", evaluator.evaluate(countA_1F).formatAsString()); + assertEquals("3.0", evaluator.evaluate(countA_1F).formatAsString()); Cell countA_2F = s1.getRow(2).getCell(3); assertNotNull(countA_2F); assertEquals("COUNTA(Sheet1:Sheet3!D1)", countA_2F.getCellFormula()); - assertEquals("0", evaluator.evaluate(countA_2F).formatAsString()); + assertEquals("0.0", evaluator.evaluate(countA_2F).formatAsString()); Cell countA_3F = s1.getRow(2).getCell(4); assertNotNull(countA_3F); assertEquals("COUNTA(Sheet1:Sheet3!E1)", countA_3F.getCellFormula()); - assertEquals("3", evaluator.evaluate(countA_3F).formatAsString()); + assertEquals("3.0", evaluator.evaluate(countA_3F).formatAsString()); } } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaParser.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaParser.java index 65e269d546..c7c466cf65 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaParser.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFFormulaParser.java @@ -31,6 +31,7 @@ import org.apache.poi.ss.formula.FormulaParsingWorkbook; import org.apache.poi.ss.formula.FormulaRenderingWorkbook; import org.apache.poi.ss.formula.FormulaType; import org.apache.poi.ss.formula.WorkbookDependentFormula; +import org.apache.poi.ss.formula.ptg.Area3DPtg; import org.apache.poi.ss.formula.ptg.Area3DPxg; import org.apache.poi.ss.formula.ptg.AreaPtg; import org.apache.poi.ss.formula.ptg.AttrPtg; @@ -242,7 +243,9 @@ public final class TestXSSFFormulaParser { /** * A handful of functions (such as SUM, COUNTA, MIN) support * multi-sheet references (eg Sheet1:Sheet3!A1 = Cell A1 from - * Sheets 1 through Sheet 3). + * Sheets 1 through Sheet 3) and multi-sheet area references + * (eg Sheet1:Sheet3!A1:B2 = Cells A1 through B2 from Sheets + * 1 through Sheet 3). * This test, based on common test files for HSSF and XSSF, checks * that we can read and parse these kinds of references * (but not evaluate - that's elsewhere in the test suite) @@ -274,6 +277,24 @@ public final class TestXSSFFormulaParser { assertNotNull(maxF); assertEquals("MAX(Sheet1:Sheet3!A$1)", maxF.getCellFormula()); + + Cell sumFA = s1.getRow(2).getCell(7); + assertNotNull(sumFA); + assertEquals("SUM(Sheet1:Sheet3!A1:B2)", sumFA.getCellFormula()); + + Cell avgFA = s1.getRow(2).getCell(8); + assertNotNull(avgFA); + assertEquals("AVERAGE(Sheet1:Sheet3!A1:B2)", avgFA.getCellFormula()); + + Cell maxFA = s1.getRow(4).getCell(8); + assertNotNull(maxFA); + assertEquals("MAX(Sheet1:Sheet3!A$1:B$2)", maxFA.getCellFormula()); + + Cell countFA = s1.getRow(5).getCell(8); + assertNotNull(countFA); + assertEquals("COUNT(Sheet1:Sheet3!$A$1:$B$2)", countFA.getCellFormula()); + + // Create a formula parser FormulaParsingWorkbook fpb = null; if (wb instanceof HSSFWorkbook) @@ -324,10 +345,41 @@ public final class TestXSSFFormulaParser { assertEquals("MIN", toFormulaString(ptgs[1], fpb)); - // Check we can round-trip - try to set a new one to a new cell + // SUM to a range of cells over 3 workbooks + ptgs = parse(fpb, "SUM(Sheet1:Sheet3!A1:B2)"); + assertEquals(2, ptgs.length); + if (wb instanceof HSSFWorkbook) { + assertEquals(Area3DPtg.class, ptgs[0].getClass()); + } else { + assertEquals(Area3DPxg.class, ptgs[0].getClass()); + } + assertEquals("Sheet1:Sheet3!A1:B2", toFormulaString(ptgs[0], fpb)); + assertEquals(AttrPtg.class, ptgs[1].getClass()); + assertEquals("SUM", toFormulaString(ptgs[1], fpb)); + + + // MIN to a range of cells over 3 workbooks, absolute reference + ptgs = parse(fpb, "MIN(Sheet1:Sheet3!$A$1:$B$2)"); + assertEquals(2, ptgs.length); + if (wb instanceof HSSFWorkbook) { + assertEquals(Area3DPtg.class, ptgs[0].getClass()); + } else { + assertEquals(Area3DPxg.class, ptgs[0].getClass()); + } + assertEquals("Sheet1:Sheet3!$A$1:$B$2", toFormulaString(ptgs[0], fpb)); + assertEquals(FuncVarPtg.class, ptgs[1].getClass()); + assertEquals("MIN", toFormulaString(ptgs[1], fpb)); + + + // Check we can round-trip - try to set a new one to a new single cell Cell newF = s1.getRow(0).createCell(10, Cell.CELL_TYPE_FORMULA); newF.setCellFormula("SUM(Sheet2:Sheet3!A1)"); assertEquals("SUM(Sheet2:Sheet3!A1)", newF.getCellFormula()); + + // Check we can round-trip - try to set a new one to a cell range + newF = s1.getRow(0).createCell(11, Cell.CELL_TYPE_FORMULA); + newF.setCellFormula("MIN(Sheet1:Sheet2!A1:B2)"); + assertEquals("MIN(Sheet1:Sheet2!A1:B2)", newF.getCellFormula()); } } private static String toFormulaString(Ptg ptg, FormulaParsingWorkbook wb) { |