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.

TestRangeCopier.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.ss.usermodel;
  20. import static org.junit.Assert.assertEquals;
  21. import org.junit.Ignore;
  22. import org.junit.Test;
  23. import org.apache.poi.ss.ITestDataProvider;
  24. import org.apache.poi.ss.util.CellRangeAddress;
  25. import org.apache.poi.ss.util.CellReference;
  26. @Ignore
  27. public abstract class TestRangeCopier {
  28. protected Sheet sheet1;
  29. protected Sheet sheet2;
  30. protected Workbook workbook;
  31. protected RangeCopier rangeCopier;
  32. protected RangeCopier transSheetRangeCopier;
  33. protected ITestDataProvider testDataProvider;
  34. protected void initSheets() {
  35. sheet1 = workbook.getSheet("sheet1");
  36. sheet2 = workbook.getSheet("sheet2");
  37. }
  38. @Test
  39. public void copySheetRangeWithoutFormulas() {
  40. CellRangeAddress rangeToCopy = CellRangeAddress.valueOf("B1:C2"); //2x2
  41. CellRangeAddress destRange = CellRangeAddress.valueOf("C2:D3"); //2x2
  42. rangeCopier.copyRange(rangeToCopy, destRange);
  43. assertEquals("1.1", sheet1.getRow(2).getCell(2).toString());
  44. assertEquals("2.1", sheet1.getRow(2).getCell(3).toString());
  45. }
  46. @Test
  47. public void tileTheRangeAway() {
  48. CellRangeAddress tileRange = CellRangeAddress.valueOf("C4:D5");
  49. CellRangeAddress destRange = CellRangeAddress.valueOf("F4:K5");
  50. rangeCopier.copyRange(tileRange, destRange);
  51. assertEquals("1.3", getCellContent(sheet1, "H4"));
  52. assertEquals("1.3", getCellContent(sheet1, "J4"));
  53. assertEquals("$C1+G$2", getCellContent(sheet1, "G5"));
  54. assertEquals("SUM(G3:I3)", getCellContent(sheet1, "H5"));
  55. assertEquals("$C1+I$2", getCellContent(sheet1, "I5"));
  56. assertEquals("", getCellContent(sheet1, "L5")); //out of borders
  57. assertEquals("", getCellContent(sheet1, "G7")); //out of borders
  58. }
  59. @Test
  60. public void tileTheRangeOver() {
  61. CellRangeAddress tileRange = CellRangeAddress.valueOf("C4:D5");
  62. CellRangeAddress destRange = CellRangeAddress.valueOf("A4:C5");
  63. rangeCopier.copyRange(tileRange, destRange);
  64. assertEquals("1.3", getCellContent(sheet1, "A4"));
  65. assertEquals("$C1+B$2", getCellContent(sheet1, "B5"));
  66. assertEquals("SUM(B3:D3)", getCellContent(sheet1, "C5"));
  67. }
  68. @Test
  69. public void copyRangeToOtherSheet() {
  70. Sheet destSheet = sheet2;
  71. CellRangeAddress tileRange = CellRangeAddress.valueOf("C4:D5"); // on sheet1
  72. CellRangeAddress destRange = CellRangeAddress.valueOf("F4:J6"); // on sheet2
  73. transSheetRangeCopier.copyRange(tileRange, destRange);
  74. assertEquals("1.3", getCellContent(destSheet, "H4"));
  75. assertEquals("1.3", getCellContent(destSheet, "J4"));
  76. assertEquals("$C1+G$2", getCellContent(destSheet, "G5"));
  77. assertEquals("SUM(G3:I3)", getCellContent(destSheet, "H5"));
  78. assertEquals("$C1+I$2", getCellContent(destSheet, "I5"));
  79. }
  80. protected static String getCellContent(Sheet sheet, String coordinates) {
  81. try {
  82. CellReference p = new CellReference(coordinates);
  83. return sheet.getRow(p.getRow()).getCell(p.getCol()).toString();
  84. }
  85. catch (NullPointerException e) { // row or cell does not exist
  86. return "";
  87. }
  88. }
  89. }