You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestWorkbookEvaluator.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertSame;
  19. import static org.junit.Assert.assertTrue;
  20. import static org.junit.Assert.fail;
  21. import java.io.IOException;
  22. import org.apache.poi.hssf.HSSFTestDataSamples;
  23. import org.apache.poi.hssf.usermodel.HSSFCell;
  24. import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
  25. import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
  26. import org.apache.poi.hssf.usermodel.HSSFRow;
  27. import org.apache.poi.hssf.usermodel.HSSFSheet;
  28. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  29. import org.apache.poi.ss.formula.eval.BlankEval;
  30. import org.apache.poi.ss.formula.eval.ErrorEval;
  31. import org.apache.poi.ss.formula.eval.MissingArgEval;
  32. import org.apache.poi.ss.formula.eval.NumberEval;
  33. import org.apache.poi.ss.formula.eval.ValueEval;
  34. import org.apache.poi.ss.formula.ptg.*;
  35. import org.apache.poi.ss.usermodel.Cell;
  36. import org.apache.poi.ss.usermodel.CellType;
  37. import org.apache.poi.ss.usermodel.CellValue;
  38. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  39. import org.apache.poi.ss.usermodel.Name;
  40. import org.apache.poi.ss.usermodel.Row;
  41. import org.apache.poi.ss.usermodel.Sheet;
  42. import org.apache.poi.ss.usermodel.Workbook;
  43. import org.junit.Ignore;
  44. import org.junit.Test;
  45. /**
  46. * Tests {@link WorkbookEvaluator}.
  47. *
  48. * @author Josh Micich
  49. */
  50. public class TestWorkbookEvaluator {
  51. private static final double EPSILON = 0.0000001;
  52. private static ValueEval evaluateFormula(Ptg[] ptgs) {
  53. HSSFWorkbook wb = new HSSFWorkbook();
  54. wb.createSheet().createRow(0).createCell(0);
  55. EvaluationWorkbook ewb = HSSFEvaluationWorkbook.create(wb);
  56. OperationEvaluationContext ec = new OperationEvaluationContext(null, ewb, 0, 0, 0, null);
  57. return new WorkbookEvaluator(null, null, null).evaluateFormula(ec, ptgs);
  58. }
  59. /**
  60. * Make sure that the evaluator can directly handle tAttrSum (instead of relying on re-parsing
  61. * the whole formula which converts tAttrSum to tFuncVar("SUM") )
  62. */
  63. @Test
  64. public void testAttrSum() {
  65. Ptg[] ptgs = {
  66. new IntPtg(42),
  67. AttrPtg.SUM,
  68. };
  69. ValueEval result = evaluateFormula(ptgs);
  70. assertEquals(42, ((NumberEval)result).getNumberValue(), 0.0);
  71. }
  72. /**
  73. * Make sure that the evaluator can directly handle (deleted) ref error tokens
  74. * (instead of relying on re-parsing the whole formula which converts these
  75. * to the error constant #REF! )
  76. */
  77. @Test
  78. public void testRefErr() {
  79. confirmRefErr(new RefErrorPtg());
  80. confirmRefErr(new AreaErrPtg());
  81. confirmRefErr(new DeletedRef3DPtg(0));
  82. confirmRefErr(new DeletedArea3DPtg(0));
  83. }
  84. private static void confirmRefErr(Ptg ptg) {
  85. Ptg[] ptgs = {
  86. ptg,
  87. };
  88. ValueEval result = evaluateFormula(ptgs);
  89. assertEquals(ErrorEval.REF_INVALID, result);
  90. }
  91. /**
  92. * Make sure that the evaluator can directly handle tAttrSum (instead of relying on re-parsing
  93. * the whole formula which converts tAttrSum to tFuncVar("SUM") )
  94. */
  95. @Test
  96. public void testMemFunc() {
  97. Ptg[] ptgs = {
  98. new IntPtg(42),
  99. AttrPtg.SUM,
  100. };
  101. ValueEval result = evaluateFormula(ptgs);
  102. assertEquals(42, ((NumberEval)result).getNumberValue(), 0.0);
  103. }
  104. @Test
  105. public void testEvaluateMultipleWorkbooks() {
  106. HSSFWorkbook wbA = HSSFTestDataSamples.openSampleWorkbook("multibookFormulaA.xls");
  107. HSSFWorkbook wbB = HSSFTestDataSamples.openSampleWorkbook("multibookFormulaB.xls");
  108. HSSFFormulaEvaluator evaluatorA = new HSSFFormulaEvaluator(wbA);
  109. HSSFFormulaEvaluator evaluatorB = new HSSFFormulaEvaluator(wbB);
  110. // Hook up the workbook evaluators to enable evaluation of formulas across books
  111. String[] bookNames = { "multibookFormulaA.xls", "multibookFormulaB.xls", };
  112. HSSFFormulaEvaluator[] evaluators = { evaluatorA, evaluatorB, };
  113. HSSFFormulaEvaluator.setupEnvironment(bookNames, evaluators);
  114. HSSFCell cell;
  115. HSSFSheet aSheet1 = wbA.getSheetAt(0);
  116. HSSFSheet bSheet1 = wbB.getSheetAt(0);
  117. // Simple case - single link from wbA to wbB
  118. confirmFormula(wbA, 0, 0, 0, "[multibookFormulaB.xls]BSheet1!B1");
  119. cell = aSheet1.getRow(0).getCell(0);
  120. confirmEvaluation(35, evaluatorA, cell);
  121. // more complex case - back link into wbA
  122. // [wbA]ASheet1!A2 references (among other things) [wbB]BSheet1!B2
  123. confirmFormula(wbA, 0, 1, 0, "[multibookFormulaB.xls]BSheet1!$B$2+2*A3");
  124. // [wbB]BSheet1!B2 references (among other things) [wbA]AnotherSheet!A1:B2
  125. confirmFormula(wbB, 0, 1, 1, "SUM([multibookFormulaA.xls]AnotherSheet!$A$1:$B$2)+B3");
  126. cell = aSheet1.getRow(1).getCell(0);
  127. confirmEvaluation(264, evaluatorA, cell);
  128. // change [wbB]BSheet1!B3 (from 50 to 60)
  129. HSSFCell cellB3 = bSheet1.getRow(2).getCell(1);
  130. cellB3.setCellValue(60);
  131. evaluatorB.notifyUpdateCell(cellB3);
  132. confirmEvaluation(274, evaluatorA, cell);
  133. // change [wbA]ASheet1!A3 (from 100 to 80)
  134. HSSFCell cellA3 = aSheet1.getRow(2).getCell(0);
  135. cellA3.setCellValue(80);
  136. evaluatorA.notifyUpdateCell(cellA3);
  137. confirmEvaluation(234, evaluatorA, cell);
  138. // change [wbA]AnotherSheet!A1 (from 2 to 3)
  139. HSSFCell cellA1 = wbA.getSheetAt(1).getRow(0).getCell(0);
  140. cellA1.setCellValue(3);
  141. evaluatorA.notifyUpdateCell(cellA1);
  142. confirmEvaluation(235, evaluatorA, cell);
  143. }
  144. private static void confirmEvaluation(double expectedValue, HSSFFormulaEvaluator fe, HSSFCell cell) {
  145. assertEquals(expectedValue, fe.evaluate(cell).getNumberValue(), 0.0);
  146. }
  147. private static void confirmFormula(HSSFWorkbook wb, int sheetIndex, int rowIndex, int columnIndex,
  148. String expectedFormula) {
  149. HSSFCell cell = wb.getSheetAt(sheetIndex).getRow(rowIndex).getCell(columnIndex);
  150. assertEquals(expectedFormula, cell.getCellFormula());
  151. }
  152. /**
  153. * This test makes sure that any {@link MissingArgEval} that propagates to
  154. * the result of a function gets translated to {@link BlankEval}.
  155. */
  156. @Test
  157. public void testMissingArg() {
  158. HSSFWorkbook wb = new HSSFWorkbook();
  159. HSSFSheet sheet = wb.createSheet("Sheet1");
  160. HSSFRow row = sheet.createRow(0);
  161. HSSFCell cell = row.createCell(0);
  162. cell.setCellFormula("1+IF(1,,)");
  163. HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
  164. CellValue cv = null;
  165. try {
  166. cv = fe.evaluate(cell);
  167. } catch (RuntimeException e) {
  168. fail("Missing arg result not being handled correctly.");
  169. }
  170. assertEquals(CellType.NUMERIC, cv.getCellType());
  171. // adding blank to 1.0 gives 1.0
  172. assertEquals(1.0, cv.getNumberValue(), 0.0);
  173. // check with string operand
  174. cell.setCellFormula("\"abc\"&IF(1,,)");
  175. fe.notifySetFormula(cell);
  176. cv = fe.evaluate(cell);
  177. assertEquals(CellType.STRING, cv.getCellType());
  178. // adding blank to "abc" gives "abc"
  179. assertEquals("abc", cv.getStringValue());
  180. // check CHOOSE()
  181. cell.setCellFormula("\"abc\"&CHOOSE(2,5,,9)");
  182. fe.notifySetFormula(cell);
  183. cv = fe.evaluate(cell);
  184. assertEquals(CellType.STRING, cv.getCellType());
  185. // adding blank to "abc" gives "abc"
  186. assertEquals("abc", cv.getStringValue());
  187. }
  188. /**
  189. * Functions like IF, INDIRECT, INDEX, OFFSET etc can return AreaEvals which
  190. * should be dereferenced by the evaluator
  191. */
  192. @Test
  193. public void testResultOutsideRange() throws IOException {
  194. Workbook wb = new HSSFWorkbook();
  195. try {
  196. Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
  197. cell.setCellFormula("D2:D5"); // IF(TRUE,D2:D5,D2) or OFFSET(D2:D5,0,0) would work too
  198. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  199. CellValue cv;
  200. try {
  201. cv = fe.evaluate(cell);
  202. } catch (IllegalArgumentException e) {
  203. if ("Specified row index (0) is outside the allowed range (1..4)".equals(e.getMessage())) {
  204. fail("Identified bug in result dereferencing");
  205. }
  206. throw new RuntimeException(e);
  207. }
  208. assertEquals(CellType.ERROR, cv.getCellType());
  209. assertEquals(ErrorEval.VALUE_INVALID.getErrorCode(), cv.getErrorValue());
  210. // verify circular refs are still detected properly
  211. fe.clearAllCachedResultValues();
  212. cell.setCellFormula("OFFSET(A1,0,0)");
  213. cv = fe.evaluate(cell);
  214. assertEquals(CellType.ERROR, cv.getCellType());
  215. assertEquals(ErrorEval.CIRCULAR_REF_ERROR.getErrorCode(), cv.getErrorValue());
  216. } finally {
  217. wb.close();
  218. }
  219. }
  220. /**
  221. * formulas with defined names.
  222. */
  223. @Test
  224. public void testNamesInFormulas() throws IOException {
  225. Workbook wb = new HSSFWorkbook();
  226. Sheet sheet = wb.createSheet("Sheet1");
  227. Name name1 = wb.createName();
  228. name1.setNameName("aConstant");
  229. name1.setRefersToFormula("3.14");
  230. Name name2 = wb.createName();
  231. name2.setNameName("aFormula");
  232. name2.setRefersToFormula("SUM(Sheet1!$A$1:$A$3)");
  233. Name name3 = wb.createName();
  234. name3.setNameName("aSet");
  235. name3.setRefersToFormula("Sheet1!$A$2:$A$4");
  236. Name name4 = wb.createName();
  237. name4.setNameName("offsetFormula");
  238. name4.setRefersToFormula("OFFSET(Sheet1!$A$1:$A$4,2,0,2,1)");
  239. Name name5 = wb.createName();
  240. name5.setNameName("rowFormula");
  241. name5.setRefersToFormula("ROW()");
  242. Row row0 = sheet.createRow(0);
  243. Row row1 = sheet.createRow(1);
  244. Row row2 = sheet.createRow(2);
  245. Row row3 = sheet.createRow(3);
  246. Row row4 = sheet.createRow(4);
  247. Row row5 = sheet.createRow(5);
  248. row0.createCell(0).setCellValue(2);
  249. row1.createCell(0).setCellValue(5);
  250. row2.createCell(0).setCellValue(3);
  251. row3.createCell(0).setCellValue(7);
  252. row0.createCell(2).setCellFormula("aConstant");
  253. row1.createCell(2).setCellFormula("aFormula");
  254. row2.createCell(2).setCellFormula("SUM(aSet)");
  255. row3.createCell(2).setCellFormula("aConstant+aFormula+SUM(aSet)");
  256. row4.createCell(2).setCellFormula("SUM(offsetFormula)");
  257. row5.createCell(2).setCellFormula("rowFormula");
  258. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  259. assertEquals(3.14, fe.evaluate(row0.getCell(2)).getNumberValue(), EPSILON);
  260. assertEquals(10.0, fe.evaluate(row1.getCell(2)).getNumberValue(), EPSILON);
  261. assertEquals(15.0, fe.evaluate(row2.getCell(2)).getNumberValue(), EPSILON);
  262. assertEquals(28.14, fe.evaluate(row3.getCell(2)).getNumberValue(), EPSILON);
  263. assertEquals(10.0, fe.evaluate(row4.getCell(2)).getNumberValue(), EPSILON);
  264. assertEquals(6.0, fe.evaluate(row5.getCell(2)).getNumberValue(), EPSILON);
  265. wb.close();
  266. }
  267. @Test
  268. public void testIgnoreMissingWorkbooks() {
  269. // TODO: update this test for meaningful functional behavior
  270. WorkbookEvaluator evaluator = new WorkbookEvaluator(null, null, null);
  271. assertFalse(evaluator.isIgnoreMissingWorkbooks());
  272. evaluator.setIgnoreMissingWorkbooks(true);
  273. assertTrue(evaluator.isIgnoreMissingWorkbooks());
  274. evaluator.setIgnoreMissingWorkbooks(false);
  275. assertFalse(evaluator.isIgnoreMissingWorkbooks());
  276. }
  277. @Test
  278. public void testDebugEvaluationOutputForNextEval() {
  279. // TODO: update this test for meaningful functional behavior
  280. WorkbookEvaluator evaluator = new WorkbookEvaluator(null, null, null);
  281. assertFalse(evaluator.isDebugEvaluationOutputForNextEval());
  282. evaluator.setDebugEvaluationOutputForNextEval(true);
  283. assertTrue(evaluator.isDebugEvaluationOutputForNextEval());
  284. evaluator.setDebugEvaluationOutputForNextEval(false);
  285. assertFalse(evaluator.isDebugEvaluationOutputForNextEval());
  286. }
  287. // Test IF-Equals Formula Evaluation (bug 58591)
  288. private Workbook testIFEqualsFormulaEvaluation_setup(String formula, CellType a1CellType) {
  289. Workbook wb = new HSSFWorkbook();
  290. Sheet sheet = wb.createSheet("IFEquals");
  291. Row row = sheet.createRow(0);
  292. Cell A1 = row.createCell(0);
  293. Cell B1 = row.createCell(1);
  294. Cell C1 = row.createCell(2);
  295. Cell D1 = row.createCell(3);
  296. switch (a1CellType) {
  297. case NUMERIC:
  298. A1.setCellValue(1.0);
  299. // "A1=1" should return true
  300. break;
  301. case STRING:
  302. A1.setCellValue("1");
  303. // "A1=1" should return false
  304. // "A1=\"1\"" should return true
  305. break;
  306. case BOOLEAN:
  307. A1.setCellValue(true);
  308. // "A1=1" should return true
  309. break;
  310. case FORMULA:
  311. A1.setCellFormula("1");
  312. // "A1=1" should return true
  313. break;
  314. case BLANK:
  315. A1.setCellValue((String) null);
  316. // "A1=1" should return false
  317. break;
  318. default:
  319. throw new IllegalArgumentException("unexpected cell type: " + a1CellType);
  320. }
  321. B1.setCellValue(2.0);
  322. C1.setCellValue(3.0);
  323. D1.setCellFormula(formula);
  324. return wb;
  325. }
  326. private void testIFEqualsFormulaEvaluation_teardown(Workbook wb) {
  327. try {
  328. wb.close();
  329. } catch (final IOException e) {
  330. fail("Unable to close workbook");
  331. }
  332. }
  333. private void testIFEqualsFormulaEvaluation_evaluate(
  334. String formula, CellType cellType, String expectedFormula, double expectedResult) {
  335. Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
  336. Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
  337. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  338. CellValue result = eval.evaluate(D1);
  339. // Call should not modify the contents
  340. assertEquals(CellType.FORMULA, D1.getCellType());
  341. assertEquals(expectedFormula, D1.getCellFormula());
  342. assertEquals(CellType.NUMERIC, result.getCellType());
  343. assertEquals(expectedResult, result.getNumberValue(), EPSILON);
  344. testIFEqualsFormulaEvaluation_teardown(wb);
  345. }
  346. private void testIFEqualsFormulaEvaluation_eval(
  347. final String formula, final CellType cellType, final String expectedFormula, final double expectedValue) {
  348. testIFEqualsFormulaEvaluation_evaluate(formula, cellType, expectedFormula, expectedValue);
  349. testIFEqualsFormulaEvaluation_evaluateFormulaCell(formula, cellType, expectedFormula, expectedValue);
  350. testIFEqualsFormulaEvaluation_evaluateInCell(formula, cellType, expectedValue);
  351. testIFEqualsFormulaEvaluation_evaluateAll(formula, cellType, expectedFormula, expectedValue);
  352. testIFEqualsFormulaEvaluation_evaluateAllFormulaCells(formula, cellType, expectedFormula, expectedValue);
  353. }
  354. @Test
  355. public void testIFEqualsFormulaEvaluation_NumericLiteral() {
  356. final String formula = "IF(A1=1, 2, 3)";
  357. final CellType cellType = CellType.NUMERIC;
  358. final String expectedFormula = "IF(A1=1,2,3)";
  359. final double expectedValue = 2.0;
  360. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  361. }
  362. @Test
  363. public void testIFEqualsFormulaEvaluation_Numeric() {
  364. final String formula = "IF(A1=1, B1, C1)";
  365. final CellType cellType = CellType.NUMERIC;
  366. final String expectedFormula = "IF(A1=1,B1,C1)";
  367. final double expectedValue = 2.0;
  368. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  369. }
  370. @Test
  371. public void testIFEqualsFormulaEvaluation_NumericCoerceToString() {
  372. final String formula = "IF(A1&\"\"=\"1\", B1, C1)";
  373. final CellType cellType = CellType.NUMERIC;
  374. final String expectedFormula = "IF(A1&\"\"=\"1\",B1,C1)";
  375. final double expectedValue = 2.0;
  376. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  377. }
  378. @Test
  379. public void testIFEqualsFormulaEvaluation_String() {
  380. final String formula = "IF(A1=1, B1, C1)";
  381. final CellType cellType = CellType.STRING;
  382. final String expectedFormula = "IF(A1=1,B1,C1)";
  383. final double expectedValue = 3.0;
  384. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  385. }
  386. @Test
  387. public void testIFEqualsFormulaEvaluation_StringCompareToString() {
  388. final String formula = "IF(A1=\"1\", B1, C1)";
  389. final CellType cellType = CellType.STRING;
  390. final String expectedFormula = "IF(A1=\"1\",B1,C1)";
  391. final double expectedValue = 2.0;
  392. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  393. }
  394. @Test
  395. public void testIFEqualsFormulaEvaluation_StringCoerceToNumeric() {
  396. final String formula = "IF(A1+0=1, B1, C1)";
  397. final CellType cellType = CellType.STRING;
  398. final String expectedFormula = "IF(A1+0=1,B1,C1)";
  399. final double expectedValue = 2.0;
  400. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  401. }
  402. @Ignore("Bug 58591: this test currently fails")
  403. @Test
  404. public void testIFEqualsFormulaEvaluation_Boolean() {
  405. final String formula = "IF(A1=1, B1, C1)";
  406. final CellType cellType = CellType.BOOLEAN;
  407. final String expectedFormula = "IF(A1=1,B1,C1)";
  408. final double expectedValue = 2.0;
  409. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  410. }
  411. @Ignore("Bug 58591: this test currently fails")
  412. @Test
  413. public void testIFEqualsFormulaEvaluation_BooleanSimple() {
  414. final String formula = "3-(A1=1)";
  415. final CellType cellType = CellType.BOOLEAN;
  416. final String expectedFormula = "3-(A1=1)";
  417. final double expectedValue = 2.0;
  418. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  419. }
  420. @Test
  421. public void testIFEqualsFormulaEvaluation_Formula() {
  422. final String formula = "IF(A1=1, B1, C1)";
  423. final CellType cellType = CellType.FORMULA;
  424. final String expectedFormula = "IF(A1=1,B1,C1)";
  425. final double expectedValue = 2.0;
  426. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  427. }
  428. @Test
  429. public void testIFEqualsFormulaEvaluation_Blank() {
  430. final String formula = "IF(A1=1, B1, C1)";
  431. final CellType cellType = CellType.BLANK;
  432. final String expectedFormula = "IF(A1=1,B1,C1)";
  433. final double expectedValue = 3.0;
  434. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  435. }
  436. @Test
  437. public void testIFEqualsFormulaEvaluation_BlankCompareToZero() {
  438. final String formula = "IF(A1=0, B1, C1)";
  439. final CellType cellType = CellType.BLANK;
  440. final String expectedFormula = "IF(A1=0,B1,C1)";
  441. final double expectedValue = 2.0;
  442. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  443. }
  444. @Ignore("Bug 58591: this test currently fails")
  445. @Test
  446. public void testIFEqualsFormulaEvaluation_BlankInverted() {
  447. final String formula = "IF(NOT(A1)=1, B1, C1)";
  448. final CellType cellType = CellType.BLANK;
  449. final String expectedFormula = "IF(NOT(A1)=1,B1,C1)";
  450. final double expectedValue = 2.0;
  451. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  452. }
  453. @Ignore("Bug 58591: this test currently fails")
  454. @Test
  455. public void testIFEqualsFormulaEvaluation_BlankInvertedSimple() {
  456. final String formula = "3-(NOT(A1)=1)";
  457. final CellType cellType = CellType.BLANK;
  458. final String expectedFormula = "3-(NOT(A1)=1)";
  459. final double expectedValue = 2.0;
  460. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  461. }
  462. private void testIFEqualsFormulaEvaluation_evaluateFormulaCell(
  463. String formula, CellType cellType, String expectedFormula, double expectedResult) {
  464. Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
  465. Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
  466. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  467. CellType resultCellType = eval.evaluateFormulaCell(D1);
  468. // Call should modify the contents, but leave the formula intact
  469. assertEquals(CellType.FORMULA, D1.getCellType());
  470. assertEquals(expectedFormula, D1.getCellFormula());
  471. assertEquals(CellType.NUMERIC, resultCellType);
  472. assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultType());
  473. assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
  474. testIFEqualsFormulaEvaluation_teardown(wb);
  475. }
  476. private void testIFEqualsFormulaEvaluation_evaluateInCell(
  477. String formula, CellType cellType, double expectedResult) {
  478. Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
  479. Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
  480. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  481. Cell result = eval.evaluateInCell(D1);
  482. // Call should modify the contents and replace the formula with the result
  483. assertSame(D1, result); // returns the same cell that was provided as an argument so that calls can be chained.
  484. try {
  485. D1.getCellFormula();
  486. fail("cell formula should be overwritten with formula result");
  487. } catch (final IllegalStateException expected) {
  488. // expected here
  489. }
  490. assertEquals(CellType.NUMERIC, D1.getCellType());
  491. assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
  492. testIFEqualsFormulaEvaluation_teardown(wb);
  493. }
  494. private void testIFEqualsFormulaEvaluation_evaluateAll(
  495. String formula, CellType cellType, String expectedFormula, double expectedResult) {
  496. Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
  497. Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
  498. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  499. eval.evaluateAll();
  500. // Call should modify the contents
  501. assertEquals(CellType.FORMULA, D1.getCellType());
  502. assertEquals(expectedFormula, D1.getCellFormula());
  503. assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultType());
  504. assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
  505. testIFEqualsFormulaEvaluation_teardown(wb);
  506. }
  507. private void testIFEqualsFormulaEvaluation_evaluateAllFormulaCells(
  508. String formula, CellType cellType, String expectedFormula, double expectedResult) {
  509. Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
  510. Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
  511. HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  512. // Call should modify the contents
  513. assertEquals(CellType.FORMULA, D1.getCellType());
  514. // whitespace gets deleted because formula is parsed and re-rendered
  515. assertEquals(expectedFormula, D1.getCellFormula());
  516. assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultType());
  517. assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
  518. testIFEqualsFormulaEvaluation_teardown(wb);
  519. }
  520. }