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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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. }
  216. /**
  217. * A handful of functions (such as SUM, COUNTA, MIN) support
  218. * multi-sheet references (eg Sheet1:Sheet3!A1 = Cell A1 from
  219. * Sheets 1 through Sheet 3).
  220. * This test, based on common test files for HSSF and XSSF, checks
  221. * that we can correctly evaluate these
  222. */
  223. @Test
  224. public void testMultiSheetReferencesHSSFandXSSF() throws Exception {
  225. Workbook wb1 = HSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xls");
  226. Workbook wb2 = XSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xlsx");
  227. for (Workbook wb : new Workbook[] {wb1,wb2}) {
  228. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  229. Sheet s1 = wb.getSheetAt(0);
  230. // Simple SUM over numbers
  231. Cell sumF = s1.getRow(2).getCell(0);
  232. assertNotNull(sumF);
  233. assertEquals("SUM(Sheet1:Sheet3!A1)", sumF.getCellFormula());
  234. assertEquals("Failed for " + wb.getClass(), "66.0", evaluator.evaluate(sumF).formatAsString());
  235. // Various Stats formulas on numbers
  236. Cell avgF = s1.getRow(2).getCell(1);
  237. assertNotNull(avgF);
  238. assertEquals("AVERAGE(Sheet1:Sheet3!A1)", avgF.getCellFormula());
  239. assertEquals("22.0", evaluator.evaluate(avgF).formatAsString());
  240. Cell minF = s1.getRow(3).getCell(1);
  241. assertNotNull(minF);
  242. assertEquals("MIN(Sheet1:Sheet3!A$1)", minF.getCellFormula());
  243. assertEquals("11.0", evaluator.evaluate(minF).formatAsString());
  244. Cell maxF = s1.getRow(4).getCell(1);
  245. assertNotNull(maxF);
  246. assertEquals("MAX(Sheet1:Sheet3!A$1)", maxF.getCellFormula());
  247. assertEquals("33.0", evaluator.evaluate(maxF).formatAsString());
  248. Cell countF = s1.getRow(5).getCell(1);
  249. assertNotNull(countF);
  250. assertEquals("COUNT(Sheet1:Sheet3!A$1)", countF.getCellFormula());
  251. assertEquals("3.0", evaluator.evaluate(countF).formatAsString());
  252. // Various CountAs on Strings
  253. Cell countA_1F = s1.getRow(2).getCell(2);
  254. assertNotNull(countA_1F);
  255. assertEquals("COUNTA(Sheet1:Sheet3!C1)", countA_1F.getCellFormula());
  256. assertEquals("3.0", evaluator.evaluate(countA_1F).formatAsString());
  257. Cell countA_2F = s1.getRow(2).getCell(3);
  258. assertNotNull(countA_2F);
  259. assertEquals("COUNTA(Sheet1:Sheet3!D1)", countA_2F.getCellFormula());
  260. assertEquals("0.0", evaluator.evaluate(countA_2F).formatAsString());
  261. Cell countA_3F = s1.getRow(2).getCell(4);
  262. assertNotNull(countA_3F);
  263. assertEquals("COUNTA(Sheet1:Sheet3!E1)", countA_3F.getCellFormula());
  264. assertEquals("3.0", evaluator.evaluate(countA_3F).formatAsString());
  265. }
  266. wb2.close();
  267. wb1.close();
  268. }
  269. /**
  270. * A handful of functions (such as SUM, COUNTA, MIN) support
  271. * multi-sheet areas (eg Sheet1:Sheet3!A1:B2 = Cell A1 to Cell B2,
  272. * from Sheets 1 through Sheet 3).
  273. * This test, based on common test files for HSSF and XSSF, checks
  274. * that we can correctly evaluate these
  275. */
  276. @Test
  277. public void testMultiSheetAreasHSSFandXSSF() throws IOException {
  278. Workbook wb1 = HSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xls");
  279. Workbook wb2 = XSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xlsx");
  280. for (Workbook wb : new Workbook[]{wb1,wb2}) {
  281. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  282. Sheet s1 = wb.getSheetAt(0);
  283. // SUM over a range
  284. Cell sumFA = s1.getRow(2).getCell(7);
  285. assertNotNull(sumFA);
  286. assertEquals("SUM(Sheet1:Sheet3!A1:B2)", sumFA.getCellFormula());
  287. assertEquals("Failed for " + wb.getClass(), "110.0", evaluator.evaluate(sumFA).formatAsString());
  288. // Various Stats formulas on ranges of numbers
  289. Cell avgFA = s1.getRow(2).getCell(8);
  290. assertNotNull(avgFA);
  291. assertEquals("AVERAGE(Sheet1:Sheet3!A1:B2)", avgFA.getCellFormula());
  292. assertEquals("27.5", evaluator.evaluate(avgFA).formatAsString());
  293. Cell minFA = s1.getRow(3).getCell(8);
  294. assertNotNull(minFA);
  295. assertEquals("MIN(Sheet1:Sheet3!A$1:B$2)", minFA.getCellFormula());
  296. assertEquals("11.0", evaluator.evaluate(minFA).formatAsString());
  297. Cell maxFA = s1.getRow(4).getCell(8);
  298. assertNotNull(maxFA);
  299. assertEquals("MAX(Sheet1:Sheet3!A$1:B$2)", maxFA.getCellFormula());
  300. assertEquals("44.0", evaluator.evaluate(maxFA).formatAsString());
  301. Cell countFA = s1.getRow(5).getCell(8);
  302. assertNotNull(countFA);
  303. assertEquals("COUNT(Sheet1:Sheet3!$A$1:$B$2)", countFA.getCellFormula());
  304. assertEquals("4.0", evaluator.evaluate(countFA).formatAsString());
  305. }
  306. wb2.close();
  307. wb1.close();
  308. }
  309. @Test
  310. public void testMultisheetFormulaEval() throws IOException {
  311. XSSFWorkbook wb = new XSSFWorkbook();
  312. try {
  313. XSSFSheet sheet1 = wb.createSheet("Sheet1");
  314. XSSFSheet sheet2 = wb.createSheet("Sheet2");
  315. XSSFSheet sheet3 = wb.createSheet("Sheet3");
  316. // sheet1 A1
  317. XSSFCell cell = sheet1.createRow(0).createCell(0);
  318. cell.setCellType(CellType.NUMERIC);
  319. cell.setCellValue(1.0);
  320. // sheet2 A1
  321. cell = sheet2.createRow(0).createCell(0);
  322. cell.setCellType(CellType.NUMERIC);
  323. cell.setCellValue(1.0);
  324. // sheet2 B1
  325. cell = sheet2.getRow(0).createCell(1);
  326. cell.setCellType(CellType.NUMERIC);
  327. cell.setCellValue(1.0);
  328. // sheet3 A1
  329. cell = sheet3.createRow(0).createCell(0);
  330. cell.setCellType(CellType.NUMERIC);
  331. cell.setCellValue(1.0);
  332. // sheet1 A2 formulae
  333. cell = sheet1.createRow(1).createCell(0);
  334. cell.setCellType(CellType.FORMULA);
  335. cell.setCellFormula("SUM(Sheet1:Sheet3!A1)");
  336. // sheet1 A3 formulae
  337. cell = sheet1.createRow(2).createCell(0);
  338. cell.setCellType(CellType.FORMULA);
  339. cell.setCellFormula("SUM(Sheet1:Sheet3!A1:B1)");
  340. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  341. cell = sheet1.getRow(1).getCell(0);
  342. assertEquals(3.0, cell.getNumericCellValue(), 0);
  343. cell = sheet1.getRow(2).getCell(0);
  344. assertEquals(4.0, cell.getNumericCellValue(), 0);
  345. } finally {
  346. wb.close();
  347. }
  348. }
  349. @Test
  350. public void testBug55843() throws IOException {
  351. XSSFWorkbook wb = new XSSFWorkbook();
  352. try {
  353. XSSFSheet sheet = wb.createSheet("test");
  354. XSSFRow row = sheet.createRow(0);
  355. XSSFRow row2 = sheet.createRow(1);
  356. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  357. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  358. cellB1.setCellValue(10);
  359. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  360. cellA2.setCellFormula("IF(B1=0,\"\",((ROW()-ROW(A$1))*12))");
  361. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  362. assertEquals("12.0", evaluate.formatAsString());
  363. cellA2.setCellFormula("IF(NOT(B1=0),((ROW()-ROW(A$1))*12),\"\")");
  364. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  365. assertEquals(evaluate.toString(), evaluateN.toString());
  366. assertEquals("12.0", evaluateN.formatAsString());
  367. } finally {
  368. wb.close();
  369. }
  370. }
  371. @Test
  372. public void testBug55843a() throws IOException {
  373. XSSFWorkbook wb = new XSSFWorkbook();
  374. try {
  375. XSSFSheet sheet = wb.createSheet("test");
  376. XSSFRow row = sheet.createRow(0);
  377. XSSFRow row2 = sheet.createRow(1);
  378. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  379. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  380. cellB1.setCellValue(10);
  381. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  382. cellA2.setCellFormula("IF(B1=0,\"\",((ROW(A$1))))");
  383. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  384. assertEquals("1.0", evaluate.formatAsString());
  385. cellA2.setCellFormula("IF(NOT(B1=0),((ROW(A$1))),\"\")");
  386. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  387. assertEquals(evaluate.toString(), evaluateN.toString());
  388. assertEquals("1.0", evaluateN.formatAsString());
  389. } finally {
  390. wb.close();
  391. }
  392. }
  393. @Test
  394. public void testBug55843b() throws IOException {
  395. XSSFWorkbook wb = new XSSFWorkbook();
  396. try {
  397. XSSFSheet sheet = wb.createSheet("test");
  398. XSSFRow row = sheet.createRow(0);
  399. XSSFRow row2 = sheet.createRow(1);
  400. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  401. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  402. cellB1.setCellValue(10);
  403. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  404. cellA2.setCellFormula("IF(B1=0,\"\",((ROW())))");
  405. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  406. assertEquals("2.0", evaluate.formatAsString());
  407. cellA2.setCellFormula("IF(NOT(B1=0),((ROW())),\"\")");
  408. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  409. assertEquals(evaluate.toString(), evaluateN.toString());
  410. assertEquals("2.0", evaluateN.formatAsString());
  411. } finally {
  412. wb.close();
  413. }
  414. }
  415. @Test
  416. public void testBug55843c() throws IOException {
  417. XSSFWorkbook wb = new XSSFWorkbook();
  418. try {
  419. XSSFSheet sheet = wb.createSheet("test");
  420. XSSFRow row = sheet.createRow(0);
  421. XSSFRow row2 = sheet.createRow(1);
  422. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  423. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  424. cellB1.setCellValue(10);
  425. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  426. cellA2.setCellFormula("IF(NOT(B1=0),((ROW())))");
  427. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  428. assertEquals("2.0", evaluateN.formatAsString());
  429. } finally {
  430. wb.close();
  431. }
  432. }
  433. @Test
  434. public void testBug55843d() throws IOException {
  435. XSSFWorkbook wb = new XSSFWorkbook();
  436. try {
  437. XSSFSheet sheet = wb.createSheet("test");
  438. XSSFRow row = sheet.createRow(0);
  439. XSSFRow row2 = sheet.createRow(1);
  440. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  441. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  442. cellB1.setCellValue(10);
  443. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  444. cellA2.setCellFormula("IF(NOT(B1=0),((ROW())),\"\")");
  445. CellValue evaluateN = formulaEvaluator.evaluate(cellA2);
  446. assertEquals("2.0", evaluateN.formatAsString());
  447. } finally {
  448. wb.close();
  449. }
  450. }
  451. @Test
  452. public void testBug55843e() throws IOException {
  453. XSSFWorkbook wb = new XSSFWorkbook();
  454. try {
  455. XSSFSheet sheet = wb.createSheet("test");
  456. XSSFRow row = sheet.createRow(0);
  457. XSSFRow row2 = sheet.createRow(1);
  458. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  459. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  460. cellB1.setCellValue(10);
  461. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  462. cellA2.setCellFormula("IF(B1=0,\"\",((ROW())))");
  463. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  464. assertEquals("2.0", evaluate.formatAsString());
  465. } finally {
  466. wb.close();
  467. }
  468. }
  469. @Test
  470. public void testBug55843f() throws IOException {
  471. XSSFWorkbook wb = new XSSFWorkbook();
  472. try {
  473. XSSFSheet sheet = wb.createSheet("test");
  474. XSSFRow row = sheet.createRow(0);
  475. XSSFRow row2 = sheet.createRow(1);
  476. XSSFCell cellA2 = row2.createCell(0, CellType.FORMULA);
  477. XSSFCell cellB1 = row.createCell(1, CellType.NUMERIC);
  478. cellB1.setCellValue(10);
  479. XSSFFormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  480. cellA2.setCellFormula("IF(B1=0,\"\",IF(B1=10,3,4))");
  481. CellValue evaluate = formulaEvaluator.evaluate(cellA2);
  482. assertEquals("3.0", evaluate.formatAsString());
  483. } finally {
  484. wb.close();
  485. }
  486. }
  487. @Test
  488. public void testBug56655() throws IOException {
  489. Workbook wb = new XSSFWorkbook();
  490. Sheet sheet = wb.createSheet();
  491. setCellFormula(sheet, 0, 0, "#VALUE!");
  492. setCellFormula(sheet, 0, 1, "SUMIFS(A:A,A:A,#VALUE!)");
  493. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  494. assertEquals(CellType.ERROR, getCell(sheet, 0,0).getCachedFormulaResultTypeEnum());
  495. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0,0).getErrorCellValue());
  496. assertEquals(CellType.ERROR, getCell(sheet, 0,1).getCachedFormulaResultTypeEnum());
  497. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0,1).getErrorCellValue());
  498. wb.close();
  499. }
  500. @Test
  501. public void testBug56655a() throws IOException {
  502. Workbook wb = new XSSFWorkbook();
  503. Sheet sheet = wb.createSheet();
  504. setCellFormula(sheet, 0, 0, "B1*C1");
  505. sheet.getRow(0).createCell(1).setCellValue("A");
  506. setCellFormula(sheet, 1, 0, "B1*C1");
  507. sheet.getRow(1).createCell(1).setCellValue("A");
  508. setCellFormula(sheet, 0, 3, "SUMIFS(A:A,A:A,A2)");
  509. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  510. assertEquals(CellType.ERROR, getCell(sheet, 0, 0).getCachedFormulaResultTypeEnum());
  511. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 0).getErrorCellValue());
  512. assertEquals(CellType.ERROR, getCell(sheet, 1, 0).getCachedFormulaResultTypeEnum());
  513. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 1, 0).getErrorCellValue());
  514. assertEquals(CellType.ERROR, getCell(sheet, 0, 3).getCachedFormulaResultTypeEnum());
  515. assertEquals(FormulaError.VALUE.getCode(), getCell(sheet, 0, 3).getErrorCellValue());
  516. wb.close();
  517. }
  518. // bug 57721
  519. @Test
  520. public void structuredReferences() throws IOException {
  521. verifyAllFormulasInWorkbookCanBeEvaluated("evaluate_formula_with_structured_table_references.xlsx");
  522. }
  523. // bug 57840
  524. @Ignore("Takes over a minute to evaluate all formulas in this large workbook. Run this test when profiling for formula evaluation speed.")
  525. @Test
  526. public void testLotsOfFormulasWithStructuredReferencesToCalculatedTableColumns() throws IOException {
  527. verifyAllFormulasInWorkbookCanBeEvaluated("StructuredRefs-lots-with-lookups.xlsx");
  528. }
  529. // FIXME: use junit4 parameterization
  530. private static void verifyAllFormulasInWorkbookCanBeEvaluated(String sampleWorkbook) throws IOException {
  531. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(sampleWorkbook);
  532. XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  533. wb.close();
  534. }
  535. /**
  536. * @param row 0-based
  537. * @param column 0-based
  538. */
  539. private void setCellFormula(Sheet sheet, int row, int column, String formula) {
  540. Row r = sheet.getRow(row);
  541. if (r == null) {
  542. r = sheet.createRow(row);
  543. }
  544. Cell cell = r.getCell(column);
  545. if (cell == null) {
  546. cell = r.createCell(column);
  547. }
  548. cell.setCellType(CellType.FORMULA);
  549. cell.setCellFormula(formula);
  550. }
  551. /**
  552. * @param rowNo 0-based
  553. * @param column 0-based
  554. */
  555. private Cell getCell(Sheet sheet, int rowNo, int column) {
  556. return sheet.getRow(rowNo).getCell(column);
  557. }
  558. @Test
  559. public void test59736() {
  560. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("59736.xlsx");
  561. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  562. Cell cell = wb.getSheetAt(0).getRow(0).getCell(0);
  563. assertEquals(1, cell.getNumericCellValue(), 0.001);
  564. cell = wb.getSheetAt(0).getRow(1).getCell(0);
  565. CellValue value = evaluator.evaluate(cell);
  566. assertEquals(1, value.getNumberValue(), 0.001);
  567. cell = wb.getSheetAt(0).getRow(2).getCell(0);
  568. value = evaluator.evaluate(cell);
  569. assertEquals(1, value.getNumberValue(), 0.001);
  570. }
  571. @Test
  572. public void evaluateInCellReturnsSameDataType() throws IOException {
  573. XSSFWorkbook wb = new XSSFWorkbook();
  574. wb.createSheet().createRow(0).createCell(0);
  575. XSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  576. XSSFCell cell = wb.getSheetAt(0).getRow(0).getCell(0);
  577. XSSFCell same = evaluator.evaluateInCell(cell);
  578. assertSame(cell, same);
  579. wb.close();
  580. }
  581. }