Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BaseTestSheetShiftRows.java 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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.ss.usermodel;
  16. import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
  17. import static org.junit.jupiter.api.Assertions.assertEquals;
  18. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotNull;
  20. import static org.junit.jupiter.api.Assertions.assertNull;
  21. import static org.junit.jupiter.api.Assertions.assertTrue;
  22. import static org.junit.jupiter.api.Assumptions.assumeTrue;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import org.apache.poi.common.usermodel.HyperlinkType;
  27. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  28. import org.apache.poi.ss.ITestDataProvider;
  29. import org.apache.poi.ss.util.CellAddress;
  30. import org.apache.poi.ss.util.CellRangeAddress;
  31. import org.apache.poi.ss.util.CellReference;
  32. import org.junit.jupiter.api.Test;
  33. /**
  34. * Tests row shifting capabilities.
  35. */
  36. public abstract class BaseTestSheetShiftRows {
  37. private final ITestDataProvider _testDataProvider;
  38. protected BaseTestSheetShiftRows(ITestDataProvider testDataProvider) {
  39. _testDataProvider = testDataProvider;
  40. }
  41. /**
  42. * Tests the shiftRows function. Does three different shifts.
  43. * After each shift, writes the workbook to file and reads back to
  44. * check. This ensures that if some changes code that breaks
  45. * writing or what not, they realize it.
  46. */
  47. @Test
  48. public final void testShiftRows() throws IOException {
  49. // Read initial file in
  50. String sampleName = "SimpleMultiCell." + _testDataProvider.getStandardFileNameExtension();
  51. try (Workbook wb1 = _testDataProvider.openSampleWorkbook(sampleName)) {
  52. Sheet s = wb1.getSheetAt(0);
  53. // Shift the second row down 1 and write to temp file
  54. s.shiftRows(1, 1, 1);
  55. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  56. // Read from temp file and check the number of cells in each
  57. // row (in original file each row was unique)
  58. s = wb2.getSheetAt(0);
  59. assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 1);
  60. confirmEmptyRow(s, 1);
  61. assertEquals(s.getRow(2).getPhysicalNumberOfCells(), 2);
  62. assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 4);
  63. assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5);
  64. // Shift rows 1-3 down 3 in the current one. This tests when
  65. // 1 row is blank. Write to a another temp file
  66. s.shiftRows(0, 2, 3);
  67. try (Workbook wb3 = _testDataProvider.writeOutAndReadBack(wb2)) {
  68. // Read and ensure things are where they should be
  69. s = wb3.getSheetAt(0);
  70. confirmEmptyRow(s, 0);
  71. confirmEmptyRow(s, 1);
  72. confirmEmptyRow(s, 2);
  73. assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 1);
  74. confirmEmptyRow(s, 4);
  75. assertEquals(s.getRow(5).getPhysicalNumberOfCells(), 2);
  76. }
  77. }
  78. }
  79. // Read the first file again
  80. try (Workbook wb4 = _testDataProvider.openSampleWorkbook(sampleName)) {
  81. Sheet s = wb4.getSheetAt(0);
  82. // Shift rows 3 and 4 up and write to temp file
  83. s.shiftRows(2, 3, -2);
  84. try (Workbook wb5 = _testDataProvider.writeOutAndReadBack(wb4)) {
  85. s = wb5.getSheetAt(0);
  86. assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 3);
  87. assertEquals(s.getRow(1).getPhysicalNumberOfCells(), 4);
  88. confirmEmptyRow(s, 2);
  89. confirmEmptyRow(s, 3);
  90. assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5);
  91. }
  92. }
  93. }
  94. private static void confirmEmptyRow(Sheet s, int rowIx) {
  95. Row row = s.getRow(rowIx);
  96. assertTrue(row == null || row.getPhysicalNumberOfCells() == 0);
  97. }
  98. /**
  99. * Tests when rows are null.
  100. */
  101. @Test
  102. public final void testShiftRow() throws IOException {
  103. try (Workbook wb = _testDataProvider.createWorkbook()) {
  104. Sheet s = wb.createSheet();
  105. s.createRow(0).createCell(0).setCellValue("TEST1");
  106. s.createRow(3).createCell(0).setCellValue("TEST2");
  107. assertDoesNotThrow(() -> s.shiftRows(0, 4, 1));
  108. }
  109. }
  110. /**
  111. * When shifting rows, the page breaks should go with it
  112. */
  113. @Test
  114. protected void testShiftRowBreaks() throws IOException {
  115. // TODO - enable XSSF test
  116. try (Workbook wb = _testDataProvider.createWorkbook()) {
  117. Sheet s = wb.createSheet();
  118. Row row = s.createRow(4);
  119. row.createCell(0).setCellValue("test");
  120. s.setRowBreak(4);
  121. s.shiftRows(4, 4, 2);
  122. assertTrue(s.isRowBroken(6), "Row number 6 should have a pagebreak");
  123. }
  124. }
  125. @Test
  126. void testShiftWithComments() throws IOException {
  127. try (Workbook wb1 = _testDataProvider.openSampleWorkbook("comments." + _testDataProvider.getStandardFileNameExtension())) {
  128. Sheet sheet = wb1.getSheet("Sheet1");
  129. assertEquals(3, sheet.getLastRowNum());
  130. // Verify comments are in the position expected
  131. assertNotNull(sheet.getCellComment(new CellAddress(0, 0)));
  132. assertNull(sheet.getCellComment(new CellAddress(1, 0)));
  133. assertNotNull(sheet.getCellComment(new CellAddress(2, 0)));
  134. assertNotNull(sheet.getCellComment(new CellAddress(3, 0)));
  135. String comment1 = sheet.getCellComment(new CellAddress(0, 0)).getString().getString();
  136. assertEquals(comment1, "comment top row1 (index0)\n");
  137. String comment3 = sheet.getCellComment(new CellAddress(2, 0)).getString().getString();
  138. assertEquals(comment3, "comment top row3 (index2)\n");
  139. String comment4 = sheet.getCellComment(new CellAddress(3, 0)).getString().getString();
  140. assertEquals(comment4, "comment top row4 (index3)\n");
  141. //Workbook wbBack = _testDataProvider.writeOutAndReadBack(wb);
  142. // Shifting all but first line down to test comments shifting
  143. sheet.shiftRows(1, sheet.getLastRowNum(), 1, true, true);
  144. // Test that comments were shifted as expected
  145. assertEquals(4, sheet.getLastRowNum());
  146. assertNotNull(sheet.getCellComment(new CellAddress(0, 0)));
  147. assertNull(sheet.getCellComment(new CellAddress(1, 0)));
  148. assertNull(sheet.getCellComment(new CellAddress(2, 0)));
  149. assertNotNull(sheet.getCellComment(new CellAddress(3, 0)));
  150. assertNotNull(sheet.getCellComment(new CellAddress(4, 0)));
  151. String comment1_shifted = sheet.getCellComment(new CellAddress(0, 0)).getString().getString();
  152. assertEquals(comment1, comment1_shifted);
  153. String comment3_shifted = sheet.getCellComment(new CellAddress(3, 0)).getString().getString();
  154. assertEquals(comment3, comment3_shifted);
  155. String comment4_shifted = sheet.getCellComment(new CellAddress(4, 0)).getString().getString();
  156. assertEquals(comment4, comment4_shifted);
  157. // Write out and read back in again
  158. // Ensure that the changes were persisted
  159. try (Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1)) {
  160. sheet = wb2.getSheet("Sheet1");
  161. assertEquals(4, sheet.getLastRowNum());
  162. // Verify comments are in the position expected after the shift
  163. assertNotNull(sheet.getCellComment(new CellAddress(0, 0)));
  164. assertNull(sheet.getCellComment(new CellAddress(1, 0)));
  165. assertNull(sheet.getCellComment(new CellAddress(2, 0)));
  166. assertNotNull(sheet.getCellComment(new CellAddress(3, 0)));
  167. assertNotNull(sheet.getCellComment(new CellAddress(4, 0)));
  168. comment1_shifted = sheet.getCellComment(new CellAddress(0, 0)).getString().getString();
  169. assertEquals(comment1, comment1_shifted);
  170. comment3_shifted = sheet.getCellComment(new CellAddress(3, 0)).getString().getString();
  171. assertEquals(comment3, comment3_shifted);
  172. comment4_shifted = sheet.getCellComment(new CellAddress(4, 0)).getString().getString();
  173. assertEquals(comment4, comment4_shifted);
  174. // Shifting back up again, now two rows
  175. sheet.shiftRows(2, sheet.getLastRowNum(), -2, true, true);
  176. // TODO: it seems HSSFSheet does not correctly remove comments from rows that are overwritten
  177. // by shifting rows...
  178. if (!(wb2 instanceof HSSFWorkbook)) {
  179. assertEquals(2, sheet.getLastRowNum());
  180. // Verify comments are in the position expected
  181. assertNull(sheet.getCellComment(new CellAddress(0, 0)),
  182. "Had: " + (sheet.getCellComment(new CellAddress(0, 0)) == null ? "null" : sheet.getCellComment(new CellAddress(0, 0)).getString()));
  183. assertNotNull(sheet.getCellComment(new CellAddress(1, 0)));
  184. assertNotNull(sheet.getCellComment(new CellAddress(2, 0)));
  185. }
  186. comment1 = sheet.getCellComment(new CellAddress(1, 0)).getString().getString();
  187. assertEquals(comment1, "comment top row3 (index2)\n");
  188. String comment2 = sheet.getCellComment(new CellAddress(2, 0)).getString().getString();
  189. assertEquals(comment2, "comment top row4 (index3)\n");
  190. }
  191. }
  192. }
  193. @Test
  194. public final void testShiftWithNames() throws IOException {
  195. try (Workbook wb = _testDataProvider.createWorkbook()) {
  196. Sheet sheet1 = wb.createSheet("Sheet1");
  197. wb.createSheet("Sheet2");
  198. Row row = sheet1.createRow(0);
  199. row.createCell(0).setCellValue(1.1);
  200. row.createCell(1).setCellValue(2.2);
  201. Name name1 = wb.createName();
  202. name1.setNameName("name1");
  203. name1.setRefersToFormula("Sheet1!$A$1+Sheet1!$B$1");
  204. Name name2 = wb.createName();
  205. name2.setNameName("name2");
  206. name2.setRefersToFormula("Sheet1!$A$1");
  207. //refers to A1 but on Sheet2. Should stay unaffected.
  208. Name name3 = wb.createName();
  209. name3.setNameName("name3");
  210. name3.setRefersToFormula("Sheet2!$A$1");
  211. //The scope of this one is Sheet2. Should stay unaffected.
  212. Name name4 = wb.createName();
  213. name4.setNameName("name4");
  214. name4.setRefersToFormula("A1");
  215. name4.setSheetIndex(1);
  216. sheet1.shiftRows(0, 1, 2); //shift down the top row on Sheet1.
  217. name1 = wb.getName("name1");
  218. assertEquals("Sheet1!$A$3+Sheet1!$B$3", name1.getRefersToFormula());
  219. name2 = wb.getName("name2");
  220. assertEquals("Sheet1!$A$3", name2.getRefersToFormula());
  221. //name3 and name4 refer to Sheet2 and should not be affected
  222. name3 = wb.getName("name3");
  223. assertEquals("Sheet2!$A$1", name3.getRefersToFormula());
  224. name4 = wb.getName("name4");
  225. assertEquals("A1", name4.getRefersToFormula());
  226. }
  227. }
  228. @Test
  229. public final void testShiftWithMergedRegions() throws IOException {
  230. try (Workbook wb = _testDataProvider.createWorkbook()) {
  231. Sheet sheet = wb.createSheet();
  232. Row row = sheet.createRow(0);
  233. row.createCell(0).setCellValue(1.1);
  234. row.createCell(1).setCellValue(2.2);
  235. CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
  236. assertEquals("A1:C1", region.formatAsString());
  237. assertEquals(0, sheet.addMergedRegion(region));
  238. sheet.shiftRows(0, 1, 2);
  239. region = sheet.getMergedRegion(0);
  240. assertEquals("A3:C3", region.formatAsString());
  241. }
  242. }
  243. //@Disabled("bug 56454: Incorrectly handles merged regions that do not contain column 0")
  244. @Test
  245. public final void shiftWithMergedRegions_bug56454() throws IOException {
  246. try (Workbook wb = _testDataProvider.createWorkbook()) {
  247. Sheet sheet = wb.createSheet();
  248. // populate sheet cells
  249. for (int i = 0; i < 10; i++) {
  250. Row row = sheet.createRow(i);
  251. for (int j = 0; j < 10; j++) {
  252. Cell cell = row.createCell(j, CellType.STRING);
  253. cell.setCellValue(i + "x" + j);
  254. }
  255. }
  256. CellRangeAddress A4_B7 = CellRangeAddress.valueOf("A4:B7");
  257. CellRangeAddress C4_D7 = CellRangeAddress.valueOf("C4:D7");
  258. assertEquals(0, sheet.addMergedRegion(A4_B7));
  259. assertEquals(1, sheet.addMergedRegion(C4_D7));
  260. assumeTrue(sheet.getLastRowNum() > 8);
  261. // Insert a row in the middle of both merged regions.
  262. sheet.shiftRows(4, sheet.getLastRowNum(), 1);
  263. // all regions should still start at row 3, and elongate by 1 row
  264. List<CellRangeAddress> expectedMergedRegions = new ArrayList<>();
  265. CellRangeAddress A4_B8 = CellRangeAddress.valueOf("A4:B8"); //A4:B7 should be elongated by 1 row
  266. CellRangeAddress C4_D8 = CellRangeAddress.valueOf("C4:D8"); //C4:B7 should be elongated by 1 row
  267. expectedMergedRegions.add(A4_B8);
  268. expectedMergedRegions.add(C4_D8);
  269. // This test is written as expected-to-fail and should be rewritten
  270. // as expected-to-pass when the bug is fixed.
  271. // FIXME: remove try, catch, and testPassesNow, skipTest when test passes
  272. assertNotEquals(expectedMergedRegions, sheet.getMergedRegions());
  273. }
  274. }
  275. /**
  276. * See bug #34023
  277. */
  278. @Test
  279. public final void testShiftWithFormulas() throws IOException {
  280. try (Workbook wb = _testDataProvider.openSampleWorkbook("ForShifting." + _testDataProvider.getStandardFileNameExtension())) {
  281. Sheet sheet = wb.getSheet("Sheet1");
  282. assertEquals(20, sheet.getLastRowNum());
  283. confirmRow(sheet, 0, 1, 171, 1, "ROW(D1)", "100+B1", "COUNT(D1:E1)");
  284. confirmRow(sheet, 1, 2, 172, 1, "ROW(D2)", "100+B2", "COUNT(D2:E2)");
  285. confirmRow(sheet, 2, 3, 173, 1, "ROW(D3)", "100+B3", "COUNT(D3:E3)");
  286. confirmCell(sheet, 6, 1, 271, "200+B1");
  287. confirmCell(sheet, 7, 1, 272, "200+B2");
  288. confirmCell(sheet, 8, 1, 273, "200+B3");
  289. confirmCell(sheet, 14, 0, 0.0, "A12"); // the cell referred to by this formula will be replaced
  290. // -----------
  291. // Row index 1 -> 11 (row "2" -> row "12")
  292. sheet.shiftRows(1, 1, 10);
  293. // Now check what sheet looks like after move
  294. // no changes on row "1"
  295. confirmRow(sheet, 0, 1, 171, 1, "ROW(D1)", "100+B1", "COUNT(D1:E1)");
  296. // row "2" is now empty
  297. confirmEmptyRow(sheet, 1);
  298. // Row "2" moved to row "12", and the formula has been updated.
  299. // note however that the cached formula result (2) has not been updated. (POI differs from Excel here)
  300. confirmRow(sheet, 11, 2, 172, 1, "ROW(D12)", "100+B12", "COUNT(D12:E12)");
  301. // no changes on row "3"
  302. confirmRow(sheet, 2, 3, 173, 1, "ROW(D3)", "100+B3", "COUNT(D3:E3)");
  303. confirmCell(sheet, 14, 0, 0.0, "#REF!");
  304. // Formulas on rows that weren't shifted:
  305. confirmCell(sheet, 6, 1, 271, "200+B1");
  306. confirmCell(sheet, 7, 1, 272, "200+B12"); // this one moved
  307. confirmCell(sheet, 8, 1, 273, "200+B3");
  308. // check formulas on other sheets
  309. Sheet sheet2 = wb.getSheet("Sheet2");
  310. confirmCell(sheet2, 0, 0, 371, "300+Sheet1!B1");
  311. confirmCell(sheet2, 1, 0, 372, "300+Sheet1!B12");
  312. confirmCell(sheet2, 2, 0, 373, "300+Sheet1!B3");
  313. confirmCell(sheet2, 11, 0, 300, "300+Sheet1!#REF!");
  314. // Note - named ranges formulas have not been updated
  315. }
  316. }
  317. private static void confirmRow(Sheet sheet, int rowIx, double valA, double valB, double valC,
  318. String formulaA, String formulaB, String formulaC) {
  319. confirmCell(sheet, rowIx, 4, valA, formulaA);
  320. confirmCell(sheet, rowIx, 5, valB, formulaB);
  321. confirmCell(sheet, rowIx, 6, valC, formulaC);
  322. }
  323. private static void confirmCell(Sheet sheet, int rowIx, int colIx,
  324. double expectedValue, String expectedFormula) {
  325. Cell cell = sheet.getRow(rowIx).getCell(colIx);
  326. assertEquals(expectedValue, cell.getNumericCellValue(), 0.0);
  327. assertEquals(expectedFormula, cell.getCellFormula());
  328. }
  329. @Test
  330. public final void testShiftSharedFormulasBug54206() throws IOException {
  331. try (Workbook wb = _testDataProvider.openSampleWorkbook("54206." + _testDataProvider.getStandardFileNameExtension())) {
  332. Sheet sheet = wb.getSheetAt(0);
  333. assertEquals("SUMIF($B$19:$B$82,$B4,G$19:G$82)", sheet.getRow(3).getCell(6).getCellFormula());
  334. assertEquals("SUMIF($B$19:$B$82,$B4,H$19:H$82)", sheet.getRow(3).getCell(7).getCellFormula());
  335. assertEquals("SUMIF($B$19:$B$82,$B4,I$19:I$82)", sheet.getRow(3).getCell(8).getCellFormula());
  336. assertEquals("SUMIF($B$19:$B$82,$B15,G$19:G$82)", sheet.getRow(14).getCell(6).getCellFormula());
  337. assertEquals("SUMIF($B$19:$B$82,$B15,H$19:H$82)", sheet.getRow(14).getCell(7).getCellFormula());
  338. assertEquals("SUMIF($B$19:$B$82,$B15,I$19:I$82)", sheet.getRow(14).getCell(8).getCellFormula());
  339. // now the whole block G4L:15
  340. for (int i = 3; i <= 14; i++) {
  341. for (int j = 6; j <= 8; j++) {
  342. String col = CellReference.convertNumToColString(j);
  343. String expectedFormula = "SUMIF($B$19:$B$82,$B" + (i + 1) + "," + col + "$19:" + col + "$82)";
  344. assertEquals(expectedFormula, sheet.getRow(i).getCell(j).getCellFormula());
  345. }
  346. }
  347. assertEquals("SUM(G24:I24)", sheet.getRow(23).getCell(9).getCellFormula());
  348. assertEquals("SUM(G25:I25)", sheet.getRow(24).getCell(9).getCellFormula());
  349. assertEquals("SUM(G26:I26)", sheet.getRow(25).getCell(9).getCellFormula());
  350. sheet.shiftRows(24, sheet.getLastRowNum(), 4, true, false);
  351. assertEquals("SUMIF($B$19:$B$86,$B4,G$19:G$86)", sheet.getRow(3).getCell(6).getCellFormula());
  352. assertEquals("SUMIF($B$19:$B$86,$B4,H$19:H$86)", sheet.getRow(3).getCell(7).getCellFormula());
  353. assertEquals("SUMIF($B$19:$B$86,$B4,I$19:I$86)", sheet.getRow(3).getCell(8).getCellFormula());
  354. assertEquals("SUMIF($B$19:$B$86,$B15,G$19:G$86)", sheet.getRow(14).getCell(6).getCellFormula());
  355. assertEquals("SUMIF($B$19:$B$86,$B15,H$19:H$86)", sheet.getRow(14).getCell(7).getCellFormula());
  356. assertEquals("SUMIF($B$19:$B$86,$B15,I$19:I$86)", sheet.getRow(14).getCell(8).getCellFormula());
  357. // now the whole block G4L:15
  358. for (int i = 3; i <= 14; i++) {
  359. for (int j = 6; j <= 8; j++) {
  360. String col = CellReference.convertNumToColString(j);
  361. String expectedFormula = "SUMIF($B$19:$B$86,$B" + (i + 1) + "," + col + "$19:" + col + "$86)";
  362. assertEquals(expectedFormula, sheet.getRow(i).getCell(j).getCellFormula());
  363. }
  364. }
  365. assertEquals("SUM(G24:I24)", sheet.getRow(23).getCell(9).getCellFormula());
  366. // shifted rows
  367. assertTrue(sheet.getRow(24) == null || sheet.getRow(24).getCell(9) == null);
  368. assertTrue(sheet.getRow(25) == null || sheet.getRow(25).getCell(9) == null);
  369. assertTrue(sheet.getRow(26) == null || sheet.getRow(26).getCell(9) == null);
  370. assertTrue(sheet.getRow(27) == null || sheet.getRow(27).getCell(9) == null);
  371. assertEquals("SUM(G29:I29)", sheet.getRow(28).getCell(9).getCellFormula());
  372. assertEquals("SUM(G30:I30)", sheet.getRow(29).getCell(9).getCellFormula());
  373. }
  374. }
  375. @Test
  376. void testBug55280() throws IOException {
  377. try (Workbook w = _testDataProvider.createWorkbook()) {
  378. Sheet s = w.createSheet();
  379. for (int row = 0; row < 5000; ++row) {
  380. assertEquals(row, s.addMergedRegion(new CellRangeAddress(row, row, 0, 3)));
  381. }
  382. s.shiftRows(0, 4999, 1); // takes a long time...
  383. }
  384. }
  385. @Test
  386. void test47169() throws IOException {
  387. try (Workbook wb = _testDataProvider.createWorkbook()) {
  388. Sheet sheet = wb.createSheet();
  389. sheet.createRow(30);
  390. sheet.shiftRows(29, 29, 1, true, true);
  391. assertDoesNotThrow(() -> sheet.createRow(30));
  392. }
  393. }
  394. /**
  395. * Unified test for:
  396. * bug 46742: XSSFSheet.shiftRows should shift hyperlinks
  397. * bug 52903: HSSFSheet.shiftRows should shift hyperlinks
  398. */
  399. @Test
  400. void testBug46742_52903_shiftHyperlinks() throws IOException {
  401. try (Workbook wb = _testDataProvider.createWorkbook()) {
  402. Sheet sheet = wb.createSheet("test");
  403. Row row = sheet.createRow(0);
  404. // How to create hyperlinks
  405. // https://poi.apache.org/spreadsheet/quick-guide.html#Hyperlinks
  406. CreationHelper helper = wb.getCreationHelper();
  407. CellStyle hlinkStyle = wb.createCellStyle();
  408. Font hlinkFont = wb.createFont();
  409. hlinkFont.setUnderline(Font.U_SINGLE);
  410. hlinkFont.setColor(IndexedColors.BLUE.getIndex());
  411. hlinkStyle.setFont(hlinkFont);
  412. // 3D relative document link
  413. // CellAddress=A1, shifted to A4
  414. Cell cell = row.createCell(0);
  415. cell.setCellStyle(hlinkStyle);
  416. createHyperlink(helper, cell, HyperlinkType.DOCUMENT, "test!E1");
  417. // URL
  418. cell = row.createCell(1);
  419. // CellAddress=B1, shifted to B4
  420. cell.setCellStyle(hlinkStyle);
  421. createHyperlink(helper, cell, HyperlinkType.URL, "https://poi.apache.org/");
  422. // row0 will be shifted on top of row1, so this URL should be removed from the workbook
  423. Row overwrittenRow = sheet.createRow(3);
  424. cell = overwrittenRow.createCell(2);
  425. // CellAddress=C4, will be overwritten (deleted)
  426. cell.setCellStyle(hlinkStyle);
  427. createHyperlink(helper, cell, HyperlinkType.EMAIL, "mailto:poi@apache.org");
  428. // hyperlinks on this row are unaffected by the row shifting, so the hyperlinks should not move
  429. Row unaffectedRow = sheet.createRow(20);
  430. cell = unaffectedRow.createCell(3);
  431. // CellAddress=D21, will be unaffected
  432. cell.setCellStyle(hlinkStyle);
  433. createHyperlink(helper, cell, HyperlinkType.FILE, "54524.xlsx");
  434. cell = wb.createSheet("other").createRow(0).createCell(0);
  435. // CellAddress=Other!A1, will be unaffected
  436. cell.setCellStyle(hlinkStyle);
  437. createHyperlink(helper, cell, HyperlinkType.URL, "http://apache.org/");
  438. int startRow = 0;
  439. int endRow = 0;
  440. int n = 3;
  441. sheet.shiftRows(startRow, endRow, n);
  442. try (Workbook read = _testDataProvider.writeOutAndReadBack(wb)) {
  443. Sheet sh = read.getSheet("test");
  444. Row shiftedRow = sh.getRow(3);
  445. // document link anchored on a shifted cell should be moved
  446. // Note that hyperlinks do not track what they point to, so this hyperlink should still refer to test!E1
  447. verifyHyperlink(shiftedRow.getCell(0), HyperlinkType.DOCUMENT, "test!E1");
  448. // URL, EMAIL, and FILE links anchored on a shifted cell should be moved
  449. verifyHyperlink(shiftedRow.getCell(1), HyperlinkType.URL, "https://poi.apache.org/");
  450. // Make sure hyperlinks were moved and not copied
  451. assertNull(sh.getHyperlink(0, 0), "Document hyperlink should be moved, not copied");
  452. assertNull(sh.getHyperlink(0, 1), "URL hyperlink should be moved, not copied");
  453. // Make sure hyperlink in overwritten row is deleted
  454. assertEquals(3, sh.getHyperlinkList().size());
  455. CellAddress unexpectedLinkAddress = new CellAddress("C4");
  456. for (Hyperlink link : sh.getHyperlinkList()) {
  457. final CellAddress linkAddress = new CellAddress(link.getFirstRow(), link.getFirstColumn());
  458. assertNotEquals(linkAddress, unexpectedLinkAddress,
  459. "Row 4, including the hyperlink at C4, should have " +
  460. "been deleted when Row 1 was shifted on top of it.");
  461. }
  462. // Make sure unaffected rows are not shifted
  463. Cell unaffectedCell = sh.getRow(20).getCell(3);
  464. assertTrue(cellHasHyperlink(unaffectedCell));
  465. verifyHyperlink(unaffectedCell, HyperlinkType.FILE, "54524.xlsx");
  466. // Make sure cells on other sheets are not affected
  467. unaffectedCell = read.getSheet("other").getRow(0).getCell(0);
  468. assertTrue(cellHasHyperlink(unaffectedCell));
  469. verifyHyperlink(unaffectedCell, HyperlinkType.URL, "http://apache.org/");
  470. }
  471. }
  472. }
  473. //@Disabled("bug 56454: Incorrectly handles merged regions that do not contain column 0")
  474. @Test
  475. void shiftRowsWithMergedRegionsThatDoNotContainColumnZero() throws IOException {
  476. try (Workbook wb = _testDataProvider.createWorkbook()) {
  477. Sheet sheet = wb.createSheet("test");
  478. // populate sheet cells
  479. for (int i = 0; i < 10; i++) {
  480. Row row = sheet.createRow(i);
  481. for (int j = 0; j < 12; j++) {
  482. Cell cell = row.createCell(j);
  483. cell.setCellValue(i + "x" + j);
  484. }
  485. }
  486. CellRangeAddress A4_B7 = new CellRangeAddress(3, 6, 0, 1);
  487. CellRangeAddress C5_D7 = new CellRangeAddress(4, 6, 2, 3);
  488. assertEquals(0, sheet.addMergedRegion(A4_B7));
  489. assertEquals(1, sheet.addMergedRegion(C5_D7));
  490. // A4:B7 will elongate vertically
  491. // C5:D7 will be shifted down with same size
  492. sheet.shiftRows(4, sheet.getLastRowNum(), 1);
  493. // This test is written as expected-to-fail and should be rewritten
  494. // as expected-to-pass when the bug is fixed.
  495. // FIXME: remove try, catch, and testPassesNow, skipTest when test passes
  496. assertEquals(2, sheet.getNumMergedRegions());
  497. assertNotEquals(CellRangeAddress.valueOf("A4:B8"), sheet.getMergedRegion(0));
  498. assertNotEquals(CellRangeAddress.valueOf("C5:D8"), sheet.getMergedRegion(1));
  499. }
  500. }
  501. @Test
  502. void shiftMergedRowsToMergedRowsUp() throws IOException {
  503. try (Workbook wb = _testDataProvider.createWorkbook()) {
  504. Sheet sheet = wb.createSheet("test");
  505. populateSheetCells(sheet, 2);
  506. CellRangeAddress A1_E1 = new CellRangeAddress(0, 0, 0, 4);
  507. CellRangeAddress A2_C2 = new CellRangeAddress(1, 1, 0, 2);
  508. assertEquals(0, sheet.addMergedRegion(A1_E1));
  509. assertEquals(1, sheet.addMergedRegion(A2_C2));
  510. // A1:E1 should be removed
  511. // A2:C2 will be A1:C1
  512. sheet.shiftRows(1, sheet.getLastRowNum(), -1);
  513. assertEquals(1, sheet.getNumMergedRegions());
  514. assertEquals(CellRangeAddress.valueOf("A1:C1"), sheet.getMergedRegion(0));
  515. }
  516. }
  517. @Test
  518. void shiftMergedRowsToMergedRowsOverlappingMergedRegion() throws IOException {
  519. try (Workbook wb = _testDataProvider.createWorkbook()) {
  520. Sheet sheet = wb.createSheet("test");
  521. populateSheetCells(sheet, 10);
  522. CellRangeAddress A1_E1 = new CellRangeAddress(0, 0, 0, 4);
  523. CellRangeAddress A2_C2 = new CellRangeAddress(1, 7, 0, 2);
  524. assertEquals(0, sheet.addMergedRegion(A1_E1));
  525. assertEquals(1, sheet.addMergedRegion(A2_C2));
  526. // A1:E1 should move to A5:E5
  527. // A2:C2 should be removed
  528. sheet.shiftRows(0, 0, 4);
  529. assertEquals(1, sheet.getNumMergedRegions());
  530. assertEquals(CellRangeAddress.valueOf("A5:E5"), sheet.getMergedRegion(0));
  531. }
  532. }
  533. @Test
  534. void bug60384ShiftMergedRegion() throws IOException {
  535. try (Workbook wb = _testDataProvider.createWorkbook()) {
  536. Sheet sheet = wb.createSheet("test");
  537. populateSheetCells(sheet, 9);
  538. CellRangeAddress A8_E8 = new CellRangeAddress(7, 7, 0, 4);
  539. CellRangeAddress A9_C9 = new CellRangeAddress(8, 8, 0, 2);
  540. assertEquals(0, sheet.addMergedRegion(A8_E8));
  541. assertEquals(1, sheet.addMergedRegion(A9_C9));
  542. // A1:E1 should be removed
  543. // A2:C2 will be A1:C1
  544. sheet.shiftRows(3, sheet.getLastRowNum(), 1);
  545. assertEquals(2, sheet.getNumMergedRegions());
  546. assertEquals(CellRangeAddress.valueOf("A9:E9"), sheet.getMergedRegion(0));
  547. assertEquals(CellRangeAddress.valueOf("A10:C10"), sheet.getMergedRegion(1));
  548. }
  549. }
  550. private void populateSheetCells(Sheet sheet, int rowCount) {
  551. // populate sheet cells
  552. for (int i = 0; i < rowCount; i++) {
  553. Row row = sheet.createRow(i);
  554. for (int j = 0; j < 5; j++) {
  555. Cell cell = row.createCell(j);
  556. cell.setCellValue(i + "x" + j);
  557. }
  558. }
  559. }
  560. @Test
  561. void shiftMergedRowsToMergedRowsDown() throws IOException {
  562. try (Workbook wb = _testDataProvider.createWorkbook()) {
  563. Sheet sheet = wb.createSheet("test");
  564. // populate sheet cells
  565. populateSheetCells(sheet, 2);
  566. CellRangeAddress A1_E1 = new CellRangeAddress(0, 0, 0, 4);
  567. CellRangeAddress A2_C2 = new CellRangeAddress(1, 1, 0, 2);
  568. assertEquals(0, sheet.addMergedRegion(A1_E1));
  569. assertEquals(1, sheet.addMergedRegion(A2_C2));
  570. // A1:E1 should be moved to A2:E2
  571. // A2:C2 will be removed
  572. sheet.shiftRows(0, 0, 1);
  573. assertEquals(1, sheet.getNumMergedRegions());
  574. assertEquals(CellRangeAddress.valueOf("A2:E2"), sheet.getMergedRegion(0));
  575. }
  576. }
  577. @Test
  578. void test61840_shifting_rows_up_does_not_produce_REF_errors() throws IOException {
  579. try (Workbook wb = _testDataProvider.createWorkbook()) {
  580. Sheet sheet = wb.createSheet();
  581. Cell cell = sheet.createRow(4).createCell(0);
  582. cell.setCellFormula("(B5-C5)/B5");
  583. sheet.shiftRows(4, 4, -1);
  584. // Cell objects created before a row shift are still valid.
  585. // The row number of those cell references will be shifted if
  586. // the cell is within the shift range.
  587. assertEquals("(B4-C4)/B4", cell.getCellFormula());
  588. // New cell references are also valid.
  589. Cell shiftedCell = sheet.getRow(3).getCell(0);
  590. assertNotNull(shiftedCell);
  591. assertEquals("(B4-C4)/B4", shiftedCell.getCellFormula());
  592. }
  593. }
  594. @Test
  595. void checkMergedRegions56454() throws IOException {
  596. try (Workbook wb = _testDataProvider.createWorkbook()) {
  597. Sheet sheet = wb.createSheet();
  598. // populate sheet cells
  599. for (int i = 0; i < 10; i++) {
  600. Row row = sheet.createRow(i);
  601. for (int j = 0; j < 10; j++) {
  602. Cell cell = row.createCell(j, CellType.STRING);
  603. cell.setCellValue(i + "x" + j);
  604. }
  605. }
  606. CellRangeAddress region1 = new CellRangeAddress(3, 6, 0, 1);
  607. CellRangeAddress region2 = new CellRangeAddress(3, 6, 2, 3);
  608. sheet.addMergedRegion(region1);
  609. sheet.addMergedRegion(region2);
  610. sheet.shiftRows(4, sheet.getLastRowNum(), 1);
  611. // check, if all regions still start at row 3
  612. for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
  613. CellRangeAddress cr = sheet.getMergedRegion(i);
  614. assertEquals(cr.getFirstRow(), 3);
  615. }
  616. }
  617. }
  618. private void createHyperlink(CreationHelper helper, Cell cell, HyperlinkType linkType, String ref) {
  619. cell.setCellValue(ref);
  620. Hyperlink link = helper.createHyperlink(linkType);
  621. link.setAddress(ref);
  622. cell.setHyperlink(link);
  623. }
  624. private void verifyHyperlink(Cell cell, HyperlinkType linkType, String ref) {
  625. assertTrue(cellHasHyperlink(cell));
  626. Hyperlink link = cell.getHyperlink();
  627. assertEquals(linkType, link.getType());
  628. assertEquals(ref, link.getAddress());
  629. }
  630. private boolean cellHasHyperlink(Cell cell) {
  631. return (cell != null) && (cell.getHyperlink() != null);
  632. }
  633. }