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.

TestXSSFSheetShiftRows.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 java.io.IOException;
  17. import org.apache.poi.ss.usermodel.BaseTestSheetShiftRows;
  18. import org.apache.poi.ss.usermodel.Cell;
  19. import org.apache.poi.ss.usermodel.Comment;
  20. import org.apache.poi.ss.usermodel.Row;
  21. import org.apache.poi.ss.usermodel.Sheet;
  22. import org.apache.poi.ss.usermodel.Workbook;
  23. import org.apache.poi.ss.util.CellRangeAddress;
  24. import org.apache.poi.ss.util.CellUtil;
  25. import org.apache.poi.xssf.XSSFITestDataProvider;
  26. import org.apache.poi.xssf.XSSFTestDataSamples;
  27. /**
  28. * @author Yegor Kozlov
  29. */
  30. public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows {
  31. public TestXSSFSheetShiftRows(){
  32. super(XSSFITestDataProvider.instance);
  33. }
  34. @Override
  35. public void testShiftRowBreaks() { // disabled test from superclass
  36. // TODO - support shifting of page breaks
  37. }
  38. @Override
  39. public void testShiftWithComments() { // disabled test from superclass
  40. // TODO - support shifting of comments.
  41. }
  42. public void testBug54524() throws IOException {
  43. XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("54524.xlsx");
  44. XSSFSheet sheet = workbook.getSheetAt(0);
  45. sheet.shiftRows(3, 5, -1);
  46. Cell cell = CellUtil.getCell(sheet.getRow(1), 0);
  47. assertEquals(1.0, cell.getNumericCellValue());
  48. cell = CellUtil.getCell(sheet.getRow(2), 0);
  49. assertEquals("SUM(A2:A2)", cell.getCellFormula());
  50. cell = CellUtil.getCell(sheet.getRow(3), 0);
  51. assertEquals("X", cell.getStringCellValue());
  52. }
  53. public void testBug53798() throws IOException {
  54. // NOTE that for HSSF (.xls) negative shifts combined with positive ones do work as expected
  55. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53798.xlsx");
  56. Sheet testSheet = wb.getSheetAt(0);
  57. // 1) corrupted xlsx (unreadable data in the first row of a shifted group) already comes about
  58. // when shifted by less than -1 negative amount (try -2)
  59. testSheet.shiftRows(3, 3, -2);
  60. Row newRow = null; Cell newCell = null;
  61. // 2) attempt to create a new row IN PLACE of a removed row by a negative shift causes corrupted
  62. // xlsx file with unreadable data in the negative shifted row.
  63. // NOTE it's ok to create any other row.
  64. newRow = testSheet.createRow(3);
  65. newCell = newRow.createCell(0);
  66. newCell.setCellValue("new Cell in row "+newRow.getRowNum());
  67. // 3) once a negative shift has been made any attempt to shift another group of rows
  68. // (note: outside of previously negative shifted rows) by a POSITIVE amount causes POI exception:
  69. // org.apache.xmlbeans.impl.values.XmlValueDisconnectedException.
  70. // NOTE: another negative shift on another group of rows is successful, provided no new rows in
  71. // place of previously shifted rows were attempted to be created as explained above.
  72. testSheet.shiftRows(6, 7, 1); // -- CHANGE the shift to positive once the behaviour of
  73. // the above has been tested
  74. //saveReport(wb, new File("/tmp/53798.xlsx"));
  75. Workbook read = XSSFTestDataSamples.writeOutAndReadBack(wb);
  76. assertNotNull(read);
  77. Sheet readSheet = read.getSheetAt(0);
  78. verifyCellContent(readSheet, 0, "0.0");
  79. verifyCellContent(readSheet, 1, "3.0");
  80. verifyCellContent(readSheet, 2, "2.0");
  81. verifyCellContent(readSheet, 3, "new Cell in row 3");
  82. verifyCellContent(readSheet, 4, "4.0");
  83. verifyCellContent(readSheet, 5, "5.0");
  84. verifyCellContent(readSheet, 6, null);
  85. verifyCellContent(readSheet, 7, "6.0");
  86. verifyCellContent(readSheet, 8, "7.0");
  87. }
  88. private void verifyCellContent(Sheet readSheet, int row, String expect) {
  89. Row readRow = readSheet.getRow(row);
  90. if(expect == null) {
  91. assertNull(readRow);
  92. return;
  93. }
  94. Cell readCell = readRow.getCell(0);
  95. if(readCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
  96. assertEquals(expect, Double.toString(readCell.getNumericCellValue()));
  97. } else {
  98. assertEquals(expect, readCell.getStringCellValue());
  99. }
  100. }
  101. public void testBug53798a() throws IOException {
  102. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53798.xlsx");
  103. Sheet testSheet = wb.getSheetAt(0);
  104. testSheet.shiftRows(3, 3, -1);
  105. for (Row r : testSheet) {
  106. r.getRowNum();
  107. }
  108. testSheet.shiftRows(6, 6, 1);
  109. //saveReport(wb, new File("/tmp/53798.xlsx"));
  110. Workbook read = XSSFTestDataSamples.writeOutAndReadBack(wb);
  111. assertNotNull(read);
  112. Sheet readSheet = read.getSheetAt(0);
  113. verifyCellContent(readSheet, 0, "0.0");
  114. verifyCellContent(readSheet, 1, "1.0");
  115. verifyCellContent(readSheet, 2, "3.0");
  116. verifyCellContent(readSheet, 3, null);
  117. verifyCellContent(readSheet, 4, "4.0");
  118. verifyCellContent(readSheet, 5, "5.0");
  119. verifyCellContent(readSheet, 6, null);
  120. verifyCellContent(readSheet, 7, "6.0");
  121. verifyCellContent(readSheet, 8, "8.0");
  122. }
  123. public void testBug56017() throws IOException {
  124. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56017.xlsx");
  125. Sheet sheet = wb.getSheetAt(0);
  126. Comment comment = sheet.getCellComment(0, 0);
  127. assertNotNull(comment);
  128. assertEquals("Amdocs", comment.getAuthor());
  129. assertEquals("Amdocs:\ntest\n", comment.getString().getString());
  130. sheet.shiftRows(0, 1, 1);
  131. // comment in row 0 is gone
  132. comment = sheet.getCellComment(0, 0);
  133. assertNull(comment);
  134. // comment is now in row 1
  135. comment = sheet.getCellComment(1, 0);
  136. assertNotNull(comment);
  137. assertEquals("Amdocs", comment.getAuthor());
  138. assertEquals("Amdocs:\ntest\n", comment.getString().getString());
  139. // FileOutputStream outputStream = new FileOutputStream("/tmp/56017.xlsx");
  140. // try {
  141. // wb.write(outputStream);
  142. // } finally {
  143. // outputStream.close();
  144. // }
  145. Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  146. assertNotNull(wbBack);
  147. Sheet sheetBack = wbBack.getSheetAt(0);
  148. // comment in row 0 is gone
  149. comment = sheetBack.getCellComment(0, 0);
  150. assertNull(comment);
  151. // comment is now in row 1
  152. comment = sheetBack.getCellComment(1, 0);
  153. assertNotNull(comment);
  154. assertEquals("Amdocs", comment.getAuthor());
  155. assertEquals("Amdocs:\ntest\n", comment.getString().getString());
  156. }
  157. public void testBug55280() throws IOException {
  158. Workbook w = new XSSFWorkbook();
  159. try {
  160. Sheet s = w.createSheet();
  161. for (int row = 0; row < 5000; ++row)
  162. s.addMergedRegion(new CellRangeAddress(row, row, 0, 3));
  163. s.shiftRows(0, 4999, 1); // takes a long time...
  164. } finally {
  165. w.close();
  166. }
  167. }
  168. public void test57171() throws Exception {
  169. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
  170. assertEquals(5, wb.getActiveSheetIndex());
  171. removeAllSheetsBut(5, wb); // 5 is the active / selected sheet
  172. assertEquals(0, wb.getActiveSheetIndex());
  173. Workbook wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
  174. assertEquals(0, wbRead.getActiveSheetIndex());
  175. wbRead.removeSheetAt(0);
  176. assertEquals(0, wbRead.getActiveSheetIndex());
  177. //wb.write(new FileOutputStream("/tmp/57171.xls"));
  178. }
  179. public void test57163() throws IOException {
  180. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
  181. assertEquals(5, wb.getActiveSheetIndex());
  182. wb.removeSheetAt(0);
  183. assertEquals(4, wb.getActiveSheetIndex());
  184. //wb.write(new FileOutputStream("/tmp/57163.xls"));
  185. }
  186. public void testSetSheetOrderAndAdjustActiveSheet() throws Exception {
  187. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
  188. assertEquals(5, wb.getActiveSheetIndex());
  189. // move the sheets around in all possible combinations to check that the active sheet
  190. // is set correctly in all cases
  191. wb.setSheetOrder(wb.getSheetName(5), 4);
  192. assertEquals(4, wb.getActiveSheetIndex());
  193. wb.setSheetOrder(wb.getSheetName(5), 5);
  194. assertEquals(4, wb.getActiveSheetIndex());
  195. wb.setSheetOrder(wb.getSheetName(3), 5);
  196. assertEquals(3, wb.getActiveSheetIndex());
  197. wb.setSheetOrder(wb.getSheetName(4), 5);
  198. assertEquals(3, wb.getActiveSheetIndex());
  199. wb.setSheetOrder(wb.getSheetName(2), 2);
  200. assertEquals(3, wb.getActiveSheetIndex());
  201. wb.setSheetOrder(wb.getSheetName(2), 1);
  202. assertEquals(3, wb.getActiveSheetIndex());
  203. wb.setSheetOrder(wb.getSheetName(3), 5);
  204. assertEquals(5, wb.getActiveSheetIndex());
  205. wb.setSheetOrder(wb.getSheetName(0), 5);
  206. assertEquals(4, wb.getActiveSheetIndex());
  207. wb.setSheetOrder(wb.getSheetName(0), 5);
  208. assertEquals(3, wb.getActiveSheetIndex());
  209. wb.setSheetOrder(wb.getSheetName(0), 5);
  210. assertEquals(2, wb.getActiveSheetIndex());
  211. wb.setSheetOrder(wb.getSheetName(0), 5);
  212. assertEquals(1, wb.getActiveSheetIndex());
  213. wb.setSheetOrder(wb.getSheetName(0), 5);
  214. assertEquals(0, wb.getActiveSheetIndex());
  215. wb.setSheetOrder(wb.getSheetName(0), 5);
  216. assertEquals(5, wb.getActiveSheetIndex());
  217. }
  218. public void testRemoveSheetAndAdjustActiveSheet() throws Exception {
  219. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
  220. assertEquals(5, wb.getActiveSheetIndex());
  221. wb.removeSheetAt(0);
  222. assertEquals(4, wb.getActiveSheetIndex());
  223. wb.setActiveSheet(3);
  224. assertEquals(3, wb.getActiveSheetIndex());
  225. wb.removeSheetAt(4);
  226. assertEquals(3, wb.getActiveSheetIndex());
  227. wb.removeSheetAt(3);
  228. assertEquals(2, wb.getActiveSheetIndex());
  229. wb.removeSheetAt(0);
  230. assertEquals(1, wb.getActiveSheetIndex());
  231. wb.removeSheetAt(1);
  232. assertEquals(0, wb.getActiveSheetIndex());
  233. wb.removeSheetAt(0);
  234. assertEquals(0, wb.getActiveSheetIndex());
  235. try {
  236. wb.removeSheetAt(0);
  237. fail("Should catch exception as no more sheets are there");
  238. } catch (IllegalArgumentException e) {
  239. // expected
  240. }
  241. assertEquals(0, wb.getActiveSheetIndex());
  242. wb.createSheet();
  243. assertEquals(0, wb.getActiveSheetIndex());
  244. wb.removeSheetAt(0);
  245. assertEquals(0, wb.getActiveSheetIndex());
  246. }
  247. // TODO: enable when bug 57165 is fixed
  248. public void disabled_test57165() throws IOException {
  249. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
  250. assertEquals(5, wb.getActiveSheetIndex());
  251. removeAllSheetsBut(3, wb);
  252. assertEquals(0, wb.getActiveSheetIndex());
  253. wb.createSheet("New Sheet1");
  254. assertEquals(0, wb.getActiveSheetIndex());
  255. wb.cloneSheet(0); // Throws exception here
  256. wb.setSheetName(1, "New Sheet");
  257. assertEquals(0, wb.getActiveSheetIndex());
  258. //wb.write(new FileOutputStream("/tmp/57165.xls"));
  259. }
  260. // public void test57165b() throws IOException {
  261. // Workbook wb = new XSSFWorkbook();
  262. // try {
  263. // wb.createSheet("New Sheet 1");
  264. // wb.createSheet("New Sheet 2");
  265. // } finally {
  266. // wb.close();
  267. // }
  268. // }
  269. private static void removeAllSheetsBut(int sheetIndex, Workbook wb)
  270. {
  271. int sheetNb = wb.getNumberOfSheets();
  272. // Move this sheet at the first position
  273. wb.setSheetOrder(wb.getSheetName(sheetIndex), 0);
  274. // Must make this sheet active (otherwise, for XLSX, Excel might protest that active sheet no longer exists)
  275. // I think POI should automatically handle this case when deleting sheets...
  276. // wb.setActiveSheet(0);
  277. for (int sn = sheetNb - 1; sn > 0; sn--)
  278. {
  279. wb.removeSheetAt(sn);
  280. }
  281. }
  282. }