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.

TestXSSFFormulaEvaluation.java 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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.xssf.usermodel;
  16. import static org.junit.Assert.*;
  17. import java.io.IOException;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import org.apache.poi.hssf.HSSFTestDataSamples;
  21. import org.apache.poi.ss.usermodel.BaseTestFormulaEvaluator;
  22. import org.apache.poi.ss.usermodel.Cell;
  23. import org.apache.poi.ss.usermodel.CellType;
  24. import org.apache.poi.ss.usermodel.CellValue;
  25. import org.apache.poi.ss.usermodel.FormulaError;
  26. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  27. import org.apache.poi.ss.usermodel.Row;
  28. import org.apache.poi.ss.usermodel.Sheet;
  29. import org.apache.poi.ss.usermodel.Workbook;
  30. import org.apache.poi.ss.util.CellReference;
  31. import org.apache.poi.xssf.XSSFITestDataProvider;
  32. import org.apache.poi.xssf.XSSFTestDataSamples;
  33. import org.junit.Ignore;
  34. import org.junit.Test;
  35. public final class TestXSSFFormulaEvaluation extends BaseTestFormulaEvaluator {
  36. public TestXSSFFormulaEvaluation() {
  37. super(XSSFITestDataProvider.instance);
  38. }
  39. @Test
  40. public void testSharedFormulas() throws IOException {
  41. baseTestSharedFormulas("shared_formulas.xlsx");
  42. }
  43. @Test
  44. public void testSharedFormulas_evaluateInCell() throws IOException {
  45. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("49872.xlsx");
  46. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  47. XSSFSheet sheet = wb.getSheetAt(0);
  48. double result = 3.0;
  49. // B3 is a master shared formula, C3 and D3 don't have the formula written in their f element.
  50. // Instead, the attribute si for a particular cell is used to figure what the formula expression
  51. // should be based on the cell's relative location to the master formula, e.g.
  52. // B3: <f t="shared" ref="B3:D3" si="0">B1+B2</f>
  53. // C3 and D3: <f t="shared" si="0"/>
  54. // get B3 and evaluate it in the cell
  55. XSSFCell b3 = sheet.getRow(2).getCell(1);
  56. assertEquals(result, evaluator.evaluateInCell(b3).getNumericCellValue(), 0);
  57. //at this point the master formula is gone, but we are still able to evaluate dependent cells
  58. XSSFCell c3 = sheet.getRow(2).getCell(2);
  59. assertEquals(result, evaluator.evaluateInCell(c3).getNumericCellValue(), 0);
  60. XSSFCell d3 = sheet.getRow(2).getCell(3);
  61. assertEquals(result, evaluator.evaluateInCell(d3).getNumericCellValue(), 0);
  62. wb.close();
  63. }
  64. /**
  65. * Evaluation of cell references with column indexes greater than 255. See bugzilla 50096
  66. */
  67. @Test
  68. public void testEvaluateColumnGreaterThan255() throws IOException {
  69. XSSFWorkbook wb = (XSSFWorkbook) _testDataProvider.openSampleWorkbook("50096.xlsx");
  70. XSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  71. /**
  72. * The first row simply contains the numbers 1 - 300.
  73. * The second row simply refers to the cell value above in the first row by a simple formula.
  74. */
  75. for (int i = 245; i < 265; i++) {
  76. XSSFCell cell_noformula = wb.getSheetAt(0).getRow(0).getCell(i);
  77. XSSFCell cell_formula = wb.getSheetAt(0).getRow(1).getCell(i);
  78. CellReference ref_noformula = new CellReference(cell_noformula.getRowIndex(), cell_noformula.getColumnIndex());
  79. CellReference ref_formula = new CellReference(cell_noformula.getRowIndex(), cell_noformula.getColumnIndex());
  80. String fmla = cell_formula.getCellFormula();
  81. // assure that the formula refers to the cell above.
  82. // the check below is 'deep' and involves conversion of the shared formula:
  83. // in the sample file a shared formula in GN1 is spanned in the range GN2:IY2,
  84. assertEquals(ref_noformula.formatAsString(), fmla);
  85. CellValue cv_noformula = evaluator.evaluate(cell_noformula);
  86. CellValue cv_formula = evaluator.evaluate(cell_formula);
  87. assertEquals("Wrong evaluation result in " + ref_formula.formatAsString(),
  88. cv_noformula.getNumberValue(), cv_formula.getNumberValue(), 0);
  89. }
  90. wb.close();
  91. }
  92. /**
  93. * Related to bugs #56737 and #56752 - XSSF workbooks which have
  94. * formulas that refer to cells and named ranges in multiple other
  95. * workbooks, both HSSF and XSSF ones
  96. */
  97. @Test
  98. public void testReferencesToOtherWorkbooks() throws Exception {
  99. XSSFWorkbook wb = (XSSFWorkbook) _testDataProvider.openSampleWorkbook("ref2-56737.xlsx");
  100. XSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  101. XSSFSheet s = wb.getSheetAt(0);
  102. // References to a .xlsx file
  103. Row rXSLX = s.getRow(2);
  104. Cell cXSLX_cell = rXSLX.getCell(4);
  105. Cell cXSLX_sNR = rXSLX.getCell(6);
  106. Cell cXSLX_gNR = rXSLX.getCell(8);
  107. assertEquals("[1]Uses!$A$1", cXSLX_cell.getCellFormula());
  108. assertEquals("[1]Defines!NR_To_A1", cXSLX_sNR.getCellFormula());
  109. assertEquals("[1]!NR_Global_B2", cXSLX_gNR.getCellFormula());
  110. assertEquals("Hello!", cXSLX_cell.getStringCellValue());
  111. assertEquals("Test A1", cXSLX_sNR.getStringCellValue());
  112. assertEquals(142.0, cXSLX_gNR.getNumericCellValue(), 0);
  113. // References to a .xls file
  114. Row rXSL = s.getRow(4);
  115. Cell cXSL_cell = rXSL.getCell(4);
  116. Cell cXSL_sNR = rXSL.getCell(6);
  117. Cell cXSL_gNR = rXSL.getCell(8);
  118. assertEquals("[2]Uses!$C$1", cXSL_cell.getCellFormula());
  119. assertEquals("[2]Defines!NR_To_A1", cXSL_sNR.getCellFormula());
  120. assertEquals("[2]!NR_Global_B2", cXSL_gNR.getCellFormula());
  121. assertEquals("Hello!", cXSL_cell.getStringCellValue());
  122. assertEquals("Test A1", cXSL_sNR.getStringCellValue());
  123. assertEquals(142.0, cXSL_gNR.getNumericCellValue(), 0);
  124. // Try to evaluate without references, won't work
  125. // (At least, not unit we fix bug #56752 that is)
  126. try {
  127. evaluator.evaluate(cXSL_cell);
  128. fail("Without a fix for #56752, shouldn't be able to evaluate a " +
  129. "reference to a non-provided linked workbook");
  130. } catch(Exception e) {
  131. // expected here
  132. }
  133. // Setup the environment
  134. Map<String,FormulaEvaluator> evaluators = new HashMap<String, FormulaEvaluator>();
  135. evaluators.put("ref2-56737.xlsx", evaluator);
  136. evaluators.put("56737.xlsx",
  137. _testDataProvider.openSampleWorkbook("56737.xlsx").getCreationHelper().createFormulaEvaluator());
  138. evaluators.put("56737.xls",
  139. HSSFTestDataSamples.openSampleWorkbook("56737.xls").getCreationHelper().createFormulaEvaluator());
  140. evaluator.setupReferencedWorkbooks(evaluators);
  141. // Try evaluating all of them, ensure we don't blow up
  142. for(Row r : s) {
  143. for (Cell c : r) {
  144. evaluator.evaluate(c);
  145. }
  146. }
  147. // And evaluate the other way too
  148. evaluator.evaluateAll();
  149. // Static evaluator won't work, as no references passed in
  150. try {
  151. XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  152. fail("Static method lacks references, shouldn't work");
  153. } catch(Exception e) {
  154. // expected here
  155. }
  156. // Evaluate specific cells and check results
  157. assertEquals("\"Hello!\"", evaluator.evaluate(cXSLX_cell).formatAsString());
  158. assertEquals("\"Test A1\"", evaluator.evaluate(cXSLX_sNR).formatAsString());
  159. assertEquals("142.0", evaluator.evaluate(cXSLX_gNR).formatAsString());
  160. assertEquals("\"Hello!\"", evaluator.evaluate(cXSL_cell).formatAsString());
  161. assertEquals("\"Test A1\"", evaluator.evaluate(cXSL_sNR).formatAsString());
  162. assertEquals("142.0", evaluator.evaluate(cXSL_gNR).formatAsString());
  163. // Add another formula referencing these workbooks
  164. Cell cXSL_cell2 = rXSL.createCell(40);
  165. cXSL_cell2.setCellFormula("[56737.xls]Uses!$C$1");
  166. // TODO Shouldn't it become [2] like the others?
  167. assertEquals("[56737.xls]Uses!$C$1", cXSL_cell2.getCellFormula());
  168. assertEquals("\"Hello!\"", evaluator.evaluate(cXSL_cell2).formatAsString());
  169. // Now add a formula that refers to yet another (different) workbook
  170. // Won't work without the workbook being linked
  171. Cell cXSLX_nw_cell = rXSLX.createCell(42);
  172. try {
  173. cXSLX_nw_cell.setCellFormula("[alt.xlsx]Sheet1!$A$1");
  174. fail("New workbook not linked, shouldn't be able to add");
  175. } catch (Exception e) {
  176. // expected here
  177. }
  178. // Link and re-try
  179. Workbook alt = new XSSFWorkbook();
  180. try {
  181. alt.createSheet().createRow(0).createCell(0).setCellValue("In another workbook");
  182. // TODO Implement the rest of this, see bug #57184
  183. /*
  184. wb.linkExternalWorkbook("alt.xlsx", alt);
  185. cXSLX_nw_cell.setCellFormula("[alt.xlsx]Sheet1!$A$1");
  186. // Check it - TODO Is this correct? Or should it become [3]Sheet1!$A$1 ?
  187. assertEquals("[alt.xlsx]Sheet1!$A$1", cXSLX_nw_cell.getCellFormula());
  188. // Evaluate it, without a link to that workbook
  189. try {
  190. evaluator.evaluate(cXSLX_nw_cell);
  191. fail("No cached value and no link to workbook, shouldn't evaluate");
  192. } catch(Exception e) {}
  193. // Add a link, check it does
  194. evaluators.put("alt.xlsx", alt.getCreationHelper().createFormulaEvaluator());
  195. evaluator.setupReferencedWorkbooks(evaluators);
  196. evaluator.evaluate(cXSLX_nw_cell);
  197. assertEquals("In another workbook", cXSLX_nw_cell.getStringCellValue());
  198. */
  199. } finally {
  200. alt.close();
  201. }
  202. wb.close();
  203. }
  204. /**
  205. * If a formula references cells or named ranges in another workbook,
  206. * but that isn't available at evaluation time, the cached values
  207. * should be used instead
  208. * TODO Add the support then add a unit test
  209. * See bug #56752
  210. */
  211. @Test
  212. @Ignore
  213. public void testCachedReferencesToOtherWorkbooks() throws Exception {
  214. // TODO
  215. fail("unit test not written yet");
  216. }
  217. /**
  218. * A handful of functions (such as SUM, COUNTA, MIN) support
  219. * multi-sheet references (eg Sheet1:Sheet3!A1 = Cell A1 from
  220. * Sheets 1 through Sheet 3).
  221. * This test, based on common test files for HSSF and XSSF, checks
  222. * that we can correctly evaluate these
  223. */
  224. @Test
  225. public void testMultiSheetReferencesHSSFandXSSF() throws Exception {
  226. Workbook wb1 = HSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xls");
  227. Workbook wb2 = XSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xlsx");
  228. for (Workbook wb : new Workbook[] {wb1,wb2}) {
  229. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  230. Sheet s1 = wb.getSheetAt(0);
  231. // Simple SUM over numbers
  232. Cell sumF = s1.getRow(2).getCell(0);
  233. assertNotNull(sumF);
  234. assertEquals("SUM(Sheet1:Sheet3!A1)", sumF.getCellFormula());
  235. assertEquals("Failed for " + wb.getClass(), "66.0", evaluator.evaluate(sumF).formatAsString());
  236. // Various Stats formulas on numbers
  237. Cell avgF = s1.getRow(2).getCell(1);
  238. assertNotNull(avgF);
  239. assertEquals("AVERAGE(Sheet1:Sheet3!A1)", avgF.getCellFormula());
  240. assertEquals("22.0", evaluator.evaluate(avgF).formatAsString());
  241. Cell minF = s1.getRow(3).getCell(1);
  242. assertNotNull(minF);
  243. assertEquals("MIN(Sheet1:Sheet3!A$1)", minF.getCellFormula());
  244. assertEquals("11.0", evaluator.evaluate(minF).formatAsString());
  245. Cell maxF = s1.getRow(4).getCell(1);
  246. assertNotNull(maxF);
  247. assertEquals("MAX(Sheet1:Sheet3!A$1)", maxF.getCellFormula());
  248. assertEquals("33.0", evaluator.evaluate(maxF).formatAsString());
  249. Cell countF = s1.getRow(5).getCell(1);
  250. assertNotNull(countF);
  251. assertEquals("COUNT(Sheet1:Sheet3!A$1)", countF.getCellFormula());
  252. assertEquals("3.0", evaluator.evaluate(countF).formatAsString());
  253. // Various CountAs on Strings
  254. Cell countA_1F = s1.getRow(2).getCell(2);
  255. assertNotNull(countA_1F);
  256. assertEquals("COUNTA(Sheet1:Sheet3!C1)", countA_1F.getCellFormula());
  257. assertEquals("3.0", evaluator.evaluate(countA_1F).formatAsString());
  258. Cell countA_2F = s1.getRow(2).getCell(3);
  259. assertNotNull(countA_2F);
  260. assertEquals("COUNTA(Sheet1:Sheet3!D1)", countA_2F.getCellFormula());
  261. assertEquals("0.0", evaluator.evaluate(countA_2F).formatAsString());
  262. Cell countA_3F = s1.getRow(2).getCell(4);
  263. assertNotNull(countA_3F);
  264. assertEquals("COUNTA(Sheet1:Sheet3!E1)", countA_3F.getCellFormula());
  265. assertEquals("3.0", evaluator.evaluate(countA_3F).formatAsString());
  266. }
  267. wb2.close();
  268. wb1.close();
  269. }
  270. /**
  271. * A handful of functions (such as SUM, COUNTA, MIN) support
  272. * multi-sheet areas (eg Sheet1:Sheet3!A1:B2 = Cell A1 to Cell B2,
  273. * from Sheets 1 through Sheet 3).
  274. * This test, based on common test files for HSSF and XSSF, checks
  275. * that we can correctly evaluate these
  276. */
  277. @Test
  278. public void testMultiSheetAreasHSSFandXSSF() throws IOException {
  279. Workbook wb1 = HSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xls");
  280. Workbook wb2 = XSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xlsx");
  281. for (Workbook wb : new Workbook[]{wb1,wb2}) {
  282. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  283. Sheet s1 = wb.getSheetAt(0);
  284. // SUM over a range
  285. Cell sumFA = s1.getRow(2).getCell(7);
  286. assertNotNull(sumFA);
  287. assertEquals("SUM(Sheet1:Sheet3!A1:B2)", sumFA.getCellFormula());
  288. assertEquals("Failed for " + wb.getClass(), "110.0", evaluator.evaluate(sumFA).formatAsString());
  289. // Various Stats formulas on ranges of numbers
  290. Cell avgFA = s1.getRow(2).getCell(8);
  291. assertNotNull(avgFA);
  292. assertEquals("AVERAGE(Sheet1:Sheet3!A1:B2)", avgFA.getCellFormula());
  293. assertEquals("27.5", evaluator.evaluate(avgFA).formatAsString());
  294. Cell minFA = s1.getRow(3).getCell(8);
  295. assertNotNull(minFA);
  296. assertEquals("MIN(Sheet1:Sheet3!A$1:B$2)", minFA.getCellFormula());
  297. assertEquals("11.0", evaluator.evaluate(minFA).formatAsString());
  298. Cell maxFA = s1.getRow(4).getCell(8);
  299. assertNotNull(maxFA);
  300. assertEquals("MAX(Sheet1:Sheet3!A$1:B$2)", maxFA.getCellFormula());
  301. assertEquals("44.0", evaluator.evaluate(maxFA).formatAsString());
  302. Cell countFA = s1.getRow(5).getCell(8);
  303. assertNotNull(countFA);
  304. assertEquals("COUNT(Sheet1:Sheet3!$A$1:$B$2)", countFA.getCellFormula());
  305. assertEquals("4.0", evaluator.evaluate(countFA).formatAsString());
  306. }
  307. wb2.close();
  308. wb1.close();
  309. }
  310. @Test
  311. public void testMultisheetFormulaEval() throws IOException {
  312. XSSFWorkbook wb = new XSSFWorkbook();
  313. try {
  314. XSSFSheet sheet1 = wb.createSheet("Sheet1");
  315. XSSFSheet sheet2 = wb.createSheet("Sheet2");
  316. XSSFSheet sheet3 = wb.createSheet("Sheet3");
  317. // sheet1 A1
  318. XSSFCell cell = sheet1.createRow(0).createCell(0);
  319. cell.setCellType(CellType.NUMERIC);
  320. cell.setCellValue(1.0);
  321. // sheet2 A1
  322. cell = sheet2.createRow(0).createCell(0);
  323. cell.setCellType(CellType.NUMERIC);
  324. cell.setCellValue(1.0);
  325. // sheet2 B1
  326. cell = sheet2.getRow(0).createCell(1);
  327. cell.setCellType(CellType.NUMERIC);
  328. cell.setCellValue(1.0);
  329. // sheet3 A1
  330. cell = sheet3.createRow(0).createCell(0);
  331. cell.setCellType(CellType.NUMERIC);
  332. cell.setCellValue(1.0);
  333. // sheet1 A2 formulae
  334. cell = sheet1.createRow(1).createCell(0);
  335. cell.setCellType(CellType.FORMULA);
  336. cell.setCellFormula("SUM(Sheet1:Sheet3!A1)");
  337. // sheet1 A3 formulae
  338. cell = sheet1.createRow(2).createCell(0);
  339. cell.setCellType(CellType.FORMULA);
  340. cell.setCellFormula("SUM(Sheet1:Sheet3!A1:B1)");
  341. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  342. cell = sheet1.getRow(1).getCell(0);
  343. assertEquals(3.0, cell.getNumericCellValue(), 0);
  344. cell = sheet1.getRow(2).getCell(0);
  345. assertEquals(4.0, cell.getNumericCellValue(), 0);
  346. } finally {
  347. wb.close();
  348. }
  349. }
  350. @Test
  351. public void testBug55843() throws IOException {
  352. XSSFWorkbook wb = new XSSFWorkbook();
  353. try {
  354. XSSFSheet sheet = wb.createSheet("test");
  355. XSSFRow row = sheet.createRow(0);
  356. XSSFRow row2 = sheet.createRow(1);
  357. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  358. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  359. cellB1.setCellValue(10);
  360. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  361. cellA2.setCellFormula("IF(B1=0,\"\",((ROW()-ROW(A$1))*12))");
  362. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  363. assertEquals("12.0", evaluate.formatAsString());
  364. cellA2.setCellFormula("IF(NOT(B1=0),((ROW()-ROW(A$1))*12),\"\")");
  365. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  366. assertEquals(evaluate.toString(), evaluateN.toString());
  367. assertEquals("12.0", evaluateN.formatAsString());
  368. } finally {
  369. wb.close();
  370. }
  371. }
  372. @Test
  373. public void testBug55843a() throws IOException {
  374. XSSFWorkbook wb = new XSSFWorkbook();
  375. try {
  376. XSSFSheet sheet = wb.createSheet("test");
  377. XSSFRow row = sheet.createRow(0);
  378. XSSFRow row2 = sheet.createRow(1);
  379. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  380. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  381. cellB1.setCellValue(10);
  382. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  383. cellA2.setCellFormula("IF(B1=0,\"\",((ROW(A$1))))");
  384. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  385. assertEquals("1.0", evaluate.formatAsString());
  386. cellA2.setCellFormula("IF(NOT(B1=0),((ROW(A$1))),\"\")");
  387. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  388. assertEquals(evaluate.toString(), evaluateN.toString());
  389. assertEquals("1.0", evaluateN.formatAsString());
  390. } finally {
  391. wb.close();
  392. }
  393. }
  394. @Test
  395. public void testBug55843b() throws IOException {
  396. XSSFWorkbook wb = new XSSFWorkbook();
  397. try {
  398. XSSFSheet sheet = wb.createSheet("test");
  399. XSSFRow row = sheet.createRow(0);
  400. XSSFRow row2 = sheet.createRow(1);
  401. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  402. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  403. cellB1.setCellValue(10);
  404. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  405. cellA2.setCellFormula("IF(B1=0,\"\",((ROW())))");
  406. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  407. assertEquals("2.0", evaluate.formatAsString());
  408. cellA2.setCellFormula("IF(NOT(B1=0),((ROW())),\"\")");
  409. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  410. assertEquals(evaluate.toString(), evaluateN.toString());
  411. assertEquals("2.0", evaluateN.formatAsString());
  412. } finally {
  413. wb.close();
  414. }
  415. }
  416. @Test
  417. public void testBug55843c() throws IOException {
  418. XSSFWorkbook wb = new XSSFWorkbook();
  419. try {
  420. XSSFSheet sheet = wb.createSheet("test");
  421. XSSFRow row = sheet.createRow(0);
  422. XSSFRow row2 = sheet.createRow(1);
  423. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  424. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  425. cellB1.setCellValue(10);
  426. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  427. cellA2.setCellFormula("IF(NOT(B1=0),((ROW())))");
  428. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  429. assertEquals("2.0", evaluateN.formatAsString());
  430. } finally {
  431. wb.close();
  432. }
  433. }
  434. @Test
  435. public void testBug55843d() throws IOException {
  436. XSSFWorkbook wb = new XSSFWorkbook();
  437. try {
  438. XSSFSheet sheet = wb.createSheet("test");
  439. XSSFRow row = sheet.createRow(0);
  440. XSSFRow row2 = sheet.createRow(1);
  441. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  442. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  443. cellB1.setCellValue(10);
  444. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  445. cellA2.setCellFormula("IF(NOT(B1=0),((ROW())),\"\")");
  446. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  447. assertEquals("2.0", evaluateN.formatAsString());
  448. } finally {
  449. wb.close();
  450. }
  451. }
  452. @Test
  453. public void testBug55843e() throws IOException {
  454. XSSFWorkbook wb = new XSSFWorkbook();
  455. try {
  456. XSSFSheet sheet = wb.createSheet("test");
  457. XSSFRow row = sheet.createRow(0);
  458. XSSFRow row2 = sheet.createRow(1);
  459. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  460. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  461. cellB1.setCellValue(10);
  462. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  463. cellA2.setCellFormula("IF(B1=0,\"\",((ROW())))");
  464. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  465. assertEquals("2.0", evaluate.formatAsString());
  466. } finally {
  467. wb.close();
  468. }
  469. }
  470. @Test
  471. public void testBug55843f() throws IOException {
  472. XSSFWorkbook wb = new XSSFWorkbook();
  473. try {
  474. XSSFSheet sheet = wb.createSheet("test");
  475. XSSFRow row = sheet.createRow(0);
  476. XSSFRow row2 = sheet.createRow(1);
  477. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  478. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  479. cellB1.setCellValue(10);
  480. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  481. cellA2.setCellFormula("IF(B1=0,\"\",IF(B1=10,3,4))");
  482. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  483. assertEquals("3.0", evaluate.formatAsString());
  484. } finally {
  485. wb.close();
  486. }
  487. }
  488. @Test
  489. public void testBug56655() throws IOException {
  490. Workbook wb = new XSSFWorkbook();
  491. Sheet sheet = wb.createSheet();
  492. setCellFormula(sheet, 0, 0, "#VALUE!");
  493. setCellFormula(sheet, 0, 1, "SUMIFS(A:A,A:A,#VALUE!)");
  494. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  495. assertEquals(CellType.ERROR, getCell(sheet, 0,0).getCachedFormulaResultTypeEnum());
  496. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0,0).getErrorCellValue());
  497. assertEquals(CellType.ERROR, getCell(sheet, 0,1).getCachedFormulaResultTypeEnum());
  498. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0,1).getErrorCellValue());
  499. wb.close();
  500. }
  501. @Test
  502. public void testBug56655a() throws IOException {
  503. Workbook wb = new XSSFWorkbook();
  504. Sheet sheet = wb.createSheet();
  505. setCellFormula(sheet, 0, 0, "B1*C1");
  506. sheet.getRow(0).createCell(1).setCellValue("A");
  507. setCellFormula(sheet, 1, 0, "B1*C1");
  508. sheet.getRow(1).createCell(1).setCellValue("A");
  509. setCellFormula(sheet, 0, 3, "SUMIFS(A:A,A:A,A2)");
  510. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  511. assertEquals(CellType.ERROR, getCell(sheet, 0, 0).getCachedFormulaResultTypeEnum());
  512. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 0).getErrorCellValue());
  513. assertEquals(CellType.ERROR, getCell(sheet, 1, 0).getCachedFormulaResultTypeEnum());
  514. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 1, 0).getErrorCellValue());
  515. assertEquals(CellType.ERROR, getCell(sheet, 0, 3).getCachedFormulaResultTypeEnum());
  516. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 3).getErrorCellValue());
  517. wb.close();
  518. }
  519. // bug 57721
  520. @Test
  521. public void structuredReferences() throws IOException {
  522. verifyAllFormulasInWorkbookCanBeEvaluated("evaluate_formula_with_structured_table_references.xlsx");
  523. }
  524. // bug 57840
  525. @Ignore("Takes over a minute to evaluate all formulas in this large workbook. Run this test when profiling for formula evaluation speed.")
  526. @Test
  527. public void testLotsOfFormulasWithStructuredReferencesToCalculatedTableColumns() throws IOException {
  528. verifyAllFormulasInWorkbookCanBeEvaluated("StructuredRefs-lots-with-lookups.xlsx");
  529. }
  530. // FIXME: use junit4 parameterization
  531. private static void verifyAllFormulasInWorkbookCanBeEvaluated(String sampleWorkbook) throws IOException {
  532. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(sampleWorkbook);
  533. XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  534. wb.close();
  535. }
  536. /**
  537. * @param row 0-based
  538. * @param column 0-based
  539. */
  540. private void setCellFormula(Sheet sheet, int row, int column, String formula) {
  541. Row r = sheet.getRow(row);
  542. if (r == null) {
  543. r = sheet.createRow(row);
  544. }
  545. Cell cell = r.getCell(column);
  546. if (cell == null) {
  547. cell = r.createCell(column);
  548. }
  549. cell.setCellType(CellType.FORMULA);
  550. cell.setCellFormula(formula);
  551. }
  552. /**
  553. * @param rowNo 0-based
  554. * @param column 0-based
  555. */
  556. private Cell getCell(Sheet sheet, int rowNo, int column) {
  557. return sheet.getRow(rowNo).getCell(column);
  558. }
  559. @Test
  560. public void test59736() {
  561. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("59736.xlsx");
  562. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  563. Cell cell = wb.getSheetAt(0).getRow(0).getCell(0);
  564. assertEquals(1, cell.getNumericCellValue(), 0.001);
  565. cell = wb.getSheetAt(0).getRow(1).getCell(0);
  566. CellValue value = evaluator.evaluate(cell);
  567. assertEquals(1, value.getNumberValue(), 0.001);
  568. cell = wb.getSheetAt(0).getRow(2).getCell(0);
  569. value = evaluator.evaluate(cell);
  570. assertEquals(1, value.getNumberValue(), 0.001);
  571. }
  572. @Test
  573. public void evaluateInCellReturnsSameDataType() throws IOException {
  574. XSSFWorkbook wb = new XSSFWorkbook();
  575. wb.createSheet().createRow(0).createCell(0);
  576. XSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  577. XSSFCell cell = wb.getSheetAt(0).getRow(0).getCell(0);
  578. XSSFCell same = evaluator.evaluateInCell(cell);
  579. assertSame(cell, same);
  580. wb.close();
  581. }
  582. @Test
  583. public void testBug61468() {
  584. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("simple-monthly-budget.xlsx");
  585. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  586. Cell cell = wb.getSheetAt(0).getRow(8).getCell(4);
  587. assertEquals(3750, cell.getNumericCellValue(), 0.001);
  588. CellValue value = evaluator.evaluate(cell);
  589. assertEquals(3750, value.getNumberValue(), 0.001);
  590. }
  591. }