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.

TestXSSFSheetShiftRowsAndColumns.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.assertTrue;
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.Locale;
  23. import org.apache.poi.ss.usermodel.CellType;
  24. import org.apache.poi.ss.util.CellAddress;
  25. import org.apache.poi.ss.util.CellRangeAddress;
  26. import org.junit.jupiter.api.AfterEach;
  27. import org.junit.jupiter.api.BeforeEach;
  28. import org.junit.jupiter.api.Test;
  29. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
  30. class TestXSSFSheetShiftRowsAndColumns {
  31. private static final File resultDir = new File("build/custom-reports-test");
  32. private static final int numRows = 4;
  33. private static final int numCols = 4;
  34. private static final int INSERT_ROW = 1;
  35. private static final int INSERT_COLUMN = 1;
  36. private static final int FIRST_MERGE_ROW = INSERT_ROW+1;
  37. private static final int LAST_MERGE_ROW = numRows-1;
  38. private static final int FIRST_MERGE_COL = INSERT_COLUMN+1;
  39. private static final int LAST_MERGE_COL = numCols-1;
  40. private XSSFWorkbook workbook = null;
  41. private XSSFSheet sheet = null;
  42. private String fileName = null;
  43. public TestXSSFSheetShiftRowsAndColumns() {
  44. assertTrue(resultDir.exists() || resultDir.mkdirs(), "Failed to create directory " + resultDir);
  45. }
  46. /**
  47. * This creates a workbook with one worksheet. It then puts data in rows 0 to numRows-1 and colulmns
  48. * 0 to numCols-1.
  49. */
  50. @BeforeEach
  51. void setup() throws IOException {
  52. final String procName = "TestXSSFSheetShiftRowsAndColumns.setup";
  53. fileName = procName+".xlsx";
  54. workbook = new XSSFWorkbook();
  55. sheet = workbook.createSheet();
  56. for (int nRow = 0; nRow < numRows; ++nRow) {
  57. final XSSFRow row = sheet.createRow(nRow);
  58. for (int nCol = 0; nCol < numCols; ++nCol) {
  59. final XSSFCell cell = row.createCell(nCol);
  60. cell.setCellType(CellType.STRING);
  61. cell.setCellValue(String.format(Locale.US, "Row %d col %d", nRow, nCol));
  62. }
  63. }
  64. /*
  65. * Add a merge area
  66. */
  67. final CellRangeAddress range = new CellRangeAddress(FIRST_MERGE_ROW,LAST_MERGE_ROW,FIRST_MERGE_COL,LAST_MERGE_COL);
  68. sheet.addMergedRegion(range);
  69. writeFile(procName);
  70. }
  71. /**
  72. * This method writes the workbook to resultDir/fileName.
  73. */
  74. @AfterEach
  75. void cleanup() throws IOException {
  76. final String procName = "TestXSSFSheetRemoveTable.cleanup";
  77. if (workbook == null) {
  78. return;
  79. }
  80. if(fileName == null) {
  81. return;
  82. }
  83. writeFile(procName);
  84. workbook.close();
  85. }
  86. private void writeFile(String procName) throws IOException {
  87. final File file = new File(resultDir,fileName);
  88. try (OutputStream fileOut = new FileOutputStream(file)) {
  89. workbook.write(fileOut);
  90. }
  91. }
  92. /**
  93. * Apply no shift. The purpose of this is to test {@code testCellAddresses} and {@code testMergeRegion}.
  94. */
  95. @Test
  96. void testNoShift() {
  97. final String procName = "testNoShift";
  98. fileName = procName+".xlsx";
  99. testCellAddresses(procName,0,0);
  100. testMergeRegion(procName,0,0);
  101. }
  102. @Test
  103. void testShiftOneRowAndTestAddresses() {
  104. final String procName = "testShiftOneRowAndTestAddresses";
  105. fileName = procName+".xlsx";
  106. final int nRowsToShift = 1;
  107. sheet.shiftRows(INSERT_ROW, numRows-1, nRowsToShift);
  108. testCellAddresses(procName,nRowsToShift,0);
  109. }
  110. @Test
  111. void testShiftOneRowAndTestMergeRegion() {
  112. final String procName = "testShiftOneRowAndTestMergeRegion";
  113. fileName = procName+".xlsx";
  114. final int nRowsToShift = 1;
  115. sheet.shiftRows(INSERT_ROW, numRows-1, nRowsToShift);
  116. testMergeRegion(procName,nRowsToShift,0);
  117. }
  118. @Test
  119. void testShiftOneColumnAndTestAddresses() {
  120. final String procName = "testShiftOneColumnAndTestAddresses";
  121. fileName = procName+".xlsx";
  122. final int nShift = 1;
  123. sheet.shiftColumns(INSERT_COLUMN, numCols-1, nShift);
  124. testCellAddresses(procName,0,nShift);
  125. }
  126. @Test
  127. void testShiftOneColumnAndTestMergeRegion() {
  128. final String procName = "testShiftOneColumnAndTestMergeRegion";
  129. fileName = procName+".xlsx";
  130. final int nShift = 1;
  131. sheet.shiftColumns(INSERT_COLUMN, numCols-1, nShift);
  132. testMergeRegion(procName,0, nShift);
  133. }
  134. /**
  135. * Verify that the cell addresses are consistent
  136. */
  137. private void testCellAddresses(String procName,int nRowsToShift,int nColsToShift) {
  138. final int nNumRows = nRowsToShift+ numCols;
  139. final int nNumCols = nColsToShift+ numCols;
  140. for(int nRow = 0;nRow<nNumRows;++nRow) {
  141. final XSSFRow row = sheet.getRow(nRow);
  142. if(row == null) {
  143. continue;
  144. }
  145. for(int nCol = 0;nCol<nNumCols;++nCol) {
  146. final String address = new CellAddress(nRow,nCol).formatAsString();
  147. final XSSFCell cell = row.getCell(nCol);
  148. if(cell == null) {
  149. continue;
  150. }
  151. final CTCell ctCell = cell.getCTCell();
  152. final Object cellAddress = cell.getAddress().formatAsString();
  153. final Object r = ctCell.getR();
  154. assertEquals(address,cellAddress,String.format(Locale.US, "%s: Testing cell.getAddress",procName));
  155. assertEquals(address,r,String.format(Locale.US, "%s: Testing ctCell.getR",procName));
  156. }
  157. }
  158. }
  159. /**
  160. * Verify that the merge area is consistent
  161. */
  162. private void testMergeRegion(String procName,int nRowsToShift,int nColsToShift) {
  163. final CellRangeAddress range = sheet.getMergedRegion(0);
  164. assertEquals(new CellRangeAddress(FIRST_MERGE_ROW + nRowsToShift, LAST_MERGE_ROW + nRowsToShift,
  165. FIRST_MERGE_COL + nColsToShift, LAST_MERGE_COL + nColsToShift), range,
  166. String.format(Locale.US, "%s: Testing merge area %s", procName, range));
  167. }
  168. }