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

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