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

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