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.

BaseTestSheetShiftRows.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 junit.framework.TestCase;
  17. import org.apache.poi.ss.ITestDataProvider;
  18. import org.apache.poi.ss.util.CellRangeAddress;
  19. import org.apache.poi.ss.util.CellReference;
  20. /**
  21. * Tests row shifting capabilities.
  22. *
  23. * @author Shawn Laubach (slaubach at apache dot com)
  24. * @author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
  25. */
  26. public abstract class BaseTestSheetShiftRows extends TestCase {
  27. private final ITestDataProvider _testDataProvider;
  28. protected BaseTestSheetShiftRows(ITestDataProvider testDataProvider) {
  29. _testDataProvider = testDataProvider;
  30. }
  31. /**
  32. * Tests the shiftRows function. Does three different shifts.
  33. * After each shift, writes the workbook to file and reads back to
  34. * check. This ensures that if some changes code that breaks
  35. * writing or what not, they realize it.
  36. */
  37. public final void testShiftRows(){
  38. // Read initial file in
  39. String sampleName = "SimpleMultiCell." + _testDataProvider.getStandardFileNameExtension();
  40. Workbook wb = _testDataProvider.openSampleWorkbook(sampleName);
  41. Sheet s = wb.getSheetAt( 0 );
  42. // Shift the second row down 1 and write to temp file
  43. s.shiftRows( 1, 1, 1 );
  44. wb = _testDataProvider.writeOutAndReadBack(wb);
  45. // Read from temp file and check the number of cells in each
  46. // row (in original file each row was unique)
  47. s = wb.getSheetAt( 0 );
  48. assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 1);
  49. confirmEmptyRow(s, 1);
  50. assertEquals(s.getRow(2).getPhysicalNumberOfCells(), 2);
  51. assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 4);
  52. assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5);
  53. // Shift rows 1-3 down 3 in the current one. This tests when
  54. // 1 row is blank. Write to a another temp file
  55. s.shiftRows( 0, 2, 3 );
  56. wb = _testDataProvider.writeOutAndReadBack(wb);
  57. // Read and ensure things are where they should be
  58. s = wb.getSheetAt(0);
  59. confirmEmptyRow(s, 0);
  60. confirmEmptyRow(s, 1);
  61. confirmEmptyRow(s, 2);
  62. assertEquals(s.getRow(3).getPhysicalNumberOfCells(), 1);
  63. confirmEmptyRow(s, 4);
  64. assertEquals(s.getRow(5).getPhysicalNumberOfCells(), 2);
  65. // Read the first file again
  66. wb = _testDataProvider.openSampleWorkbook(sampleName);
  67. s = wb.getSheetAt( 0 );
  68. // Shift rows 3 and 4 up and write to temp file
  69. s.shiftRows( 2, 3, -2 );
  70. wb = _testDataProvider.writeOutAndReadBack(wb);
  71. s = wb.getSheetAt( 0 );
  72. assertEquals(s.getRow(0).getPhysicalNumberOfCells(), 3);
  73. assertEquals(s.getRow(1).getPhysicalNumberOfCells(), 4);
  74. confirmEmptyRow(s, 2);
  75. confirmEmptyRow(s, 3);
  76. assertEquals(s.getRow(4).getPhysicalNumberOfCells(), 5);
  77. }
  78. private static void confirmEmptyRow(Sheet s, int rowIx) {
  79. Row row = s.getRow(rowIx);
  80. assertTrue(row == null || row.getPhysicalNumberOfCells() == 0);
  81. }
  82. /**
  83. * Tests when rows are null.
  84. */
  85. public final void testShiftRow() {
  86. Workbook b = _testDataProvider.createWorkbook();
  87. Sheet s = b.createSheet();
  88. s.createRow(0).createCell(0).setCellValue("TEST1");
  89. s.createRow(3).createCell(0).setCellValue("TEST2");
  90. s.shiftRows(0,4,1);
  91. }
  92. /**
  93. * Tests when shifting the first row.
  94. */
  95. public final void testActiveCell() {
  96. Workbook b = _testDataProvider.createWorkbook();
  97. Sheet s = b.createSheet();
  98. s.createRow(0).createCell(0).setCellValue("TEST1");
  99. s.createRow(3).createCell(0).setCellValue("TEST2");
  100. s.shiftRows(0,4,1);
  101. }
  102. /**
  103. * When shifting rows, the page breaks should go with it
  104. */
  105. public void testShiftRowBreaks() { // TODO - enable XSSF test
  106. Workbook b = _testDataProvider.createWorkbook();
  107. Sheet s = b.createSheet();
  108. Row row = s.createRow(4);
  109. row.createCell(0).setCellValue("test");
  110. s.setRowBreak(4);
  111. s.shiftRows(4, 4, 2);
  112. assertTrue("Row number 6 should have a pagebreak", s.isRowBroken(6));
  113. }
  114. public void testShiftWithComments() { // TODO - enable XSSF test
  115. Workbook wb = _testDataProvider.openSampleWorkbook("comments." + _testDataProvider.getStandardFileNameExtension());
  116. Sheet sheet = wb.getSheet("Sheet1");
  117. assertEquals(3, sheet.getLastRowNum());
  118. // Verify comments are in the position expected
  119. assertNotNull(sheet.getCellComment(0,0));
  120. assertNull(sheet.getCellComment(1,0));
  121. assertNotNull(sheet.getCellComment(2,0));
  122. assertNotNull(sheet.getCellComment(3,0));
  123. String comment1 = sheet.getCellComment(0,0).getString().getString();
  124. assertEquals(comment1,"comment top row1 (index0)\n");
  125. String comment3 = sheet.getCellComment(2,0).getString().getString();
  126. assertEquals(comment3,"comment top row3 (index2)\n");
  127. String comment4 = sheet.getCellComment(3,0).getString().getString();
  128. assertEquals(comment4,"comment top row4 (index3)\n");
  129. // Shifting all but first line down to test comments shifting
  130. sheet.shiftRows(1, sheet.getLastRowNum(), 1, true, true);
  131. // Test that comments were shifted as expected
  132. assertEquals(4, sheet.getLastRowNum());
  133. assertNotNull(sheet.getCellComment(0,0));
  134. assertNull(sheet.getCellComment(1,0));
  135. assertNull(sheet.getCellComment(2,0));
  136. assertNotNull(sheet.getCellComment(3,0));
  137. assertNotNull(sheet.getCellComment(4,0));
  138. String comment1_shifted = sheet.getCellComment(0,0).getString().getString();
  139. assertEquals(comment1,comment1_shifted);
  140. String comment3_shifted = sheet.getCellComment(3,0).getString().getString();
  141. assertEquals(comment3,comment3_shifted);
  142. String comment4_shifted = sheet.getCellComment(4,0).getString().getString();
  143. assertEquals(comment4,comment4_shifted);
  144. // Write out and read back in again
  145. // Ensure that the changes were persisted
  146. wb = _testDataProvider.writeOutAndReadBack(wb);
  147. sheet = wb.getSheet("Sheet1");
  148. assertEquals(4, sheet.getLastRowNum());
  149. // Verify comments are in the position expected after the shift
  150. assertNotNull(sheet.getCellComment(0,0));
  151. assertNull(sheet.getCellComment(1,0));
  152. assertNull(sheet.getCellComment(2,0));
  153. assertNotNull(sheet.getCellComment(3,0));
  154. assertNotNull(sheet.getCellComment(4,0));
  155. comment1_shifted = sheet.getCellComment(0,0).getString().getString();
  156. assertEquals(comment1,comment1_shifted);
  157. comment3_shifted = sheet.getCellComment(3,0).getString().getString();
  158. assertEquals(comment3,comment3_shifted);
  159. comment4_shifted = sheet.getCellComment(4,0).getString().getString();
  160. assertEquals(comment4,comment4_shifted);
  161. }
  162. public final void testShiftWithNames() {
  163. Workbook wb = _testDataProvider.createWorkbook();
  164. Sheet sheet1 = wb.createSheet("Sheet1");
  165. wb.createSheet("Sheet2");
  166. Row row = sheet1.createRow(0);
  167. row.createCell(0).setCellValue(1.1);
  168. row.createCell(1).setCellValue(2.2);
  169. Name name1 = wb.createName();
  170. name1.setNameName("name1");
  171. name1.setRefersToFormula("Sheet1!$A$1+Sheet1!$B$1");
  172. Name name2 = wb.createName();
  173. name2.setNameName("name2");
  174. name2.setRefersToFormula("Sheet1!$A$1");
  175. //refers to A1 but on Sheet2. Should stay unaffected.
  176. Name name3 = wb.createName();
  177. name3.setNameName("name3");
  178. name3.setRefersToFormula("Sheet2!$A$1");
  179. //The scope of this one is Sheet2. Should stay unaffected.
  180. Name name4 = wb.createName();
  181. name4.setNameName("name4");
  182. name4.setRefersToFormula("A1");
  183. name4.setSheetIndex(1);
  184. sheet1.shiftRows(0, 1, 2); //shift down the top row on Sheet1.
  185. name1 = wb.getNameAt(0);
  186. assertEquals("Sheet1!$A$3+Sheet1!$B$3", name1.getRefersToFormula());
  187. name2 = wb.getNameAt(1);
  188. assertEquals("Sheet1!$A$3", name2.getRefersToFormula());
  189. //name3 and name4 refer to Sheet2 and should not be affected
  190. name3 = wb.getNameAt(2);
  191. assertEquals("Sheet2!$A$1", name3.getRefersToFormula());
  192. name4 = wb.getNameAt(3);
  193. assertEquals("A1", name4.getRefersToFormula());
  194. }
  195. public final void testShiftWithMergedRegions() {
  196. Workbook wb = _testDataProvider.createWorkbook();
  197. Sheet sheet = wb.createSheet();
  198. Row row = sheet.createRow(0);
  199. row.createCell(0).setCellValue(1.1);
  200. row.createCell(1).setCellValue(2.2);
  201. CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
  202. assertEquals("A1:C1", region.formatAsString());
  203. sheet.addMergedRegion(region);
  204. sheet.shiftRows(0, 1, 2);
  205. region = sheet.getMergedRegion(0);
  206. assertEquals("A3:C3", region.formatAsString());
  207. }
  208. /**
  209. * See bug #34023
  210. */
  211. public final void testShiftWithFormulas() {
  212. Workbook wb = _testDataProvider.openSampleWorkbook("ForShifting." + _testDataProvider.getStandardFileNameExtension());
  213. Sheet sheet = wb.getSheet("Sheet1");
  214. assertEquals(20, sheet.getLastRowNum());
  215. confirmRow(sheet, 0, 1, 171, 1, "ROW(D1)", "100+B1", "COUNT(D1:E1)");
  216. confirmRow(sheet, 1, 2, 172, 1, "ROW(D2)", "100+B2", "COUNT(D2:E2)");
  217. confirmRow(sheet, 2, 3, 173, 1, "ROW(D3)", "100+B3", "COUNT(D3:E3)");
  218. confirmCell(sheet, 6, 1, 271, "200+B1");
  219. confirmCell(sheet, 7, 1, 272, "200+B2");
  220. confirmCell(sheet, 8, 1, 273, "200+B3");
  221. confirmCell(sheet, 14, 0, 0.0, "A12"); // the cell referred to by this formula will be replaced
  222. // -----------
  223. // Row index 1 -> 11 (row "2" -> row "12")
  224. sheet.shiftRows(1, 1, 10);
  225. // Now check what sheet looks like after move
  226. // no changes on row "1"
  227. confirmRow(sheet, 0, 1, 171, 1, "ROW(D1)", "100+B1", "COUNT(D1:E1)");
  228. // row "2" is now empty
  229. confirmEmptyRow(sheet, 1);
  230. // Row "2" moved to row "12", and the formula has been updated.
  231. // note however that the cached formula result (2) has not been updated. (POI differs from Excel here)
  232. confirmRow(sheet, 11, 2, 172, 1, "ROW(D12)", "100+B12", "COUNT(D12:E12)");
  233. // no changes on row "3"
  234. confirmRow(sheet, 2, 3, 173, 1, "ROW(D3)", "100+B3", "COUNT(D3:E3)");
  235. confirmCell(sheet, 14, 0, 0.0, "#REF!");
  236. // Formulas on rows that weren't shifted:
  237. confirmCell(sheet, 6, 1, 271, "200+B1");
  238. confirmCell(sheet, 7, 1, 272, "200+B12"); // this one moved
  239. confirmCell(sheet, 8, 1, 273, "200+B3");
  240. // check formulas on other sheets
  241. Sheet sheet2 = wb.getSheet("Sheet2");
  242. confirmCell(sheet2, 0, 0, 371, "300+Sheet1!B1");
  243. confirmCell(sheet2, 1, 0, 372, "300+Sheet1!B12");
  244. confirmCell(sheet2, 2, 0, 373, "300+Sheet1!B3");
  245. confirmCell(sheet2, 11, 0, 300, "300+Sheet1!#REF!");
  246. // Note - named ranges formulas have not been updated
  247. }
  248. private static void confirmRow(Sheet sheet, int rowIx, double valA, double valB, double valC,
  249. String formulaA, String formulaB, String formulaC) {
  250. confirmCell(sheet, rowIx, 4, valA, formulaA);
  251. confirmCell(sheet, rowIx, 5, valB, formulaB);
  252. confirmCell(sheet, rowIx, 6, valC, formulaC);
  253. }
  254. private static void confirmCell(Sheet sheet, int rowIx, int colIx,
  255. double expectedValue, String expectedFormula) {
  256. Cell cell = sheet.getRow(rowIx).getCell(colIx);
  257. assertEquals(expectedValue, cell.getNumericCellValue(), 0.0);
  258. assertEquals(expectedFormula, cell.getCellFormula());
  259. }
  260. public final void testShiftSharedFormulasBug54206() {
  261. Workbook wb = _testDataProvider.openSampleWorkbook("54206." + _testDataProvider.getStandardFileNameExtension());
  262. Sheet sheet = wb.getSheetAt(0);
  263. assertEquals("SUMIF($B$19:$B$82,$B4,G$19:G$82)", sheet.getRow(3).getCell(6).getCellFormula());
  264. assertEquals("SUMIF($B$19:$B$82,$B4,H$19:H$82)", sheet.getRow(3).getCell(7).getCellFormula());
  265. assertEquals("SUMIF($B$19:$B$82,$B4,I$19:I$82)", sheet.getRow(3).getCell(8).getCellFormula());
  266. assertEquals("SUMIF($B$19:$B$82,$B15,G$19:G$82)", sheet.getRow(14).getCell(6).getCellFormula());
  267. assertEquals("SUMIF($B$19:$B$82,$B15,H$19:H$82)", sheet.getRow(14).getCell(7).getCellFormula());
  268. assertEquals("SUMIF($B$19:$B$82,$B15,I$19:I$82)", sheet.getRow(14).getCell(8).getCellFormula());
  269. // now the whole block G4L:15
  270. for(int i = 3; i <= 14; i++){
  271. for(int j = 6; j <= 8; j++){
  272. String col = CellReference.convertNumToColString(j);
  273. String expectedFormula = "SUMIF($B$19:$B$82,$B"+(i+1)+","+col+"$19:"+col+"$82)";
  274. assertEquals(expectedFormula, sheet.getRow(i).getCell(j).getCellFormula());
  275. }
  276. }
  277. assertEquals("SUM(G24:I24)", sheet.getRow(23).getCell(9).getCellFormula());
  278. assertEquals("SUM(G25:I25)", sheet.getRow(24).getCell(9).getCellFormula());
  279. assertEquals("SUM(G26:I26)", sheet.getRow(25).getCell(9).getCellFormula());
  280. sheet.shiftRows(24, sheet.getLastRowNum(), 4, true, false);
  281. assertEquals("SUMIF($B$19:$B$86,$B4,G$19:G$86)", sheet.getRow(3).getCell(6).getCellFormula());
  282. assertEquals("SUMIF($B$19:$B$86,$B4,H$19:H$86)", sheet.getRow(3).getCell(7).getCellFormula());
  283. assertEquals("SUMIF($B$19:$B$86,$B4,I$19:I$86)", sheet.getRow(3).getCell(8).getCellFormula());
  284. assertEquals("SUMIF($B$19:$B$86,$B15,G$19:G$86)", sheet.getRow(14).getCell(6).getCellFormula());
  285. assertEquals("SUMIF($B$19:$B$86,$B15,H$19:H$86)", sheet.getRow(14).getCell(7).getCellFormula());
  286. assertEquals("SUMIF($B$19:$B$86,$B15,I$19:I$86)", sheet.getRow(14).getCell(8).getCellFormula());
  287. // now the whole block G4L:15
  288. for(int i = 3; i <= 14; i++){
  289. for(int j = 6; j <= 8; j++){
  290. String col = CellReference.convertNumToColString(j);
  291. String expectedFormula = "SUMIF($B$19:$B$86,$B"+(i+1)+","+col+"$19:"+col+"$86)";
  292. assertEquals(expectedFormula, sheet.getRow(i).getCell(j).getCellFormula());
  293. }
  294. }
  295. assertEquals("SUM(G24:I24)", sheet.getRow(23).getCell(9).getCellFormula());
  296. // shifted rows
  297. assertTrue( sheet.getRow(24) == null || sheet.getRow(24).getCell(9) == null);
  298. assertTrue( sheet.getRow(25) == null || sheet.getRow(25).getCell(9) == null);
  299. assertTrue( sheet.getRow(26) == null || sheet.getRow(26).getCell(9) == null);
  300. assertTrue( sheet.getRow(27) == null || sheet.getRow(27).getCell(9) == null);
  301. assertEquals("SUM(G29:I29)", sheet.getRow(28).getCell(9).getCellFormula());
  302. assertEquals("SUM(G30:I30)", sheet.getRow(29).getCell(9).getCellFormula());
  303. }
  304. }