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.

TestXSSFSheetUpdateArrayFormulas.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 static org.junit.jupiter.api.Assertions.assertFalse;
  18. import static org.junit.jupiter.api.Assertions.assertNotNull;
  19. import static org.junit.jupiter.api.Assertions.assertNull;
  20. import static org.junit.jupiter.api.Assertions.assertSame;
  21. import java.io.IOException;
  22. import org.apache.poi.ss.usermodel.BaseTestSheetUpdateArrayFormulas;
  23. import org.apache.poi.ss.usermodel.CellRange;
  24. import org.apache.poi.ss.util.CellRangeAddress;
  25. import org.apache.poi.xssf.XSSFITestDataProvider;
  26. import org.junit.jupiter.api.Test;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
  28. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellFormulaType;
  30. /**
  31. * Test array formulas in XSSF
  32. */
  33. public final class TestXSSFSheetUpdateArrayFormulas extends BaseTestSheetUpdateArrayFormulas {
  34. public TestXSSFSheetUpdateArrayFormulas() {
  35. super(XSSFITestDataProvider.instance);
  36. }
  37. // Test methods common with HSSF are in superclass
  38. // Local methods here test XSSF-specific details of updating array formulas
  39. @Test
  40. void testXSSFSetArrayFormula_singleCell() throws IOException {
  41. CellRange<XSSFCell> cells;
  42. XSSFWorkbook workbook = new XSSFWorkbook();
  43. XSSFSheet sheet = workbook.createSheet();
  44. // 1. single-cell array formula
  45. String formula1 = "123";
  46. CellRangeAddress range = CellRangeAddress.valueOf("C3:C3");
  47. cells = sheet.setArrayFormula(formula1, range);
  48. assertEquals(1, cells.size());
  49. // check getFirstCell...
  50. XSSFCell firstCell = cells.getTopLeftCell();
  51. assertSame(firstCell, sheet.getFirstCellInArrayFormula(firstCell));
  52. //retrieve the range and check it is the same
  53. assertEquals(range.formatAsString(), firstCell.getArrayFormulaRange().formatAsString());
  54. confirmArrayFormulaCell(firstCell, "C3", formula1, "C3");
  55. workbook.close();
  56. }
  57. @Test
  58. void testXSSFSetArrayFormula_multiCell() throws IOException {
  59. CellRange<XSSFCell> cells;
  60. String formula2 = "456";
  61. XSSFWorkbook workbook = new XSSFWorkbook();
  62. XSSFSheet sheet = workbook.createSheet();
  63. CellRangeAddress range = CellRangeAddress.valueOf("C4:C6");
  64. cells = sheet.setArrayFormula(formula2, range);
  65. assertEquals(3, cells.size());
  66. // sheet.setArrayFormula creates rows and cells for the designated range
  67. /*
  68. * From the spec:
  69. * For a multi-cell formula, the c elements for all cells except the top-left
  70. * cell in that range shall not have an f element;
  71. */
  72. // Check that each cell exists and that the formula text is set correctly on the first cell
  73. XSSFCell firstCell = cells.getTopLeftCell();
  74. confirmArrayFormulaCell(firstCell, "C4", formula2, "C4:C6");
  75. confirmArrayFormulaCell(cells.getCell(1, 0), "C5");
  76. confirmArrayFormulaCell(cells.getCell(2, 0), "C6");
  77. assertSame(firstCell, sheet.getFirstCellInArrayFormula(firstCell));
  78. workbook.close();
  79. }
  80. private static void confirmArrayFormulaCell(XSSFCell c, String cellRef) {
  81. confirmArrayFormulaCell(c, cellRef, null, null);
  82. }
  83. private static void confirmArrayFormulaCell(XSSFCell c, String cellRef, String formulaText, String arrayRangeRef) {
  84. assertNotNull(c, "Cell should not be null.");
  85. CTCell ctCell = c.getCTCell();
  86. assertEquals(cellRef, ctCell.getR());
  87. if (formulaText == null) {
  88. assertFalse(ctCell.isSetF());
  89. assertNull(ctCell.getF());
  90. } else {
  91. CTCellFormula f = ctCell.getF();
  92. assertEquals(arrayRangeRef, f.getRef());
  93. assertEquals(formulaText, f.getStringValue());
  94. assertEquals(STCellFormulaType.ARRAY, f.getT());
  95. }
  96. }
  97. }