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 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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.assertNull;
  19. import static org.junit.Assert.fail;
  20. import java.io.IOException;
  21. import org.apache.poi.ss.usermodel.BaseTestSheetShiftRows;
  22. import org.apache.poi.ss.usermodel.Cell;
  23. import org.apache.poi.ss.usermodel.CellType;
  24. import org.apache.poi.ss.usermodel.Comment;
  25. import org.apache.poi.ss.usermodel.Row;
  26. import org.apache.poi.ss.usermodel.Sheet;
  27. import org.apache.poi.ss.usermodel.Workbook;
  28. import org.apache.poi.ss.util.CellAddress;
  29. import org.apache.poi.ss.util.CellUtil;
  30. import org.apache.poi.xssf.XSSFITestDataProvider;
  31. import org.apache.poi.xssf.XSSFTestDataSamples;
  32. import org.junit.Ignore;
  33. import org.junit.Test;
  34. public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows {
  35. public TestXSSFSheetShiftRows(){
  36. super(XSSFITestDataProvider.instance);
  37. }
  38. @Override
  39. @Test
  40. public void testShiftRowBreaks() {
  41. // disabled test from superclass
  42. // TODO - support shifting of page breaks
  43. }
  44. /** Error occurred at FormulaShifter#rowMoveAreaPtg while shift rows upward. */
  45. @Test
  46. public void testBug54524() throws IOException {
  47. XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("54524.xlsx");
  48. XSSFSheet sheet = workbook.getSheetAt(0);
  49. sheet.shiftRows(3, 5, -1);
  50. Cell cell = CellUtil.getCell(sheet.getRow(1), 0);
  51. assertEquals(1.0, cell.getNumericCellValue(), 0);
  52. cell = CellUtil.getCell(sheet.getRow(2), 0);
  53. assertEquals("SUM(A2:A2)", cell.getCellFormula());
  54. cell = CellUtil.getCell(sheet.getRow(3), 0);
  55. assertEquals("X", cell.getStringCellValue());
  56. workbook.close();
  57. }
  58. /** negative row shift causes corrupted data or throws exception */
  59. @Test
  60. public void testBug53798() throws IOException {
  61. // NOTE that for HSSF (.xls) negative shifts combined with positive ones do work as expected
  62. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53798.xlsx");
  63. Sheet testSheet = wb.getSheetAt(0);
  64. // 1) corrupted xlsx (unreadable data in the first row of a shifted group) already comes about
  65. // when shifted by less than -1 negative amount (try -2)
  66. testSheet.shiftRows(3, 3, -2);
  67. // 2) attempt to create a new row IN PLACE of a removed row by a negative shift causes corrupted
  68. // xlsx file with unreadable data in the negative shifted row.
  69. // NOTE it's ok to create any other row.
  70. Row newRow = testSheet.createRow(3);
  71. Cell newCell = newRow.createCell(0);
  72. newCell.setCellValue("new Cell in row "+newRow.getRowNum());
  73. // 3) once a negative shift has been made any attempt to shift another group of rows
  74. // (note: outside of previously negative shifted rows) by a POSITIVE amount causes POI exception:
  75. // org.apache.xmlbeans.impl.values.XmlValueDisconnectedException.
  76. // NOTE: another negative shift on another group of rows is successful, provided no new rows in
  77. // place of previously shifted rows were attempted to be created as explained above.
  78. // -- CHANGE the shift to positive once the behaviour of the above has been tested
  79. testSheet.shiftRows(6, 7, 1);
  80. Workbook read = XSSFTestDataSamples.writeOutAndReadBack(wb);
  81. wb.close();
  82. assertNotNull(read);
  83. Sheet readSheet = read.getSheetAt(0);
  84. verifyCellContent(readSheet, 0, "0.0");
  85. verifyCellContent(readSheet, 1, "3.0");
  86. verifyCellContent(readSheet, 2, "2.0");
  87. verifyCellContent(readSheet, 3, "new Cell in row 3");
  88. verifyCellContent(readSheet, 4, "4.0");
  89. verifyCellContent(readSheet, 5, "5.0");
  90. verifyCellContent(readSheet, 6, null);
  91. verifyCellContent(readSheet, 7, "6.0");
  92. verifyCellContent(readSheet, 8, "7.0");
  93. read.close();
  94. }
  95. private void verifyCellContent(Sheet readSheet, int row, String expect) {
  96. Row readRow = readSheet.getRow(row);
  97. if(expect == null) {
  98. assertNull(readRow);
  99. return;
  100. }
  101. Cell readCell = readRow.getCell(0);
  102. //noinspection deprecation
  103. if(readCell.getCellTypeEnum() == CellType.NUMERIC) {
  104. assertEquals(expect, Double.toString(readCell.getNumericCellValue()));
  105. } else {
  106. assertEquals(expect, readCell.getStringCellValue());
  107. }
  108. }
  109. /** negative row shift causes corrupted data or throws exception */
  110. @Test
  111. public void testBug53798a() throws IOException {
  112. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("53798.xlsx");
  113. Sheet testSheet = wb.getSheetAt(0);
  114. testSheet.shiftRows(3, 3, -1);
  115. for (Row r : testSheet) {
  116. r.getRowNum();
  117. }
  118. testSheet.shiftRows(6, 6, 1);
  119. Workbook read = XSSFTestDataSamples.writeOutAndReadBack(wb);
  120. wb.close();
  121. assertNotNull(read);
  122. Sheet readSheet = read.getSheetAt(0);
  123. verifyCellContent(readSheet, 0, "0.0");
  124. verifyCellContent(readSheet, 1, "1.0");
  125. verifyCellContent(readSheet, 2, "3.0");
  126. verifyCellContent(readSheet, 3, null);
  127. verifyCellContent(readSheet, 4, "4.0");
  128. verifyCellContent(readSheet, 5, "5.0");
  129. verifyCellContent(readSheet, 6, null);
  130. verifyCellContent(readSheet, 7, "6.0");
  131. verifyCellContent(readSheet, 8, "8.0");
  132. read.close();
  133. }
  134. /** Shifting rows with comment result - Unreadable content error and comment deletion */
  135. @Test
  136. public void testBug56017() throws IOException {
  137. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56017.xlsx");
  138. Sheet sheet = wb.getSheetAt(0);
  139. Comment comment = sheet.getCellComment(new CellAddress(0, 0));
  140. assertNotNull(comment);
  141. assertEquals("Amdocs", comment.getAuthor());
  142. assertEquals("Amdocs:\ntest\n", comment.getString().getString());
  143. sheet.shiftRows(0, 1, 1);
  144. // comment in row 0 is gone
  145. comment = sheet.getCellComment(new CellAddress(0, 0));
  146. assertNull(comment);
  147. // comment is now in row 1
  148. comment = sheet.getCellComment(new CellAddress(1, 0));
  149. assertNotNull(comment);
  150. assertEquals("Amdocs", comment.getAuthor());
  151. assertEquals("Amdocs:\ntest\n", comment.getString().getString());
  152. Workbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
  153. wb.close();
  154. assertNotNull(wbBack);
  155. Sheet sheetBack = wbBack.getSheetAt(0);
  156. // comment in row 0 is gone
  157. comment = sheetBack.getCellComment(new CellAddress(0, 0));
  158. assertNull(comment);
  159. // comment is now in row 1
  160. comment = sheetBack.getCellComment(new CellAddress(1, 0));
  161. assertNotNull(comment);
  162. assertEquals("Amdocs", comment.getAuthor());
  163. assertEquals("Amdocs:\ntest\n", comment.getString().getString());
  164. wbBack.close();
  165. }
  166. /** Moving the active sheet and deleting the others results in a corrupted file */
  167. @Test
  168. public void test57171() throws IOException {
  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. wb.close();
  175. assertEquals(0, wbRead.getActiveSheetIndex());
  176. wbRead.removeSheetAt(0);
  177. assertEquals(0, wbRead.getActiveSheetIndex());
  178. wbRead.close();
  179. }
  180. /** Cannot delete an arbitrary sheet in an XLS workbook (only the last one) */
  181. @Test
  182. public void test57163() throws IOException {
  183. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
  184. assertEquals(5, wb.getActiveSheetIndex());
  185. wb.removeSheetAt(0);
  186. assertEquals(4, wb.getActiveSheetIndex());
  187. wb.close();
  188. }
  189. @Test
  190. public void testSetSheetOrderAndAdjustActiveSheet() throws IOException {
  191. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
  192. assertEquals(5, wb.getActiveSheetIndex());
  193. // move the sheets around in all possible combinations to check that the active sheet
  194. // is set correctly in all cases
  195. wb.setSheetOrder(wb.getSheetName(5), 4);
  196. assertEquals(4, wb.getActiveSheetIndex());
  197. wb.setSheetOrder(wb.getSheetName(5), 5);
  198. assertEquals(4, wb.getActiveSheetIndex());
  199. wb.setSheetOrder(wb.getSheetName(3), 5);
  200. assertEquals(3, wb.getActiveSheetIndex());
  201. wb.setSheetOrder(wb.getSheetName(4), 5);
  202. assertEquals(3, wb.getActiveSheetIndex());
  203. wb.setSheetOrder(wb.getSheetName(2), 2);
  204. assertEquals(3, wb.getActiveSheetIndex());
  205. wb.setSheetOrder(wb.getSheetName(2), 1);
  206. assertEquals(3, wb.getActiveSheetIndex());
  207. wb.setSheetOrder(wb.getSheetName(3), 5);
  208. assertEquals(5, wb.getActiveSheetIndex());
  209. wb.setSheetOrder(wb.getSheetName(0), 5);
  210. assertEquals(4, wb.getActiveSheetIndex());
  211. wb.setSheetOrder(wb.getSheetName(0), 5);
  212. assertEquals(3, wb.getActiveSheetIndex());
  213. wb.setSheetOrder(wb.getSheetName(0), 5);
  214. assertEquals(2, wb.getActiveSheetIndex());
  215. wb.setSheetOrder(wb.getSheetName(0), 5);
  216. assertEquals(1, wb.getActiveSheetIndex());
  217. wb.setSheetOrder(wb.getSheetName(0), 5);
  218. assertEquals(0, wb.getActiveSheetIndex());
  219. wb.setSheetOrder(wb.getSheetName(0), 5);
  220. assertEquals(5, wb.getActiveSheetIndex());
  221. wb.close();
  222. }
  223. @Test
  224. public void testRemoveSheetAndAdjustActiveSheet() throws IOException {
  225. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
  226. assertEquals(5, wb.getActiveSheetIndex());
  227. wb.removeSheetAt(0);
  228. assertEquals(4, wb.getActiveSheetIndex());
  229. wb.setActiveSheet(3);
  230. assertEquals(3, wb.getActiveSheetIndex());
  231. wb.removeSheetAt(4);
  232. assertEquals(3, wb.getActiveSheetIndex());
  233. wb.removeSheetAt(3);
  234. assertEquals(2, wb.getActiveSheetIndex());
  235. wb.removeSheetAt(0);
  236. assertEquals(1, wb.getActiveSheetIndex());
  237. wb.removeSheetAt(1);
  238. assertEquals(0, wb.getActiveSheetIndex());
  239. wb.removeSheetAt(0);
  240. assertEquals(0, wb.getActiveSheetIndex());
  241. try {
  242. wb.removeSheetAt(0);
  243. fail("Should catch exception as no more sheets are there");
  244. } catch (IllegalArgumentException e) {
  245. // expected
  246. }
  247. assertEquals(0, wb.getActiveSheetIndex());
  248. wb.createSheet();
  249. assertEquals(0, wb.getActiveSheetIndex());
  250. wb.removeSheetAt(0);
  251. assertEquals(0, wb.getActiveSheetIndex());
  252. wb.close();
  253. }
  254. /** Failed to clone a sheet from an Excel 2010 */
  255. @Test
  256. public void test57165() throws IOException {
  257. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("57171_57163_57165.xlsx");
  258. assertEquals(5, wb.getActiveSheetIndex());
  259. removeAllSheetsBut(3, wb);
  260. assertEquals(0, wb.getActiveSheetIndex());
  261. wb.createSheet("New Sheet1");
  262. assertEquals(0, wb.getActiveSheetIndex());
  263. wb.cloneSheet(0); // Throws exception here
  264. wb.setSheetName(1, "New Sheet");
  265. assertEquals(0, wb.getActiveSheetIndex());
  266. wb.close();
  267. }
  268. private static void removeAllSheetsBut(int sheetIndex, Workbook wb) {
  269. int sheetNb = wb.getNumberOfSheets();
  270. // Move this sheet at the first position
  271. wb.setSheetOrder(wb.getSheetName(sheetIndex), 0);
  272. // Must make this sheet active (otherwise, for XLSX, Excel might protest that active sheet no longer exists)
  273. // I think POI should automatically handle this case when deleting sheets...
  274. // wb.setActiveSheet(0);
  275. for (int sn = sheetNb - 1; sn > 0; sn--) {
  276. wb.removeSheetAt(sn);
  277. }
  278. }
  279. /** Shifting rows with cell comments only shifts comments from first such cell. Other cell comments not shifted */
  280. @Test
  281. public void testBug57828_OnlyOneCommentShiftedInRow() throws IOException {
  282. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57828.xlsx");
  283. XSSFSheet sheet = wb.getSheetAt(0);
  284. Comment comment1 = sheet.getCellComment(new CellAddress(2, 1));
  285. assertNotNull(comment1);
  286. Comment comment2 = sheet.getCellComment(new CellAddress(2, 2));
  287. assertNotNull(comment2);
  288. Comment comment3 = sheet.getCellComment(new CellAddress(1, 1));
  289. assertNull("NO comment in (1,1) and it should be null", comment3);
  290. sheet.shiftRows(2, 2, -1);
  291. comment3 = sheet.getCellComment(new CellAddress(1, 1));
  292. assertNotNull("Comment in (2,1) moved to (1,1) so its not null now.", comment3);
  293. comment1 = sheet.getCellComment(new CellAddress(2, 1));
  294. assertNull("No comment currently in (2,1) and hence it is null", comment1);
  295. comment2 = sheet.getCellComment(new CellAddress(1, 2));
  296. assertNotNull("Comment in (2,2) should have moved as well because of shift rows. But its not", comment2);
  297. wb.close();
  298. }
  299. @Ignore("Bug 59733 - shiftRows() causes org.apache.xmlbeans.impl.values.XmlValueDisconnectedException")
  300. @Test
  301. public void bug59733() throws IOException {
  302. Workbook workbook = new XSSFWorkbook();
  303. Sheet sheet = workbook.createSheet();
  304. for (int r=0; r<4; r++) {
  305. sheet.createRow(r);
  306. }
  307. // Shift the 2nd row on top of the 0th row
  308. sheet.shiftRows(2, 2, -2);
  309. /*
  310. * The following error is thrown when shifting the 3rd row on top of the 0th row
  311. * If the rows are not created, the error does not occur
  312. org.apache.xmlbeans.impl.values.XmlValueDisconnectedException
  313. at org.apache.xmlbeans.impl.values.XmlObjectBase.check_orphaned(XmlObjectBase.java:1258)
  314. at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTRowImpl.getR(Unknown Source)
  315. at org.apache.poi.xssf.usermodel.XSSFRow.getRowNum(XSSFRow.java:363)
  316. at org.apache.poi.xssf.usermodel.TestXSSFSheetShiftRows.bug59733(TestXSSFSheetShiftRows.java:393)
  317. */
  318. sheet.removeRow(sheet.getRow(0));
  319. assertEquals(1, sheet.getRow(1).getRowNum());
  320. workbook.close();
  321. }
  322. }