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.

BaseTestFormulaEvaluator.java 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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.usermodel;
  16. import static org.junit.jupiter.api.Assertions.assertEquals;
  17. import static org.junit.jupiter.api.Assertions.assertNotNull;
  18. import static org.junit.jupiter.api.Assertions.assertNull;
  19. import static org.junit.jupiter.api.Assertions.assertSame;
  20. import static org.junit.jupiter.api.Assertions.assertTrue;
  21. import static org.junit.jupiter.api.Assertions.fail;
  22. import java.io.IOException;
  23. import java.util.Locale;
  24. import org.apache.poi.ss.ITestDataProvider;
  25. import org.apache.poi.ss.SpreadsheetVersion;
  26. import org.apache.poi.ss.util.CellRangeAddressList;
  27. import org.junit.jupiter.api.Test;
  28. /**
  29. * Common superclass for testing implementation of {@link FormulaEvaluator}
  30. */
  31. public abstract class BaseTestFormulaEvaluator {
  32. protected final ITestDataProvider _testDataProvider;
  33. /**
  34. * @param testDataProvider an object that provides test data in / specific way
  35. */
  36. protected BaseTestFormulaEvaluator(ITestDataProvider testDataProvider) {
  37. _testDataProvider = testDataProvider;
  38. }
  39. @Test
  40. public void testSimpleArithmetic() throws IOException {
  41. Workbook wb = _testDataProvider.createWorkbook();
  42. Sheet s = wb.createSheet();
  43. Row r = s.createRow(0);
  44. Cell c1 = r.createCell(0);
  45. c1.setCellFormula("1+5");
  46. assertEquals(0.0, c1.getNumericCellValue(), 0.0);
  47. Cell c2 = r.createCell(1);
  48. c2.setCellFormula("10/2");
  49. assertEquals(0.0, c2.getNumericCellValue(), 0.0);
  50. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  51. fe.evaluateFormulaCell(c1);
  52. fe.evaluateFormulaCell(c2);
  53. assertEquals(6.0, c1.getNumericCellValue(), 0.0001);
  54. assertEquals(5.0, c2.getNumericCellValue(), 0.0001);
  55. wb.close();
  56. }
  57. @Test
  58. public void testSumCount() throws IOException {
  59. Workbook wb = _testDataProvider.createWorkbook();
  60. Sheet s = wb.createSheet();
  61. Row r = s.createRow(0);
  62. r.createCell(0).setCellValue(2.5);
  63. r.createCell(1).setCellValue(1.1);
  64. r.createCell(2).setCellValue(3.2);
  65. r.createCell(4).setCellValue(10.7);
  66. r = s.createRow(1);
  67. Cell c1 = r.createCell(0);
  68. c1.setCellFormula("SUM(A1:B1)");
  69. assertEquals(0.0, c1.getNumericCellValue(), 0.0);
  70. Cell c2 = r.createCell(1);
  71. c2.setCellFormula("SUM(A1:E1)");
  72. assertEquals(0.0, c2.getNumericCellValue(), 0.0);
  73. Cell c3 = r.createCell(2);
  74. c3.setCellFormula("COUNT(A1:A1)");
  75. assertEquals(0.0, c3.getNumericCellValue(), 0.0);
  76. Cell c4 = r.createCell(3);
  77. c4.setCellFormula("COUNTA(A1:E1)");
  78. assertEquals(0.0, c4.getNumericCellValue(), 0.0);
  79. // Evaluate and test
  80. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  81. fe.evaluateFormulaCell(c1);
  82. fe.evaluateFormulaCell(c2);
  83. fe.evaluateFormulaCell(c3);
  84. fe.evaluateFormulaCell(c4);
  85. assertEquals(3.6, c1.getNumericCellValue(), 0.0001);
  86. assertEquals(17.5, c2.getNumericCellValue(), 0.0001);
  87. assertEquals(1, c3.getNumericCellValue(), 0.0001);
  88. assertEquals(4, c4.getNumericCellValue(), 0.0001);
  89. wb.close();
  90. }
  91. @Test
  92. public void testSharedFormulas() throws IOException {
  93. String fileName = "shared_formulas.xls" + (getClass().getName().contains("xssf") ? "x" : "");
  94. try (Workbook wb = _testDataProvider.openSampleWorkbook(fileName)) {
  95. Sheet sheet = wb.getSheetAt(0);
  96. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  97. Cell cell;
  98. cell = sheet.getRow(1).getCell(0);
  99. assertEquals("B2", cell.getCellFormula());
  100. assertEquals("ProductionOrderConfirmation", evaluator.evaluate(cell).getStringValue());
  101. cell = sheet.getRow(2).getCell(0);
  102. assertEquals("B3", cell.getCellFormula());
  103. assertEquals("RequiredAcceptanceDate", evaluator.evaluate(cell).getStringValue());
  104. cell = sheet.getRow(3).getCell(0);
  105. assertEquals("B4", cell.getCellFormula());
  106. assertEquals("Header", evaluator.evaluate(cell).getStringValue());
  107. cell = sheet.getRow(4).getCell(0);
  108. assertEquals("B5", cell.getCellFormula());
  109. assertEquals("UniqueDocumentNumberID", evaluator.evaluate(cell).getStringValue());
  110. }
  111. }
  112. /**
  113. * Test creation / evaluation of formulas with sheet-level names
  114. */
  115. @Test
  116. public void testSheetLevelFormulas() throws IOException {
  117. Workbook wb = _testDataProvider.createWorkbook();
  118. Row row;
  119. Sheet sh1 = wb.createSheet("Sheet1");
  120. Name nm1 = wb.createName();
  121. nm1.setNameName("sales_1");
  122. nm1.setSheetIndex(0);
  123. nm1.setRefersToFormula("Sheet1!$A$1");
  124. row = sh1.createRow(0);
  125. row.createCell(0).setCellValue(3);
  126. row.createCell(1).setCellFormula("sales_1");
  127. row.createCell(2).setCellFormula("sales_1*2");
  128. Sheet sh2 = wb.createSheet("Sheet2");
  129. Name nm2 = wb.createName();
  130. nm2.setNameName("sales_1");
  131. nm2.setSheetIndex(1);
  132. nm2.setRefersToFormula("Sheet2!$A$1");
  133. row = sh2.createRow(0);
  134. row.createCell(0).setCellValue(5);
  135. row.createCell(1).setCellFormula("sales_1");
  136. row.createCell(2).setCellFormula("sales_1*3");
  137. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  138. assertEquals(3.0, evaluator.evaluate(sh1.getRow(0).getCell(1)).getNumberValue(), 0.0);
  139. assertEquals(6.0, evaluator.evaluate(sh1.getRow(0).getCell(2)).getNumberValue(), 0.0);
  140. assertEquals(5.0, evaluator.evaluate(sh2.getRow(0).getCell(1)).getNumberValue(), 0.0);
  141. assertEquals(15.0, evaluator.evaluate(sh2.getRow(0).getCell(2)).getNumberValue(), 0.0);
  142. wb.close();
  143. }
  144. @Test
  145. public void testFullColumnRefs() throws IOException {
  146. Workbook wb = _testDataProvider.createWorkbook();
  147. Sheet sheet = wb.createSheet("Sheet1");
  148. Row row = sheet.createRow(0);
  149. Cell cell0 = row.createCell(0);
  150. cell0.setCellFormula("sum(D:D)");
  151. Cell cell1 = row.createCell(1);
  152. cell1.setCellFormula("sum(D:E)");
  153. // some values in column D
  154. setValue(sheet, 1, 3, 5.0);
  155. setValue(sheet, 2, 3, 6.0);
  156. setValue(sheet, 5, 3, 7.0);
  157. setValue(sheet, 50, 3, 8.0);
  158. // some values in column E
  159. setValue(sheet, 1, 4, 9.0);
  160. setValue(sheet, 2, 4, 10.0);
  161. setValue(sheet, 30000, 4, 11.0);
  162. // some other values
  163. setValue(sheet, 1, 2, 100.0);
  164. setValue(sheet, 2, 5, 100.0);
  165. setValue(sheet, 3, 6, 100.0);
  166. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  167. assertEquals(26.0, fe.evaluate(cell0).getNumberValue(), 0.0);
  168. assertEquals(56.0, fe.evaluate(cell1).getNumberValue(), 0.0);
  169. wb.close();
  170. }
  171. @Test
  172. public void testRepeatedEvaluation() throws IOException {
  173. Workbook wb = _testDataProvider.createWorkbook();
  174. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  175. Sheet sheet = wb.createSheet("Sheet1");
  176. Row r = sheet.createRow(0);
  177. Cell c = r.createCell(0, CellType.FORMULA);
  178. // Create a value and check it
  179. c.setCellFormula("Date(2011,10,6)");
  180. CellValue cellValue = fe.evaluate(c);
  181. assertEquals(40822.0, cellValue.getNumberValue(), 0.0);
  182. cellValue = fe.evaluate(c);
  183. assertEquals(40822.0, cellValue.getNumberValue(), 0.0);
  184. // Change it
  185. c.setCellFormula("Date(2011,10,4)");
  186. // Evaluate it, no change as the formula evaluator
  187. // won't know to clear the cache
  188. cellValue = fe.evaluate(c);
  189. assertEquals(40822.0, cellValue.getNumberValue(), 0.0);
  190. // Manually flush for this cell, and check
  191. fe.notifySetFormula(c);
  192. cellValue = fe.evaluate(c);
  193. assertEquals(40820.0, cellValue.getNumberValue(), 0.0);
  194. // Change again, without notifying
  195. c.setCellFormula("Date(2010,10,4)");
  196. cellValue = fe.evaluate(c);
  197. assertEquals(40820.0, cellValue.getNumberValue(), 0.0);
  198. // Now manually clear all, will see the new value
  199. fe.clearAllCachedResultValues();
  200. cellValue = fe.evaluate(c);
  201. assertEquals(40455.0, cellValue.getNumberValue(), 0.0);
  202. wb.close();
  203. }
  204. private static void setValue(Sheet sheet, int rowIndex, int colIndex, double value) {
  205. Row row = sheet.getRow(rowIndex);
  206. if (row == null) {
  207. row = sheet.createRow(rowIndex);
  208. }
  209. row.createCell(colIndex).setCellValue(value);
  210. }
  211. /**
  212. * {@link FormulaEvaluator#evaluate(org.apache.poi.ss.usermodel.Cell)} should behave the same whether the cell
  213. * is <code>null</code> or blank.
  214. */
  215. @Test
  216. public void testEvaluateBlank() throws IOException {
  217. Workbook wb = _testDataProvider.createWorkbook();
  218. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  219. assertNull(fe.evaluate(null));
  220. Sheet sheet = wb.createSheet("Sheet1");
  221. Cell cell = sheet.createRow(0).createCell(0);
  222. assertNull(fe.evaluate(cell));
  223. wb.close();
  224. }
  225. /**
  226. * Test for bug due to attempt to convert a cached formula error result to a boolean
  227. */
  228. @Test
  229. public void testUpdateCachedFormulaResultFromErrorToNumber_bug46479() throws IOException {
  230. try (Workbook wb = _testDataProvider.createWorkbook()) {
  231. Sheet sheet = wb.createSheet("Sheet1");
  232. Row row = sheet.createRow(0);
  233. Cell cellA1 = row.createCell(0);
  234. Cell cellB1 = row.createCell(1);
  235. cellB1.setCellFormula("A1+1");
  236. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  237. cellA1.setCellErrorValue(FormulaError.NAME.getCode());
  238. assertEquals(CellType.ERROR, fe.evaluateFormulaCell(cellB1));
  239. assertEquals(CellType.FORMULA, cellB1.getCellType());
  240. cellA1.setCellValue(2.5);
  241. fe.notifyUpdateCell(cellA1);
  242. try {
  243. fe.evaluateInCell(cellB1);
  244. } catch (IllegalStateException e) {
  245. if (e.getMessage().equalsIgnoreCase("Cannot get a numeric value from a error formula cell")) {
  246. fail("Identified bug 46479a");
  247. }
  248. }
  249. assertEquals(3.5, cellB1.getNumericCellValue(), 0.0);
  250. }
  251. }
  252. @Test
  253. public void testRounding_bug51339() throws IOException {
  254. Workbook wb = _testDataProvider.createWorkbook();
  255. Sheet sheet = wb.createSheet("Sheet1");
  256. Row row = sheet.createRow(0);
  257. Cell cellA1 = row.createCell(0);
  258. cellA1.setCellValue(2162.615d);
  259. Cell cellB1 = row.createCell(1);
  260. cellB1.setCellFormula("round(a1,2)");
  261. Cell cellC1 = row.createCell(2);
  262. cellC1.setCellFormula("roundup(a1,2)");
  263. Cell cellD1 = row.createCell(3);
  264. cellD1.setCellFormula("rounddown(a1,2)");
  265. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  266. assertEquals(2162.62, fe.evaluateInCell(cellB1).getNumericCellValue(), 0.0);
  267. assertEquals(2162.62, fe.evaluateInCell(cellC1).getNumericCellValue(), 0.0);
  268. assertEquals(2162.61, fe.evaluateInCell(cellD1).getNumericCellValue(), 0.0);
  269. wb.close();
  270. }
  271. @Test
  272. public void evaluateInCellReturnsSameCell() throws IOException {
  273. try (Workbook wb = _testDataProvider.createWorkbook()) {
  274. wb.createSheet().createRow(0).createCell(0);
  275. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  276. Cell cell = wb.getSheetAt(0).getRow(0).getCell(0);
  277. Cell same = evaluator.evaluateInCell(cell);
  278. assertSame(cell, same);
  279. }
  280. }
  281. @Test
  282. public void testBug61148() throws IOException {
  283. try (Workbook wb = _testDataProvider.createWorkbook()) {
  284. final Cell cell = wb.createSheet().createRow(0).createCell(0);
  285. cell.setCellFormula("1+2");
  286. assertEquals(0, (int)cell.getNumericCellValue());
  287. assertEquals("1+2", cell.toString());
  288. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  289. eval.evaluateInCell(cell);
  290. assertEquals("3.0", cell.toString());
  291. }
  292. }
  293. @Test
  294. public void testMultisheetFormulaEval() throws IOException {
  295. try (Workbook wb = _testDataProvider.createWorkbook()) {
  296. Sheet sheet1 = wb.createSheet("Sheet1");
  297. Sheet sheet2 = wb.createSheet("Sheet2");
  298. Sheet sheet3 = wb.createSheet("Sheet3");
  299. // sheet1 A1
  300. Cell cell = sheet1.createRow(0).createCell(0);
  301. cell.setCellValue(1.0);
  302. // sheet2 A1
  303. cell = sheet2.createRow(0).createCell(0);
  304. cell.setCellValue(1.0);
  305. // sheet2 B1
  306. cell = sheet2.getRow(0).createCell(1);
  307. cell.setCellValue(1.0);
  308. // sheet3 A1
  309. cell = sheet3.createRow(0).createCell(0);
  310. cell.setCellValue(1.0);
  311. // sheet1 A2 formulae
  312. cell = sheet1.createRow(1).createCell(0);
  313. cell.setCellFormula("SUM(Sheet1:Sheet3!A1)");
  314. // sheet1 A3 formulae
  315. cell = sheet1.createRow(2).createCell(0);
  316. cell.setCellFormula("SUM(Sheet1:Sheet3!A1:B1)");
  317. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  318. cell = sheet1.getRow(1).getCell(0);
  319. assertEquals(3.0, cell.getNumericCellValue(), 0);
  320. cell = sheet1.getRow(2).getCell(0);
  321. assertEquals(4.0, cell.getNumericCellValue(), 0);
  322. }
  323. }
  324. @Test
  325. public void testBug55843() throws IOException {
  326. try (Workbook wb = _testDataProvider.createWorkbook()) {
  327. Sheet sheet = wb.createSheet("test");
  328. Row row = sheet.createRow(0);
  329. Row row2 = sheet.createRow(1);
  330. Cell cellA2 = row2.createCell(0, CellType.FORMULA);
  331. Cell cellB1 = row.createCell(1, CellType.NUMERIC);
  332. cellB1.setCellValue(10);
  333. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  334. cellA2.setCellFormula("IF(B1=0,\"\",((ROW()-ROW(A$1))*12))");
  335. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  336. assertEquals("12.0", evaluate.formatAsString());
  337. cellA2.setCellFormula("IF(NOT(B1=0),((ROW()-ROW(A$1))*12),\"\")");
  338. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  339. assertEquals(evaluate.toString(), evaluateN.toString());
  340. assertEquals("12.0", evaluateN.formatAsString());
  341. }
  342. }
  343. @Test
  344. public void testBug55843a() throws IOException {
  345. try (Workbook wb = _testDataProvider.createWorkbook()) {
  346. Sheet sheet = wb.createSheet("test");
  347. Row row = sheet.createRow(0);
  348. Row row2 = sheet.createRow(1);
  349. Cell cellA2 = row2.createCell(0, CellType.FORMULA);
  350. Cell cellB1 = row.createCell(1, CellType.NUMERIC);
  351. cellB1.setCellValue(10);
  352. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  353. cellA2.setCellFormula("IF(B1=0,\"\",((ROW(A$1))))");
  354. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  355. assertEquals("1.0", evaluate.formatAsString());
  356. cellA2.setCellFormula("IF(NOT(B1=0),((ROW(A$1))),\"\")");
  357. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  358. assertEquals(evaluate.toString(), evaluateN.toString());
  359. assertEquals("1.0", evaluateN.formatAsString());
  360. }
  361. }
  362. @Test
  363. public void testBug55843b() throws IOException {
  364. try (Workbook wb = _testDataProvider.createWorkbook()) {
  365. Sheet sheet = wb.createSheet("test");
  366. Row row = sheet.createRow(0);
  367. Row row2 = sheet.createRow(1);
  368. Cell cellA2 = row2.createCell(0, CellType.FORMULA);
  369. Cell cellB1 = row.createCell(1, CellType.NUMERIC);
  370. cellB1.setCellValue(10);
  371. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  372. cellA2.setCellFormula("IF(B1=0,\"\",((ROW())))");
  373. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  374. assertEquals("2.0", evaluate.formatAsString());
  375. cellA2.setCellFormula("IF(NOT(B1=0),((ROW())),\"\")");
  376. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  377. assertEquals(evaluate.toString(), evaluateN.toString());
  378. assertEquals("2.0", evaluateN.formatAsString());
  379. }
  380. }
  381. @Test
  382. public void testBug55843c() throws IOException {
  383. try (Workbook wb = _testDataProvider.createWorkbook()) {
  384. Sheet sheet = wb.createSheet("test");
  385. Row row = sheet.createRow(0);
  386. Row row2 = sheet.createRow(1);
  387. Cell cellA2 = row2.createCell(0, CellType.FORMULA);
  388. Cell cellB1 = row.createCell(1, CellType.NUMERIC);
  389. cellB1.setCellValue(10);
  390. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  391. cellA2.setCellFormula("IF(NOT(B1=0),((ROW())))");
  392. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  393. assertEquals("2.0", evaluateN.formatAsString());
  394. }
  395. }
  396. @Test
  397. public void testBug55843d() throws IOException {
  398. try (Workbook wb = _testDataProvider.createWorkbook()) {
  399. Sheet sheet = wb.createSheet("test");
  400. Row row = sheet.createRow(0);
  401. Row row2 = sheet.createRow(1);
  402. Cell cellA2 = row2.createCell(0, CellType.FORMULA);
  403. Cell cellB1 = row.createCell(1, CellType.NUMERIC);
  404. cellB1.setCellValue(10);
  405. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  406. cellA2.setCellFormula("IF(NOT(B1=0),((ROW())),\"\")");
  407. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  408. assertEquals("2.0", evaluateN.formatAsString());
  409. }
  410. }
  411. @Test
  412. public void testBug55843e() throws IOException {
  413. try (Workbook wb = _testDataProvider.createWorkbook()) {
  414. Sheet sheet = wb.createSheet("test");
  415. Row row = sheet.createRow(0);
  416. Row row2 = sheet.createRow(1);
  417. Cell cellA2 = row2.createCell(0, CellType.FORMULA);
  418. Cell cellB1 = row.createCell(1, CellType.NUMERIC);
  419. cellB1.setCellValue(10);
  420. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  421. cellA2.setCellFormula("IF(B1=0,\"\",((ROW())))");
  422. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  423. assertEquals("2.0", evaluate.formatAsString());
  424. }
  425. }
  426. @Test
  427. public void testBug55843f() throws IOException {
  428. try (Workbook wb = _testDataProvider.createWorkbook()) {
  429. Sheet sheet = wb.createSheet("test");
  430. Row row = sheet.createRow(0);
  431. Row row2 = sheet.createRow(1);
  432. Cell cellA2 = row2.createCell(0, CellType.FORMULA);
  433. Cell cellB1 = row.createCell(1, CellType.NUMERIC);
  434. cellB1.setCellValue(10);
  435. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  436. cellA2.setCellFormula("IF(B1=0,\"\",IF(B1=10,3,4))");
  437. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  438. assertEquals("3.0", evaluate.formatAsString());
  439. }
  440. }
  441. @Test
  442. public void testBug56655() throws IOException {
  443. try (Workbook wb = _testDataProvider.createWorkbook()) {
  444. Sheet sheet = wb.createSheet();
  445. setCellFormula(sheet, 0, 0, "#VALUE!");
  446. setCellFormula(sheet, 0, 1, "SUMIFS(A:A,A:A,#VALUE!)");
  447. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  448. assertEquals(CellType.ERROR, getCell(sheet, 0, 0).getCachedFormulaResultType());
  449. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 0).getErrorCellValue());
  450. assertEquals(CellType.ERROR, getCell(sheet, 0, 1).getCachedFormulaResultType());
  451. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 1).getErrorCellValue());
  452. }
  453. }
  454. @Test
  455. public void testBug56655a() throws IOException {
  456. try (Workbook wb = _testDataProvider.createWorkbook()) {
  457. Sheet sheet = wb.createSheet();
  458. setCellFormula(sheet, 0, 0, "B1*C1");
  459. sheet.getRow(0).createCell(1).setCellValue("A");
  460. setCellFormula(sheet, 1, 0, "B1*C1");
  461. sheet.getRow(1).createCell(1).setCellValue("A");
  462. setCellFormula(sheet, 0, 3, "SUMIFS(A:A,A:A,A2)");
  463. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  464. assertEquals(CellType.ERROR, getCell(sheet, 0, 0).getCachedFormulaResultType());
  465. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 0).getErrorCellValue());
  466. assertEquals(CellType.ERROR, getCell(sheet, 1, 0).getCachedFormulaResultType());
  467. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 1, 0).getErrorCellValue());
  468. assertEquals(CellType.ERROR, getCell(sheet, 0, 3).getCachedFormulaResultType());
  469. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 3).getErrorCellValue());
  470. }
  471. }
  472. /**
  473. * @param row 0-based
  474. * @param column 0-based
  475. */
  476. private void setCellFormula(Sheet sheet, int row, int column, String formula) {
  477. Row r = sheet.getRow(row);
  478. if (r == null) {
  479. r = sheet.createRow(row);
  480. }
  481. Cell cell = r.getCell(column);
  482. if (cell == null) {
  483. cell = r.createCell(column);
  484. }
  485. cell.setCellFormula(formula);
  486. }
  487. /**
  488. * @param rowNo 0-based
  489. * @param column 0-based
  490. */
  491. private Cell getCell(Sheet sheet, int rowNo, int column) {
  492. return sheet.getRow(rowNo).getCell(column);
  493. }
  494. @Test
  495. public void testBug61532() throws IOException {
  496. try (Workbook wb = _testDataProvider.createWorkbook()) {
  497. final Cell cell = wb.createSheet().createRow(0).createCell(0);
  498. cell.setCellFormula("1+2");
  499. assertEquals(0, (int)cell.getNumericCellValue());
  500. assertEquals("1+2", cell.toString());
  501. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  502. CellValue value = eval.evaluate(cell);
  503. assertEquals(CellType.NUMERIC, value.getCellType());
  504. assertEquals(3.0, value.getNumberValue(), 0.01);
  505. assertEquals(CellType.FORMULA, cell.getCellType());
  506. assertEquals("1+2", cell.getCellFormula());
  507. assertEquals("1+2", cell.toString());
  508. assertNotNull(eval.evaluateInCell(cell));
  509. assertEquals("3.0", cell.toString());
  510. assertEquals(CellType.NUMERIC, cell.getCellType());
  511. assertEquals(3.0, cell.getNumericCellValue(), 0.01);
  512. }
  513. }
  514. @Test
  515. public void testFormulaEvaluatorEvaluateSimpleFormulaCell() throws Exception {
  516. try (Workbook wb = _testDataProvider.createWorkbook()) {
  517. final Row row = wb.createSheet().createRow(0);
  518. final Cell a1 = row.createCell(0, CellType.NUMERIC);
  519. a1.setCellValue(1.0);
  520. final Cell a2 = row.createCell(1, CellType.NUMERIC);
  521. a2.setCellValue(2.0);
  522. final Cell a3 = row.createCell(2, CellType.FORMULA);
  523. final String formula = "SUM(A1:B1)";
  524. a3.setCellFormula(formula);
  525. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  526. CellType resultType = evaluator.evaluateFormulaCell(a3);
  527. assertEquals(CellType.NUMERIC, resultType);
  528. double result = a3.getNumericCellValue();
  529. // result is correct
  530. assertTrue(result > 2.0, String.format(Locale.ROOT, "Expected %f to be greater than %f", result, 2.0));
  531. assertTrue(result < 4.0, String.format(Locale.ROOT, "Expected %f to be less than %f", result, 4.0));
  532. // ensure that this works for SUM
  533. assertEquals(CellType.FORMULA, a3.getCellType());
  534. assertEquals(formula, a3.getCellFormula());
  535. }
  536. }
  537. @Test
  538. public void testFormulaEvaluatorEvaluateVlookupFormulaCell() throws Exception {
  539. try (Workbook wb = _testDataProvider.createWorkbook()) {
  540. final Sheet mainSheet = wb.createSheet("main");
  541. final Sheet otherSheet = wb.createSheet("other");
  542. final Row otherRow1 = otherSheet.createRow(0);
  543. final Cell label1 = otherRow1.createCell(0, CellType.STRING);
  544. label1.setCellValue("Thing One");
  545. final Cell id1 = otherRow1.createCell(1, CellType.STRING);
  546. id1.setCellValue("1");
  547. final Row otherRow2 = otherSheet.createRow(1);
  548. final Cell label2 = otherRow2.createCell(0, CellType.STRING);
  549. label2.setCellValue("Thing Two");
  550. final Cell id2 = otherRow2.createCell(1, CellType.STRING);
  551. id2.setCellValue("2");
  552. final DataValidationHelper dvHelper = mainSheet.getDataValidationHelper();
  553. final int maxRows = SpreadsheetVersion.EXCEL2007.getMaxRows() - 1;
  554. final CellRangeAddressList addressList = new CellRangeAddressList(0, maxRows, 0, 0);
  555. final String constraint = "'other'!$A:$A";
  556. final DataValidationConstraint dvConstraint = dvHelper.createFormulaListConstraint(constraint);
  557. final DataValidation dataValidation = dvHelper.createValidation(dvConstraint, addressList);
  558. dataValidation.setShowErrorBox(true);
  559. mainSheet.addValidationData(dataValidation);
  560. wb.setSheetHidden(wb.getSheetIndex(otherSheet), true);
  561. final Row row = mainSheet.createRow(0);
  562. final Cell a1 = row.createCell(0, CellType.STRING);
  563. a1.setCellValue("Thing Two");
  564. final Cell a2 = row.createCell(1, CellType.FORMULA);
  565. final String formula = "VLOOKUP(A1,other!A:B,2,FALSE)";
  566. a2.setCellFormula(formula);
  567. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  568. CellType resultType = evaluator.evaluateFormulaCell(a2);
  569. assertEquals(CellType.STRING, resultType);
  570. // result is correct
  571. String result = a2.getStringCellValue();
  572. assertEquals("2", result);
  573. // ensure that this works for vlookup as well
  574. assertEquals(CellType.FORMULA, a2.getCellType());
  575. assertEquals(formula, a2.getCellFormula());
  576. }
  577. }
  578. // setting an evaluation of function arguments with the intersect operator (space)
  579. // see https://bz.apache.org/bugzilla/show_bug.cgi?id=60980
  580. @Test
  581. public void testIntersectionInFunctionArgs_60980() throws IOException {
  582. Workbook wb = _testDataProvider.createWorkbook();
  583. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  584. Name n1 = wb.createName();
  585. n1.setNameName("foo");
  586. n1.setRefersToFormula("A4:B5");
  587. Name n2 = wb.createName();
  588. n2.setNameName("bar");
  589. n2.setRefersToFormula("B4:C5");
  590. Sheet sheet = wb.createSheet();
  591. Row row3 = sheet.createRow(3);
  592. row3.createCell(0).setCellValue(1);
  593. row3.createCell(1).setCellValue(2);
  594. row3.createCell(2).setCellValue(3);
  595. Row row4 = sheet.createRow(4);
  596. row4.createCell(0).setCellValue(4);
  597. row4.createCell(1).setCellValue(5);
  598. row4.createCell(2).setCellValue(6);
  599. Cell fmla1 = row3.createCell(4);
  600. fmla1.setCellFormula("SUM(A4:B5 B4:C5)");
  601. fe.evaluateFormulaCell(fmla1);
  602. assertEquals(7, fmla1.getNumericCellValue(), 0.000);
  603. Cell fmla2 = row3.createCell(5);
  604. fmla2.setCellFormula("SUM(foo bar)");
  605. fe.evaluateFormulaCell(fmla2);
  606. assertEquals(7, fmla2.getNumericCellValue(), 0.000);
  607. wb.close();
  608. }
  609. }