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 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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.getCellTypeEnum());
  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.getCellTypeEnum());
  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.getCellTypeEnum());
  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.getCellTypeEnum());
  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.getCellTypeEnum());
  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. Row row0 = sheet.createRow(0);
  237. Row row1 = sheet.createRow(1);
  238. Row row2 = sheet.createRow(2);
  239. Row row3 = sheet.createRow(3);
  240. row0.createCell(0).setCellValue(2);
  241. row1.createCell(0).setCellValue(5);
  242. row2.createCell(0).setCellValue(3);
  243. row3.createCell(0).setCellValue(7);
  244. row0.createCell(2).setCellFormula("aConstant");
  245. row1.createCell(2).setCellFormula("aFormula");
  246. row2.createCell(2).setCellFormula("SUM(aSet)");
  247. row3.createCell(2).setCellFormula("aConstant+aFormula+SUM(aSet)");
  248. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  249. assertEquals(3.14, fe.evaluate(row0.getCell(2)).getNumberValue(), EPSILON);
  250. assertEquals(10.0, fe.evaluate(row1.getCell(2)).getNumberValue(), EPSILON);
  251. assertEquals(15.0, fe.evaluate(row2.getCell(2)).getNumberValue(), EPSILON);
  252. assertEquals(28.14, fe.evaluate(row3.getCell(2)).getNumberValue(), EPSILON);
  253. wb.close();
  254. }
  255. @Test
  256. public void testIgnoreMissingWorkbooks() {
  257. // TODO: update this test for meaningful functional behavior
  258. WorkbookEvaluator evaluator = new WorkbookEvaluator(null, null, null);
  259. assertFalse(evaluator.isIgnoreMissingWorkbooks());
  260. evaluator.setIgnoreMissingWorkbooks(true);
  261. assertTrue(evaluator.isIgnoreMissingWorkbooks());
  262. evaluator.setIgnoreMissingWorkbooks(false);
  263. assertFalse(evaluator.isIgnoreMissingWorkbooks());
  264. }
  265. @Test
  266. public void testDebugEvaluationOutputForNextEval() {
  267. // TODO: update this test for meaningful functional behavior
  268. WorkbookEvaluator evaluator = new WorkbookEvaluator(null, null, null);
  269. assertFalse(evaluator.isDebugEvaluationOutputForNextEval());
  270. evaluator.setDebugEvaluationOutputForNextEval(true);
  271. assertTrue(evaluator.isDebugEvaluationOutputForNextEval());
  272. evaluator.setDebugEvaluationOutputForNextEval(false);
  273. assertFalse(evaluator.isDebugEvaluationOutputForNextEval());
  274. }
  275. // Test IF-Equals Formula Evaluation (bug 58591)
  276. private Workbook testIFEqualsFormulaEvaluation_setup(String formula, CellType a1CellType) {
  277. Workbook wb = new HSSFWorkbook();
  278. Sheet sheet = wb.createSheet("IFEquals");
  279. Row row = sheet.createRow(0);
  280. Cell A1 = row.createCell(0);
  281. Cell B1 = row.createCell(1);
  282. Cell C1 = row.createCell(2);
  283. Cell D1 = row.createCell(3);
  284. switch (a1CellType) {
  285. case NUMERIC:
  286. A1.setCellValue(1.0);
  287. // "A1=1" should return true
  288. break;
  289. case STRING:
  290. A1.setCellValue("1");
  291. // "A1=1" should return false
  292. // "A1=\"1\"" should return true
  293. break;
  294. case BOOLEAN:
  295. A1.setCellValue(true);
  296. // "A1=1" should return true
  297. break;
  298. case FORMULA:
  299. A1.setCellFormula("1");
  300. // "A1=1" should return true
  301. break;
  302. case BLANK:
  303. A1.setCellValue((String) null);
  304. // "A1=1" should return false
  305. break;
  306. default:
  307. throw new IllegalArgumentException("unexpected cell type: " + a1CellType);
  308. }
  309. B1.setCellValue(2.0);
  310. C1.setCellValue(3.0);
  311. D1.setCellFormula(formula);
  312. return wb;
  313. }
  314. private void testIFEqualsFormulaEvaluation_teardown(Workbook wb) {
  315. try {
  316. wb.close();
  317. } catch (final IOException e) {
  318. fail("Unable to close workbook");
  319. }
  320. }
  321. private void testIFEqualsFormulaEvaluation_evaluate(
  322. String formula, CellType cellType, String expectedFormula, double expectedResult) {
  323. Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
  324. Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
  325. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  326. CellValue result = eval.evaluate(D1);
  327. // Call should not modify the contents
  328. assertEquals(CellType.FORMULA, D1.getCellTypeEnum());
  329. assertEquals(expectedFormula, D1.getCellFormula());
  330. assertEquals(CellType.NUMERIC, result.getCellTypeEnum());
  331. assertEquals(expectedResult, result.getNumberValue(), EPSILON);
  332. testIFEqualsFormulaEvaluation_teardown(wb);
  333. }
  334. private void testIFEqualsFormulaEvaluation_eval(
  335. final String formula, final CellType cellType, final String expectedFormula, final double expectedValue) {
  336. testIFEqualsFormulaEvaluation_evaluate(formula, cellType, expectedFormula, expectedValue);
  337. testIFEqualsFormulaEvaluation_evaluateFormulaCell(formula, cellType, expectedFormula, expectedValue);
  338. testIFEqualsFormulaEvaluation_evaluateInCell(formula, cellType, expectedValue);
  339. testIFEqualsFormulaEvaluation_evaluateAll(formula, cellType, expectedFormula, expectedValue);
  340. testIFEqualsFormulaEvaluation_evaluateAllFormulaCells(formula, cellType, expectedFormula, expectedValue);
  341. }
  342. @Test
  343. public void testIFEqualsFormulaEvaluation_NumericLiteral() {
  344. final String formula = "IF(A1=1, 2, 3)";
  345. final CellType cellType = CellType.NUMERIC;
  346. final String expectedFormula = "IF(A1=1,2,3)";
  347. final double expectedValue = 2.0;
  348. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  349. }
  350. @Test
  351. public void testIFEqualsFormulaEvaluation_Numeric() {
  352. final String formula = "IF(A1=1, B1, C1)";
  353. final CellType cellType = CellType.NUMERIC;
  354. final String expectedFormula = "IF(A1=1,B1,C1)";
  355. final double expectedValue = 2.0;
  356. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  357. }
  358. @Test
  359. public void testIFEqualsFormulaEvaluation_NumericCoerceToString() {
  360. final String formula = "IF(A1&\"\"=\"1\", B1, C1)";
  361. final CellType cellType = CellType.NUMERIC;
  362. final String expectedFormula = "IF(A1&\"\"=\"1\",B1,C1)";
  363. final double expectedValue = 2.0;
  364. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  365. }
  366. @Test
  367. public void testIFEqualsFormulaEvaluation_String() {
  368. final String formula = "IF(A1=1, B1, C1)";
  369. final CellType cellType = CellType.STRING;
  370. final String expectedFormula = "IF(A1=1,B1,C1)";
  371. final double expectedValue = 3.0;
  372. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  373. }
  374. @Test
  375. public void testIFEqualsFormulaEvaluation_StringCompareToString() {
  376. final String formula = "IF(A1=\"1\", B1, C1)";
  377. final CellType cellType = CellType.STRING;
  378. final String expectedFormula = "IF(A1=\"1\",B1,C1)";
  379. final double expectedValue = 2.0;
  380. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  381. }
  382. @Test
  383. public void testIFEqualsFormulaEvaluation_StringCoerceToNumeric() {
  384. final String formula = "IF(A1+0=1, B1, C1)";
  385. final CellType cellType = CellType.STRING;
  386. final String expectedFormula = "IF(A1+0=1,B1,C1)";
  387. final double expectedValue = 2.0;
  388. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  389. }
  390. @Ignore("Bug 58591: this test currently fails")
  391. @Test
  392. public void testIFEqualsFormulaEvaluation_Boolean() {
  393. final String formula = "IF(A1=1, B1, C1)";
  394. final CellType cellType = CellType.BOOLEAN;
  395. final String expectedFormula = "IF(A1=1,B1,C1)";
  396. final double expectedValue = 2.0;
  397. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  398. }
  399. @Ignore("Bug 58591: this test currently fails")
  400. @Test
  401. public void testIFEqualsFormulaEvaluation_BooleanSimple() {
  402. final String formula = "3-(A1=1)";
  403. final CellType cellType = CellType.BOOLEAN;
  404. final String expectedFormula = "3-(A1=1)";
  405. final double expectedValue = 2.0;
  406. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  407. }
  408. @Test
  409. public void testIFEqualsFormulaEvaluation_Formula() {
  410. final String formula = "IF(A1=1, B1, C1)";
  411. final CellType cellType = CellType.FORMULA;
  412. final String expectedFormula = "IF(A1=1,B1,C1)";
  413. final double expectedValue = 2.0;
  414. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  415. }
  416. @Test
  417. public void testIFEqualsFormulaEvaluation_Blank() {
  418. final String formula = "IF(A1=1, B1, C1)";
  419. final CellType cellType = CellType.BLANK;
  420. final String expectedFormula = "IF(A1=1,B1,C1)";
  421. final double expectedValue = 3.0;
  422. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  423. }
  424. @Test
  425. public void testIFEqualsFormulaEvaluation_BlankCompareToZero() {
  426. final String formula = "IF(A1=0, B1, C1)";
  427. final CellType cellType = CellType.BLANK;
  428. final String expectedFormula = "IF(A1=0,B1,C1)";
  429. final double expectedValue = 2.0;
  430. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  431. }
  432. @Ignore("Bug 58591: this test currently fails")
  433. @Test
  434. public void testIFEqualsFormulaEvaluation_BlankInverted() {
  435. final String formula = "IF(NOT(A1)=1, B1, C1)";
  436. final CellType cellType = CellType.BLANK;
  437. final String expectedFormula = "IF(NOT(A1)=1,B1,C1)";
  438. final double expectedValue = 2.0;
  439. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  440. }
  441. @Ignore("Bug 58591: this test currently fails")
  442. @Test
  443. public void testIFEqualsFormulaEvaluation_BlankInvertedSimple() {
  444. final String formula = "3-(NOT(A1)=1)";
  445. final CellType cellType = CellType.BLANK;
  446. final String expectedFormula = "3-(NOT(A1)=1)";
  447. final double expectedValue = 2.0;
  448. testIFEqualsFormulaEvaluation_eval(formula, cellType, expectedFormula, expectedValue);
  449. }
  450. private void testIFEqualsFormulaEvaluation_evaluateFormulaCell(
  451. String formula, CellType cellType, String expectedFormula, double expectedResult) {
  452. Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
  453. Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
  454. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  455. CellType resultCellType = eval.evaluateFormulaCellEnum(D1);
  456. // Call should modify the contents, but leave the formula intact
  457. assertEquals(CellType.FORMULA, D1.getCellTypeEnum());
  458. assertEquals(expectedFormula, D1.getCellFormula());
  459. assertEquals(CellType.NUMERIC, resultCellType);
  460. assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultTypeEnum());
  461. assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
  462. testIFEqualsFormulaEvaluation_teardown(wb);
  463. }
  464. private void testIFEqualsFormulaEvaluation_evaluateInCell(
  465. String formula, CellType cellType, double expectedResult) {
  466. Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
  467. Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
  468. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  469. Cell result = eval.evaluateInCell(D1);
  470. // Call should modify the contents and replace the formula with the result
  471. assertSame(D1, result); // returns the same cell that was provided as an argument so that calls can be chained.
  472. try {
  473. D1.getCellFormula();
  474. fail("cell formula should be overwritten with formula result");
  475. } catch (final IllegalStateException expected) {
  476. // expected here
  477. }
  478. assertEquals(CellType.NUMERIC, D1.getCellTypeEnum());
  479. assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
  480. testIFEqualsFormulaEvaluation_teardown(wb);
  481. }
  482. private void testIFEqualsFormulaEvaluation_evaluateAll(
  483. String formula, CellType cellType, String expectedFormula, double expectedResult) {
  484. Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
  485. Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
  486. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  487. eval.evaluateAll();
  488. // Call should modify the contents
  489. assertEquals(CellType.FORMULA, D1.getCellTypeEnum());
  490. assertEquals(expectedFormula, D1.getCellFormula());
  491. assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultTypeEnum());
  492. assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
  493. testIFEqualsFormulaEvaluation_teardown(wb);
  494. }
  495. private void testIFEqualsFormulaEvaluation_evaluateAllFormulaCells(
  496. String formula, CellType cellType, String expectedFormula, double expectedResult) {
  497. Workbook wb = testIFEqualsFormulaEvaluation_setup(formula, cellType);
  498. Cell D1 = wb.getSheet("IFEquals").getRow(0).getCell(3);
  499. HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  500. // Call should modify the contents
  501. assertEquals(CellType.FORMULA, D1.getCellTypeEnum());
  502. // whitespace gets deleted because formula is parsed and re-rendered
  503. assertEquals(expectedFormula, D1.getCellFormula());
  504. assertEquals(CellType.NUMERIC, D1.getCachedFormulaResultTypeEnum());
  505. assertEquals(expectedResult, D1.getNumericCellValue(), EPSILON);
  506. testIFEqualsFormulaEvaluation_teardown(wb);
  507. }
  508. }