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.

TestXSSFRow.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertSame;
  19. import java.io.IOException;
  20. import org.apache.poi.ss.SpreadsheetVersion;
  21. import org.apache.poi.ss.usermodel.BaseTestRow;
  22. import org.apache.poi.ss.usermodel.Cell;
  23. import org.apache.poi.ss.usermodel.CellCopyPolicy;
  24. import org.apache.poi.ss.usermodel.Row;
  25. import org.apache.poi.ss.usermodel.Sheet;
  26. import org.apache.poi.xssf.XSSFITestDataProvider;
  27. import org.junit.Test;
  28. /**
  29. * Tests for XSSFRow
  30. */
  31. public final class TestXSSFRow extends BaseTestRow {
  32. public TestXSSFRow() {
  33. super(XSSFITestDataProvider.instance);
  34. }
  35. @Test
  36. public void testRowBounds() throws IOException {
  37. baseTestRowBounds(SpreadsheetVersion.EXCEL2007.getLastRowIndex());
  38. }
  39. @Test
  40. public void testCellBounds() throws IOException {
  41. baseTestCellBounds(SpreadsheetVersion.EXCEL2007.getLastColumnIndex());
  42. }
  43. public void testCopyRowFrom() throws IOException {
  44. final XSSFWorkbook workbook = new XSSFWorkbook();
  45. final XSSFSheet sheet = workbook.createSheet("test");
  46. final XSSFRow srcRow = sheet.createRow(0);
  47. srcRow.createCell(0).setCellValue("Hello");
  48. final XSSFRow destRow = sheet.createRow(1);
  49. destRow.copyRowFrom(srcRow, new CellCopyPolicy());
  50. assertNotNull(destRow.getCell(0));
  51. assertEquals("Hello", destRow.getCell(0).getStringCellValue());
  52. workbook.close();
  53. }
  54. public void testCopyRowFromExternalSheet() throws IOException {
  55. final XSSFWorkbook workbook = new XSSFWorkbook();
  56. final Sheet srcSheet = workbook.createSheet("src");
  57. final XSSFSheet destSheet = workbook.createSheet("dest");
  58. workbook.createSheet("other");
  59. final Row srcRow = srcSheet.createRow(0);
  60. int col = 0;
  61. //Test 2D and 3D Ref Ptgs (Pxg for OOXML Workbooks)
  62. srcRow.createCell(col++).setCellFormula("B5");
  63. srcRow.createCell(col++).setCellFormula("src!B5");
  64. srcRow.createCell(col++).setCellFormula("dest!B5");
  65. srcRow.createCell(col++).setCellFormula("other!B5");
  66. //Test 2D and 3D Ref Ptgs with absolute row
  67. srcRow.createCell(col++).setCellFormula("B$5");
  68. srcRow.createCell(col++).setCellFormula("src!B$5");
  69. srcRow.createCell(col++).setCellFormula("dest!B$5");
  70. srcRow.createCell(col++).setCellFormula("other!B$5");
  71. //Test 2D and 3D Area Ptgs (Pxg for OOXML Workbooks)
  72. srcRow.createCell(col++).setCellFormula("SUM(B5:D$5)");
  73. srcRow.createCell(col++).setCellFormula("SUM(src!B5:D$5)");
  74. srcRow.createCell(col++).setCellFormula("SUM(dest!B5:D$5)");
  75. srcRow.createCell(col++).setCellFormula("SUM(other!B5:D$5)");
  76. //////////////////
  77. final XSSFRow destRow = destSheet.createRow(1);
  78. destRow.copyRowFrom(srcRow, new CellCopyPolicy());
  79. //////////////////
  80. //Test 2D and 3D Ref Ptgs (Pxg for OOXML Workbooks)
  81. col = 0;
  82. Cell cell = destRow.getCell(col++);
  83. assertNotNull(cell);
  84. assertEquals("RefPtg", "B6", cell.getCellFormula());
  85. cell = destRow.getCell(col++);
  86. assertNotNull(cell);
  87. assertEquals("Ref3DPtg", "src!B6", cell.getCellFormula());
  88. cell = destRow.getCell(col++);
  89. assertNotNull(cell);
  90. assertEquals("Ref3DPtg", "dest!B6", cell.getCellFormula());
  91. cell = destRow.getCell(col++);
  92. assertNotNull(cell);
  93. assertEquals("Ref3DPtg", "other!B6", cell.getCellFormula());
  94. /////////////////////////////////////////////
  95. //Test 2D and 3D Ref Ptgs with absolute row (Ptg row number shouldn't change)
  96. cell = destRow.getCell(col++);
  97. assertNotNull(cell);
  98. assertEquals("RefPtg", "B$5", cell.getCellFormula());
  99. cell = destRow.getCell(col++);
  100. assertNotNull(cell);
  101. assertEquals("Ref3DPtg", "src!B$5", cell.getCellFormula());
  102. cell = destRow.getCell(col++);
  103. assertNotNull(cell);
  104. assertEquals("Ref3DPtg", "dest!B$5", cell.getCellFormula());
  105. cell = destRow.getCell(col++);
  106. assertNotNull(cell);
  107. assertEquals("Ref3DPtg", "other!B$5", cell.getCellFormula());
  108. //////////////////////////////////////////
  109. //Test 2D and 3D Area Ptgs (Pxg for OOXML Workbooks)
  110. // Note: absolute row changes from last cell to first cell in order
  111. // to maintain topLeft:bottomRight order
  112. cell = destRow.getCell(col++);
  113. assertNotNull(cell);
  114. assertEquals("Area2DPtg", "SUM(B$5:D6)", cell.getCellFormula());
  115. cell = destRow.getCell(col++);
  116. assertNotNull(cell);
  117. assertEquals("Area3DPtg", "SUM(src!B$5:D6)", cell.getCellFormula());
  118. cell = destRow.getCell(col++);
  119. assertNotNull(destRow.getCell(6));
  120. assertEquals("Area3DPtg", "SUM(dest!B$5:D6)", cell.getCellFormula());
  121. cell = destRow.getCell(col++);
  122. assertNotNull(destRow.getCell(7));
  123. assertEquals("Area3DPtg", "SUM(other!B$5:D6)", cell.getCellFormula());
  124. workbook.close();
  125. }
  126. public void testCopyRowOverwritesExistingRow() throws IOException {
  127. final XSSFWorkbook workbook = new XSSFWorkbook();
  128. final XSSFSheet sheet1 = workbook.createSheet("Sheet1");
  129. final Sheet sheet2 = workbook.createSheet("Sheet2");
  130. final Row srcRow = sheet1.createRow(0);
  131. final XSSFRow destRow = sheet1.createRow(1);
  132. final Row observerRow = sheet1.createRow(2);
  133. final Row externObserverRow = sheet2.createRow(0);
  134. srcRow.createCell(0).setCellValue("hello");
  135. srcRow.createCell(1).setCellValue("world");
  136. destRow.createCell(0).setCellValue(5.0); //A2 -> 5.0
  137. destRow.createCell(1).setCellFormula("A1"); // B2 -> A1 -> "hello"
  138. observerRow.createCell(0).setCellFormula("A2"); // A3 -> A2 -> 5.0
  139. observerRow.createCell(1).setCellFormula("B2"); // B3 -> B2 -> A1 -> "hello"
  140. externObserverRow.createCell(0).setCellFormula("Sheet1!A2"); //Sheet2!A1 -> Sheet1!A2 -> 5.0
  141. // overwrite existing destRow with row-copy of srcRow
  142. destRow.copyRowFrom(srcRow, new CellCopyPolicy());
  143. // copyRowFrom should update existing destRow, rather than creating a new row and reassigning the destRow pointer
  144. // to the new row (and allow the old row to be garbage collected)
  145. // this is mostly so existing references to rows that are overwritten are updated
  146. // rather than allowing users to continue updating rows that are no longer part of the sheet
  147. assertSame("existing references to srcRow are still valid", srcRow, sheet1.getRow(0));
  148. assertSame("existing references to destRow are still valid", destRow, sheet1.getRow(1));
  149. assertSame("existing references to observerRow are still valid", observerRow, sheet1.getRow(2));
  150. assertSame("existing references to externObserverRow are still valid", externObserverRow, sheet2.getRow(0));
  151. // Make sure copyRowFrom actually copied row (this is tested elsewhere)
  152. assertEquals(Cell.CELL_TYPE_STRING, destRow.getCell(0).getCellType());
  153. assertEquals("hello", destRow.getCell(0).getStringCellValue());
  154. // We don't want #REF! errors if we copy a row that contains cells that are referred to by other cells outside of copied region
  155. assertEquals("references to overwritten cells are unmodified", "A2", observerRow.getCell(0).getCellFormula());
  156. assertEquals("references to overwritten cells are unmodified", "B2", observerRow.getCell(1).getCellFormula());
  157. assertEquals("references to overwritten cells are unmodified", "Sheet1!A2", externObserverRow.getCell(0).getCellFormula());
  158. workbook.close();
  159. }
  160. }