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

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