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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 junit.framework.TestCase;
  17. import org.apache.poi.ss.usermodel.*;
  18. import org.apache.poi.ss.util.CellReference;
  19. import org.apache.poi.xssf.XSSFTestDataSamples;
  20. import org.apache.poi.xssf.XSSFITestDataProvider;
  21. public final class TestXSSFFormulaEvaluation extends BaseTestFormulaEvaluator {
  22. public TestXSSFFormulaEvaluation() {
  23. super(XSSFITestDataProvider.instance);
  24. }
  25. public void testSharedFormulas(){
  26. baseTestSharedFormulas("shared_formulas.xlsx");
  27. }
  28. public void testSharedFormulas_evaluateInCell(){
  29. XSSFWorkbook wb = (XSSFWorkbook)_testDataProvider.openSampleWorkbook("49872.xlsx");
  30. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  31. XSSFSheet sheet = wb.getSheetAt(0);
  32. double result = 3.0;
  33. // B3 is a master shared formula, C3 and D3 don't have the formula written in their f element.
  34. // Instead, the attribute si for a particular cell is used to figure what the formula expression
  35. // should be based on the cell's relative location to the master formula, e.g.
  36. // B3: <f t="shared" ref="B3:D3" si="0">B1+B2</f>
  37. // C3 and D3: <f t="shared" si="0"/>
  38. // get B3 and evaluate it in the cell
  39. XSSFCell b3 = sheet.getRow(2).getCell(1);
  40. assertEquals(result, evaluator.evaluateInCell(b3).getNumericCellValue());
  41. //at this point the master formula is gone, but we are still able to evaluate dependent cells
  42. XSSFCell c3 = sheet.getRow(2).getCell(2);
  43. assertEquals(result, evaluator.evaluateInCell(c3).getNumericCellValue());
  44. XSSFCell d3 = sheet.getRow(2).getCell(3);
  45. assertEquals(result, evaluator.evaluateInCell(d3).getNumericCellValue());
  46. }
  47. /**
  48. * Evaluation of cell references with column indexes greater than 255. See bugzilla 50096
  49. */
  50. public void testEvaluateColumnGreaterThan255() {
  51. XSSFWorkbook wb = (XSSFWorkbook) _testDataProvider.openSampleWorkbook("50096.xlsx");
  52. XSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  53. /**
  54. * The first row simply contains the numbers 1 - 300.
  55. * The second row simply refers to the cell value above in the first row by a simple formula.
  56. */
  57. for (int i = 245; i < 265; i++) {
  58. XSSFCell cell_noformula = wb.getSheetAt(0).getRow(0).getCell(i);
  59. XSSFCell cell_formula = wb.getSheetAt(0).getRow(1).getCell(i);
  60. CellReference ref_noformula = new CellReference(cell_noformula.getRowIndex(), cell_noformula.getColumnIndex());
  61. CellReference ref_formula = new CellReference(cell_noformula.getRowIndex(), cell_noformula.getColumnIndex());
  62. String fmla = cell_formula.getCellFormula();
  63. // assure that the formula refers to the cell above.
  64. // the check below is 'deep' and involves conversion of the shared formula:
  65. // in the sample file a shared formula in GN1 is spanned in the range GN2:IY2,
  66. assertEquals(ref_noformula.formatAsString(), fmla);
  67. CellValue cv_noformula = evaluator.evaluate(cell_noformula);
  68. CellValue cv_formula = evaluator.evaluate(cell_formula);
  69. assertEquals("Wrong evaluation result in " + ref_formula.formatAsString(),
  70. cv_noformula.getNumberValue(), cv_formula.getNumberValue());
  71. }
  72. }
  73. }