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.

TestXSSFEvaluationWorkbook.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.jupiter.api.Assertions.assertEquals;
  17. import org.apache.poi.ss.usermodel.Cell;
  18. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  19. import org.apache.poi.ss.usermodel.Row;
  20. import org.apache.poi.ss.usermodel.Sheet;
  21. import org.apache.poi.ss.usermodel.Workbook;
  22. import org.apache.poi.ss.util.CellRangeAddress;
  23. import org.junit.jupiter.api.Test;
  24. class TestXSSFEvaluationWorkbook {
  25. @Test
  26. void testRefToBlankCellInArrayFormula() {
  27. Workbook wb = new XSSFWorkbook();
  28. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  29. verifySheet(wb, formulaEvaluator);
  30. verifySheet(wb, formulaEvaluator);
  31. wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
  32. }
  33. private void verifySheet(Workbook wb, FormulaEvaluator formulaEvaluator) {
  34. Sheet sheet = wb.createSheet();
  35. Row row = sheet.createRow(0);
  36. Cell cellA1 = row.createCell(0);
  37. Cell cellB1 = row.createCell(1);
  38. Cell cellC1 = row.createCell(2);
  39. Row row2 = sheet.createRow(1);
  40. Cell cellA2 = row2.createCell(0);
  41. Cell cellB2 = row2.createCell(1);
  42. Cell cellC2 = row2.createCell(2);
  43. Row row3 = sheet.createRow(2);
  44. Cell cellA3 = row3.createCell(0);
  45. Cell cellB3 = row3.createCell(1);
  46. Cell cellC3 = row3.createCell(2);
  47. cellA1.setCellValue("1");
  48. // cell B1 intentionally left blank
  49. cellC1.setCellValue("3");
  50. cellA2.setCellFormula("A1");
  51. cellB2.setCellFormula("B1");
  52. cellC2.setCellFormula("C1");
  53. sheet.setArrayFormula("A1:C1", CellRangeAddress.valueOf("A3:C3"));
  54. formulaEvaluator.evaluateAll();
  55. assertEquals("1", cellA2.getStringCellValue());
  56. assertEquals(0,cellB2.getNumericCellValue(), 0.00001);
  57. assertEquals("3",cellC2.getStringCellValue());
  58. assertEquals("1", cellA3.getStringCellValue());
  59. assertEquals(0,cellB3.getNumericCellValue(), 0.00001);
  60. assertEquals("3",cellC3.getStringCellValue());
  61. }
  62. }