*
* @param region to merge
* @param validate whether to validate merged region
- * @return index of this region
+ * @return 0-based index of this region
* @throws IllegalArgumentException if region contains fewer than 2 cells (this check is inexpensive and is performed regardless of <tt>validate</tt>)
* @throws IllegalStateException if region intersects with a multi-cell array formula
* @throws IllegalStateException if region intersects with an existing region on this sheet
validateArrayFormulas(region);
// Throw IllegalStateException if the argument CellRangeAddress intersects with
- // a merged region already in this sheet
+ // a merged region already in this sheet
validateMergedRegions(region);
}
CTMergeCells ctMergeCells = worksheet.isSetMergeCells() ? worksheet.getMergeCells() : worksheet.addNewMergeCells();
CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell();
ctMergeCell.setRef(region.formatAsString());
- return ctMergeCells.sizeOfMergeCellArray();
+ return ctMergeCells.sizeOfMergeCellArray()-1;
}
/**
* Tests the auto-sizing behaviour of {@link SXSSFSheet} when not all
* rows fit into the memory window size etc.
*
- * @see Bug #57450 which reported the original mis-behaviour
+ * see Bug #57450 which reported the original misbehaviour
*/
public class TestAutoSizeColumnTracker {
}
tracker.updateColumnWidths(row1);
tracker.updateColumnWidths(row2);
- sheet.addMergedRegion(CellRangeAddress.valueOf("D1:E1"));
+ assertEquals(0, sheet.addMergedRegion(CellRangeAddress.valueOf("D1:E1")));
assumeRequiredFontsAreInstalled(workbook, row1.getCell(columns.iterator().next()));
// Excel 2013 and LibreOffice 4.2.8.2 both treat columns with merged regions as blank
- /** A B C D E
+ /* A B C D E
* 1 LONG LONG LONGMERGE
* 2 SHORT SHORT SHORT
*/
* Tests the auto-sizing behaviour of {@link SXSSFSheet} when not all
* rows fit into the memory window size etc.
*
- * @see Bug #57450 which reported the original mis-behaviour
+ * see Bug #57450 which reported the original misbehaviour
*/
@RunWith(Parameterized.class)
public class TestSXSSFSheetAutoSizeColumn {
private SXSSFSheet sheet;
private SXSSFWorkbook workbook;
- @Parameter(0)
+ @Parameter
public boolean useMergedCells;
@Parameters(name="{index}: useMergedCells={0}")
Cell a1 = createRowWithCellValues(sheet, 0, LONG_CELL_VALUE);
assumeRequiredFontsAreInstalled(workbook, a1);
- sheet.addMergedRegion(CellRangeAddress.valueOf("A1:B1"));
+ assertEquals(0, sheet.addMergedRegion(CellRangeAddress.valueOf("A1:B1")));
createRowWithCellValues(sheet, 1, SHORT_CELL_VALUE, SHORT_CELL_VALUE);
- /**
+ /*
* A B
* 1 LONGMERGED
* 2 SHORT SHORT
wbBack.close();
}
+ // This test will run green, but the resulting file is formatted incorrectly,
+ // see the bug at https://bz.apache.org/bugzilla/show_bug.cgi?id=55752
@Test
public void testBug55752() throws IOException {
try (Workbook wb = new XSSFWorkbook()) {
// merge cells
CellRangeAddress range1 = new CellRangeAddress(0, 0, 0, 1);
- sheet.addMergedRegion(range1);
+ assertEquals(0, sheet.addMergedRegion(range1));
CellRangeAddress range2 = new CellRangeAddress(1, 1, 0, 1);
- sheet.addMergedRegion(range2);
+ assertEquals(1, sheet.addMergedRegion(range2));
CellRangeAddress range3 = new CellRangeAddress(2, 2, 0, 1);
- sheet.addMergedRegion(range3);
+ assertEquals(2, sheet.addMergedRegion(range3));
assertEquals(0, range3.getFirstColumn());
assertEquals(1, range3.getLastColumn());
assertEquals(2, range3.getLastRow());
CellRangeAddress range4 = new CellRangeAddress(3, 3, 0, 1);
- sheet.addMergedRegion(range4);
+ assertEquals(3, sheet.addMergedRegion(range4));
// set border
RegionUtil.setBorderBottom(BorderStyle.THIN, range1, sheet);
CellRangeAddress region_2 = CellRangeAddress.valueOf("C3:D4");
CellRangeAddress region_3 = CellRangeAddress.valueOf("E5:F6");
CellRangeAddress region_4 = CellRangeAddress.valueOf("G7:H8");
- sheet.addMergedRegion(region_1);
- sheet.addMergedRegion(region_2);
- sheet.addMergedRegion(region_3);
+ assertEquals(0, sheet.addMergedRegion(region_1));
+ assertEquals(1, sheet.addMergedRegion(region_2));
+ assertEquals(2, sheet.addMergedRegion(region_3));
assertEquals("C3:D4", ctWorksheet.getMergeCells().getMergeCellArray(1).getRef());
assertEquals(3, sheet.getNumMergedRegions());
sheet.removeMergedRegion(1);
assertEquals(0, sheet.getNumMergedRegions());
assertNull(" CTMergeCells should be deleted after removing the last merged " +
"region on the sheet.", sheet.getCTWorksheet().getMergeCells());
- sheet.addMergedRegion(region_1);
- sheet.addMergedRegion(region_2);
- sheet.addMergedRegion(region_3);
- sheet.addMergedRegion(region_4);
+ assertEquals(0, sheet.addMergedRegion(region_1));
+ assertEquals(1, sheet.addMergedRegion(region_2));
+ assertEquals(2, sheet.addMergedRegion(region_3));
+ assertEquals(3, sheet.addMergedRegion(region_4));
// test invalid indexes OOBE
Set<Integer> rmIdx = new HashSet<>(Arrays.asList(5, 6));
sheet.removeMergedRegions(rmIdx);
int sclLoc = sheet.getSheet().findFirstRecordLocBySid(SCLRecord.sid);
int window2Loc = sheet.getSheet().findFirstRecordLocBySid(WindowTwoRecord.sid);
- assertTrue(sclLoc == window2Loc + 1);
+ assertEquals(sclLoc, window2Loc + 1);
// verify limits
try {
assertTrue("Column autosized with only one row: wrong width", sheet.getColumnWidth(0) <= maxWithRow1And2);
//create a region over the 2nd row and auto size the first column
- sheet.addMergedRegion(new CellRangeAddress(1,1,0,1));
+ assertEquals(1, sheet.addMergedRegion(new CellRangeAddress(1,1,0,1)));
assertNotNull(sheet.getMergedRegion(0));
sheet.autoSizeColumn((short)0);
HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
populateSheet(s);
- s.addMergedRegion(new CellRangeAddress(0, 10, 0, 10));
- s.addMergedRegion(new CellRangeAddress(30, 40, 5, 15));
+ assertEquals(0, s.addMergedRegion(new CellRangeAddress(0, 10, 0, 10)));
+ assertEquals(1, s.addMergedRegion(new CellRangeAddress(30, 40, 5, 15)));
sanityChecker.checkHSSFWorkbook(wb1);
HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
*/
@Test
public void bug22720() throws IOException {
- Workbook wb = _testDataProvider.createWorkbook();
- wb.createSheet("TEST");
- Sheet template = wb.getSheetAt(0);
+ Workbook wb = _testDataProvider.createWorkbook();
+ wb.createSheet("TEST");
+ Sheet template = wb.getSheetAt(0);
- template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
- template.addMergedRegion(new CellRangeAddress(2, 3, 0, 2));
+ assertEquals(0, template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2)));
+ assertEquals(1, template.addMergedRegion(new CellRangeAddress(2, 3, 0, 2)));
- Sheet clone = wb.cloneSheet(0);
- int originalMerged = template.getNumMergedRegions();
- assertEquals("2 merged regions", 2, originalMerged);
+ Sheet clone = wb.cloneSheet(0);
+ int originalMerged = template.getNumMergedRegions();
+ assertEquals("2 merged regions", 2, originalMerged);
- //remove merged regions from clone
- for (int i=template.getNumMergedRegions()-1; i>=0; i--) {
- clone.removeMergedRegion(i);
- }
+ //remove merged regions from clone
+ for (int i=template.getNumMergedRegions()-1; i>=0; i--) {
+ clone.removeMergedRegion(i);
+ }
- assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());
- //check if template's merged regions are OK
- if (template.getNumMergedRegions()>0) {
+ assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());
+ //check if template's merged regions are OK
+ if (template.getNumMergedRegions()>0) {
// fetch the first merged region...EXCEPTION OCCURS HERE
template.getMergedRegion(0);
- }
+ }
- wb.close();
+ wb.close();
}
@Test
public void testCloneSheetBasic() throws IOException{
Workbook b = _testDataProvider.createWorkbook();
Sheet s = b.createSheet("Test");
- s.addMergedRegion(new CellRangeAddress(0, 1, 0, 1));
+ assertEquals(0, s.addMergedRegion(new CellRangeAddress(0, 1, 0, 1)));
Sheet clonedSheet = b.cloneSheet(0);
assertEquals("One merged area", 1, clonedSheet.getNumMergedRegions());
/**
* Ensures that pagebreak cloning works properly
- * @throws IOException
*/
@Test
public void testPageBreakClones() throws IOException {
final Sheet sheet = wb.createSheet();
final CellRangeAddress baseRegion = new CellRangeAddress(0, 1, 0, 1); //A1:B2
- sheet.addMergedRegion(baseRegion);
+ assertEquals(0, sheet.addMergedRegion(baseRegion));
try {
final CellRangeAddress duplicateRegion = new CellRangeAddress(0, 1, 0, 1); //A1:B2
}
final CellRangeAddress disjointRegion = new CellRangeAddress(10, 11, 10, 11);
- sheet.addMergedRegion(disjointRegion);
+ assertEquals(1, sheet.addMergedRegion(disjointRegion));
wb.close();
}
SpreadsheetVersion ssVersion = _testDataProvider.getSpreadsheetVersion();
CellRangeAddress region = new CellRangeAddress(0, 1, 0, 1);
- sheet.addMergedRegion(region);
+ assertEquals(0, sheet.addMergedRegion(region));
assertEquals(1, sheet.getNumMergedRegions());
try {
Workbook wb = _testDataProvider.createWorkbook();
Sheet sheet = wb.createSheet();
CellRangeAddress region = new CellRangeAddress(0, 1, 0, 1);
- sheet.addMergedRegion(region);
+ assertEquals(0, sheet.addMergedRegion(region));
region = new CellRangeAddress(2, 3, 0, 1);
- sheet.addMergedRegion(region);
+ assertEquals(1, sheet.addMergedRegion(region));
sheet.removeMergedRegion(0);
assertEquals("there should be no merged regions left!", 0, sheet.getNumMergedRegions());
//an, add, remove, get(0) would null pointer
- sheet.addMergedRegion(region);
+ assertEquals(0, sheet.addMergedRegion(region));
assertEquals("there should now be one merged region!", 1, sheet.getNumMergedRegions());
sheet.removeMergedRegion(0);
assertEquals("there should now be zero merged regions!", 0, sheet.getNumMergedRegions());
//add it again!
region.setLastRow(4);
- sheet.addMergedRegion(region);
+ assertEquals(0, sheet.addMergedRegion(region));
assertEquals("there should now be one merged region!", 1, sheet.getNumMergedRegions());
//should exist now!
for (int r=0; r<10; r++) {
CellRangeAddress region = new CellRangeAddress(r, r, 0, 1);
mergedRegions.put(r, region);
- sheet.addMergedRegion(region);
+ assertEquals(r, sheet.addMergedRegion(region));
}
assertCollectionEquals(mergedRegions.values(), sheet.getMergedRegions());
cell.setCellValue(factory.createRichTextString("second row, second cell"));
CellRangeAddress region = CellRangeAddress.valueOf("A2:B2");
- sheet.addMergedRegion(region);
+ assertEquals(0, sheet.addMergedRegion(region));
sheet.shiftRows(1, 1, 1);
assumeTrue(region1.intersects(region2));
assumeTrue(region2.intersects(region3));
- sh.addMergedRegionUnsafe(region1);
+ assertEquals(0, sh.addMergedRegionUnsafe(region1));
assertTrue(sh.getMergedRegions().contains(region1));
// adding a duplicate or overlapping merged region should not
// raise an exception with the unsafe version of addMergedRegion.
- sh.addMergedRegionUnsafe(region2);
+ assertEquals(1, sh.addMergedRegionUnsafe(region2));
// the safe version of addMergedRegion should throw when trying to add a merged region that overlaps an existing region
assertTrue(sh.getMergedRegions().contains(region2));
assertFalse(sh.getMergedRegions().contains(region3));
}
// addMergedRegion should not re-validate previously-added merged regions
- sh.addMergedRegion(region4);
+ assertEquals(2, sh.addMergedRegion(region4));
// validation methods should detect a problem with previously added merged regions (runs in O(n^2) time)
try {
Workbook wb1 = _testDataProvider.createWorkbook();
Sheet sheet = wb1.createSheet();
- assertEquals(sheet.isDisplayGridlines(), true);
- assertEquals(sheet.isDisplayRowColHeadings(), true);
- assertEquals(sheet.isDisplayFormulas(), false);
- assertEquals(sheet.isDisplayZeros(), true);
+ assertTrue(sheet.isDisplayGridlines());
+ assertTrue(sheet.isDisplayRowColHeadings());
+ assertFalse(sheet.isDisplayFormulas());
+ assertTrue(sheet.isDisplayZeros());
sheet.setDisplayGridlines(false);
sheet.setDisplayRowColHeadings(false);
wb1.close();
sheet = wb2.getSheetAt(0);
- assertEquals(sheet.isDisplayGridlines(), false);
- assertEquals(sheet.isDisplayRowColHeadings(), false);
- assertEquals(sheet.isDisplayFormulas(), true);
- assertEquals(sheet.isDisplayZeros(), false);
+ assertFalse(sheet.isDisplayGridlines());
+ assertFalse(sheet.isDisplayRowColHeadings());
+ assertTrue(sheet.isDisplayFormulas());
+ assertFalse(sheet.isDisplayZeros());
wb2.close();
}
workbook.close();
}
-
-
+
@Test
public void getCellComments() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet("TEST");
// a sheet with no cell comments should return an empty map (not null or raise NPE).
- assertEquals(Collections.emptyMap(), sheet.getCellComments());
+ assertTrue(sheet.getCellComments().isEmpty());
Drawing<?> dg = sheet.createDrawingPatriarch();
ClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
CellAddress B6 = new CellAddress(5, 1);
assertEquals("row, col", hyperlink, sheet.getHyperlink(5, 1));
assertEquals("addr", hyperlink, sheet.getHyperlink(B6));
- assertEquals("no hyperlink at A1", null, sheet.getHyperlink(CellAddress.A1));
+ assertNull("no hyperlink at A1", sheet.getHyperlink(CellAddress.A1));
workbook.close();
}
@Test
- public void removeAllHyperlinks() throws IOException {
+ public void removeAllHyperlinks() {
Workbook workbook = _testDataProvider.createWorkbook();
Hyperlink hyperlink = workbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
hyperlink.setAddress("https://poi.apache.org/");
Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet();
CellRangeAddress region = CellRangeAddress.valueOf("B2:D4");
- sheet.addMergedRegion(region);
+ assertEquals(0, sheet.addMergedRegion(region));
assertEquals("B2:D4", sheet.getMergedRegion(0).formatAsString());
assertEquals(1, sheet.getNumMergedRegions());
trackColumnsForAutoSizingIfSXSSF(sheet);
Row row = sheet.createRow(0);
- Font defaultFont = workbook.getFontAt((short)0);
+ Font defaultFont = workbook.getFontAt(0);
CellStyle style1 = workbook.createCellStyle();
Font font1 = workbook.createFont();
trackColumnsForAutoSizingIfSXSSF(sheet);
Row row = sheet.createRow(0);
- sheet.addMergedRegion(CellRangeAddress.valueOf("A1:B1"));
+ assertEquals(0, sheet.addMergedRegion(CellRangeAddress.valueOf("A1:B1")));
Cell cell0 = row.createCell(0);
cell0.setCellValue("Apache Software Foundation");
workbook.close();
}
-
/**
* Auto-Sizing a column needs to work when we have rows
* passed the 32767 boundary. See bug #48079
protected static void fixFonts(Workbook workbook) {
if (!JvmBugs.hasLineBreakMeasurerBug()) return;
- for (int i=workbook.getNumberOfFonts()-1; i>=0; i--) {
- Font f = workbook.getFontAt((short)0);
+ for (int i=workbook.getNumberOfFontsAsInt()-1; i>=0; i--) {
+ Font f = workbook.getFontAt(0);
if ("Calibri".equals(f.getFontName())) {
f.setFontName("Lucida Sans");
} else if ("Cambria".equals(f.getFontName())) {
package org.apache.poi.ss.usermodel;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
assertEquals("$D1+D$2", formulaD5);
Cell newb5Null = sheet1.getRow(4).getCell(1);
- assertEquals(newb5Null, null);
+ assertNull(newb5Null);
boolean logicalValue = sheet1.getRow(6).getCell(2).getBooleanCellValue();
- assertEquals(logicalValue, false);
+ assertFalse(logicalValue);
Cell textCell = sheet1.getRow(6).getCell(3);
assertEquals(textCell.getStringCellValue(), "TEXT");
assertEquals(textCell.getCellStyle().getAlignment(), HorizontalAlignment.CENTER);
assertEquals("SUM(A3:E3)", formulaD5);
Cell b5Null = sheet1.getRow(4).getCell(1);
- assertEquals(b5Null, null);
+ assertNull(b5Null);
Object c6Null = sheet1.getRow(5).getCell(2); // null cell A5 is shifted
// for 2 columns, so now
// c5 should be null
- assertEquals(c6Null, null);
+ assertNull(c6Null);
}
@Test
String formulaB5 = sheet1.getRow(4).getCell(1).getCellFormula();
assertEquals("$B1+B$2", formulaB5);
Cell newb6Null = sheet1.getRow(5).getCell(1);
- assertEquals(newb6Null, null);
+ assertNull(newb6Null);
}
@Test(expected = IllegalStateException.class)
CellRangeAddress A1_A5 = new CellRangeAddress(0, 4, 0, 0); // NOSONAR, it's more readable this way
CellRangeAddress B1_B3 = new CellRangeAddress(0, 2, 1, 1); // NOSONAR, it's more readable this way
- sheet.addMergedRegion(B1_B3);
- sheet.addMergedRegion(A1_A5);
+ assertEquals(0, sheet.addMergedRegion(B1_B3));
+ assertEquals(1, sheet.addMergedRegion(A1_A5));
// A1:A5 should be moved to B1:B5
// B1:B3 will be removed
CellRangeAddress A1_A5 = new CellRangeAddress(0, 4, 0, 0); // NOSONAR, it's more readable this way
CellRangeAddress B1_B3 = new CellRangeAddress(0, 2, 1, 1); // NOSONAR, it's more readable this way
- sheet.addMergedRegion(A1_A5);
- sheet.addMergedRegion(B1_B3);
+ assertEquals(0, sheet.addMergedRegion(A1_A5));
+ assertEquals(1, sheet.addMergedRegion(B1_B3));
// A1:E1 should be removed
// B1:B3 will be A1:A3
CellRangeAddress region = new CellRangeAddress(0, 2, 0, 0);
assertEquals("A1:A3", region.formatAsString());
- sheet.addMergedRegion(region);
+ assertEquals(0, sheet.addMergedRegion(region));
sheet.shiftColumns(0, 1, 2);
region = sheet.getMergedRegion(0);
CellRangeAddress region = new CellRangeAddress(0, 0, 0, 2);
assertEquals("A1:C1", region.formatAsString());
- sheet.addMergedRegion(region);
+ assertEquals(0, sheet.addMergedRegion(region));
sheet.shiftRows(0, 1, 2);
region = sheet.getMergedRegion(0);
CellRangeAddress A4_B7 = CellRangeAddress.valueOf("A4:B7");
CellRangeAddress C4_D7 = CellRangeAddress.valueOf("C4:D7");
-
- sheet.addMergedRegion(A4_B7);
- sheet.addMergedRegion(C4_D7);
+
+ assertEquals(0, sheet.addMergedRegion(A4_B7));
+ assertEquals(1, sheet.addMergedRegion(C4_D7));
assumeTrue(sheet.getLastRowNum() > 8);
public void testBug55280() throws IOException {
Workbook w = _testDataProvider.createWorkbook();
Sheet s = w.createSheet();
- for (int row = 0; row < 5000; ++row)
- s.addMergedRegion(new CellRangeAddress(row, row, 0, 3));
+ for (int row = 0; row < 5000; ++row) {
+ assertEquals(row, s.addMergedRegion(new CellRangeAddress(row, row, 0, 3)));
+ }
s.shiftRows(0, 4999, 1); // takes a long time...
w.close();
CellRangeAddress A4_B7 = new CellRangeAddress(3, 6, 0, 1);
CellRangeAddress C5_D7 = new CellRangeAddress(4, 6, 2, 3);
- sheet.addMergedRegion(A4_B7);
- sheet.addMergedRegion(C5_D7);
+ assertEquals(0, sheet.addMergedRegion(A4_B7));
+ assertEquals(1, sheet.addMergedRegion(C5_D7));
// A4:B7 will elongate vertically
// C5:D7 will be shifted down with same size
CellRangeAddress A1_E1 = new CellRangeAddress(0, 0, 0, 4);
CellRangeAddress A2_C2 = new CellRangeAddress(1, 1, 0, 2);
- sheet.addMergedRegion(A1_E1);
- sheet.addMergedRegion(A2_C2);
+ assertEquals(0, sheet.addMergedRegion(A1_E1));
+ assertEquals(1, sheet.addMergedRegion(A2_C2));
// A1:E1 should be removed
// A2:C2 will be A1:C1
CellRangeAddress A1_E1 = new CellRangeAddress(0, 0, 0, 4);
CellRangeAddress A2_C2 = new CellRangeAddress(1, 7, 0, 2);
- sheet.addMergedRegion(A1_E1);
- sheet.addMergedRegion(A2_C2);
+ assertEquals(0, sheet.addMergedRegion(A1_E1));
+ assertEquals(1, sheet.addMergedRegion(A2_C2));
// A1:E1 should move to A5:E5
// A2:C2 should be removed
CellRangeAddress A8_E8 = new CellRangeAddress(7, 7, 0, 4);
CellRangeAddress A9_C9 = new CellRangeAddress(8, 8, 0, 2);
- sheet.addMergedRegion(A8_E8);
- sheet.addMergedRegion(A9_C9);
+ assertEquals(0, sheet.addMergedRegion(A8_E8));
+ assertEquals(1, sheet.addMergedRegion(A9_C9));
// A1:E1 should be removed
// A2:C2 will be A1:C1
CellRangeAddress A1_E1 = new CellRangeAddress(0, 0, 0, 4);
CellRangeAddress A2_C2 = new CellRangeAddress(1, 1, 0, 2);
- sheet.addMergedRegion(A1_E1);
- sheet.addMergedRegion(A2_C2);
+ assertEquals(0, sheet.addMergedRegion(A1_E1));
+ assertEquals(1, sheet.addMergedRegion(A2_C2));
// A1:E1 should be moved to A2:E2
// A2:C2 will be removed
CellRange<? extends Cell> srange =
sheet.setArrayFormula("SUM(A4:A6,B4:B6)", CellRangeAddress.valueOf("B5"));
Cell scell = srange.getTopLeftCell();
- sheet.addMergedRegion(CellRangeAddress.valueOf("B5:C6"));
+ assertEquals(0, sheet.addMergedRegion(CellRangeAddress.valueOf("B5:C6")));
//we are still an array formula
assertEquals(CellType.FORMULA, scell.getCellType());
assertTrue(scell.isPartOfArrayFormulaGroup());
Sheet sheet = workbook.createSheet();
CellRangeAddress mergedRegion = CellRangeAddress.valueOf("B2:C2");
- sheet.addMergedRegion(mergedRegion);
+ assertEquals(0, sheet.addMergedRegion(mergedRegion));
CellRangeAddress arrayFormula = CellRangeAddress.valueOf("C1:C3");
assumeTrue(mergedRegion.intersects(arrayFormula));
assumeTrue(arrayFormula.intersects(mergedRegion));
Sheet sheet = book.createSheet("TEST");
sheet.createRow(0).createCell(0).setCellValue("Test");
sheet.createRow(1).createCell(0).setCellValue(36.6);
- sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
- sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 2));
+ assertEquals(0, sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 2)));
+ assertEquals(1, sheet.addMergedRegion(new CellRangeAddress(2, 3, 0, 2)));
assertTrue(sheet.isSelected());
Sheet clonedSheet = book.cloneSheet(0);
//cloned sheet is a deep copy, adding rows or merged regions in the original does not affect the clone
sheet.createRow(2).createCell(0).setCellValue(1);
- sheet.addMergedRegion(new CellRangeAddress(4, 5, 0, 2));
+ assertEquals(2, sheet.addMergedRegion(new CellRangeAddress(4, 5, 0, 2)));
assertEquals(2, clonedSheet.getPhysicalNumberOfRows());
assertEquals(2, clonedSheet.getNumMergedRegions());
clonedSheet.createRow(2).createCell(0).setCellValue(1);
- clonedSheet.addMergedRegion(new CellRangeAddress(6, 7, 0, 2));
+ assertEquals(2, clonedSheet.addMergedRegion(new CellRangeAddress(6, 7, 0, 2)));
assertEquals(3, clonedSheet.getPhysicalNumberOfRows());
assertEquals(3, clonedSheet.getNumMergedRegions());
book.close();
package org.apache.poi.ss.util;
-import java.io.IOException;
-
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
+import org.junit.Test;
+
+import java.io.IOException;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
/**
* Tests SheetUtil.
*
* @see org.apache.poi.ss.util.SheetUtil
*/
-public final class TestSheetUtil extends TestCase {
+public final class TestSheetUtil {
+ @Test
public void testCellWithMerges() throws Exception {
Workbook wb = new HSSFWorkbook();
Sheet s = wb.createSheet();
Row r3 = s.createRow(2);
r3.createCell(0).setCellValue(20);
r3.createCell(1).setCellValue(21);
-
- s.addMergedRegion(new CellRangeAddress(2, 3, 0, 0));
- s.addMergedRegion(new CellRangeAddress(2, 2, 1, 4));
+
+ assertEquals(0, s.addMergedRegion(new CellRangeAddress(2, 3, 0, 0)));
+ assertEquals(1, s.addMergedRegion(new CellRangeAddress(2, 2, 1, 4)));
// With a cell that isn't defined, we'll get null
- assertEquals(null, SheetUtil.getCellWithMerges(s, 0, 0));
+ assertNull(SheetUtil.getCellWithMerges(s, 0, 0));
// With a cell that's not in a merged region, we'll get that
- assertEquals(10.0, SheetUtil.getCellWithMerges(s, 1, 0).getNumericCellValue());
- assertEquals(11.0, SheetUtil.getCellWithMerges(s, 1, 1).getNumericCellValue());
+ assertEquals(10.0, SheetUtil.getCellWithMerges(s, 1, 0).getNumericCellValue(), 0.01);
+ assertEquals(11.0, SheetUtil.getCellWithMerges(s, 1, 1).getNumericCellValue(), 0.01);
// With a cell that's the primary one of a merged region, we get that cell
- assertEquals(20.0, SheetUtil.getCellWithMerges(s, 2, 0).getNumericCellValue());
- assertEquals(21., SheetUtil.getCellWithMerges(s, 2, 1).getNumericCellValue());
+ assertEquals(20.0, SheetUtil.getCellWithMerges(s, 2, 0).getNumericCellValue(), 0.01);
+ assertEquals(21., SheetUtil.getCellWithMerges(s, 2, 1).getNumericCellValue(), 0.01);
// With a cell elsewhere in the merged region, get top-left
- assertEquals(20.0, SheetUtil.getCellWithMerges(s, 3, 0).getNumericCellValue());
- assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 2).getNumericCellValue());
- assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 3).getNumericCellValue());
- assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 4).getNumericCellValue());
+ assertEquals(20.0, SheetUtil.getCellWithMerges(s, 3, 0).getNumericCellValue(), 0.01);
+ assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 2).getNumericCellValue(), 0.01);
+ assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 3).getNumericCellValue(), 0.01);
+ assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 4).getNumericCellValue(), 0.01);
wb.close();
}
-
+
+ @Test
public void testCanComputeWidthHSSF() throws IOException {
Workbook wb = new HSSFWorkbook();
// cannot check on result because on some machines we get back false here!
- SheetUtil.canComputeColumnWidth(wb.getFontAt((short)0));
+ SheetUtil.canComputeColumnWidth(wb.getFontAt(0));
wb.close();
}
+ @Test
public void testGetCellWidthEmpty() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("sheet");
// no contents: cell.setCellValue("sometext");
- assertEquals(-1.0, SheetUtil.getCellWidth(cell, 1, null, true));
+ assertEquals(-1.0, SheetUtil.getCellWidth(cell, 1, null, true), 0.01);
wb.close();
}
+ @Test
public void testGetCellWidthString() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("sheet");
wb.close();
}
+ @Test
public void testGetCellWidthNumber() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("sheet");
wb.close();
}
+ @Test
public void testGetCellWidthBoolean() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("sheet");
wb.close();
}
+ @Test
public void testGetColumnWidthString() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("sheet");
assertTrue("Having some width for rows with actual cells",
SheetUtil.getColumnWidth(sheet, 0, true) > 0);
assertEquals("Not having any widht for rows with all empty cells",
- -1.0, SheetUtil.getColumnWidth(sheet, 0, true, 1, 2));
+ -1.0, SheetUtil.getColumnWidth(sheet, 0, true, 1, 2), 0.01);
wb.close();
}