View | Details | Raw Unified | Return to bug 43902
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java (-1 / +7 lines)
Lines 1615-1622 Link Here
1615
        for (Iterator it = rowIterator(); it.hasNext();) {
1615
        for (Iterator it = rowIterator(); it.hasNext();) {
1616
            HSSFRow row = (HSSFRow) it.next();
1616
            HSSFRow row = (HSSFRow) it.next();
1617
            HSSFCell cell = row.getCell(column);
1617
            HSSFCell cell = row.getCell(column);
1618
            if (cell == null) continue;
1619
1618
1619
            boolean isCellInMergedRegion = false;
1620
            for (int i = 0 ; i < getNumMergedRegions() && ! isCellInMergedRegion; i++) {
1621
                isCellInMergedRegion = getMergedRegionAt(i).contains(row.getRowNum(), column);
1622
            }
1623
1624
            if (cell == null | isCellInMergedRegion) continue;
1625
1620
            HSSFCellStyle style = cell.getCellStyle();
1626
            HSSFCellStyle style = cell.getCellStyle();
1621
            HSSFFont font = wb.getFontAt(style.getFontIndex());
1627
            HSSFFont font = wb.getFontAt(style.getFontIndex());
1622
1628
(-)src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java (+39 lines)
Lines 32-37 Link Here
32
import org.apache.poi.hssf.record.WSBoolRecord;
32
import org.apache.poi.hssf.record.WSBoolRecord;
33
import org.apache.poi.hssf.record.WindowTwoRecord;
33
import org.apache.poi.hssf.record.WindowTwoRecord;
34
import org.apache.poi.hssf.util.Region;
34
import org.apache.poi.hssf.util.Region;
35
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
35
import org.apache.poi.util.TempFile;
36
import org.apache.poi.util.TempFile;
36
37
37
/**
38
/**
Lines 615-620 Link Here
615
        assertTrue("No Exceptions while reading file", true);
616
        assertTrue("No Exceptions while reading file", true);
616
617
617
    }
618
    }
619
    
620
    public void testAutoSizeColumn() throws Exception {
621
		String filename = System.getProperty("HSSF.testdata.path");
622
		filename = filename + "/43902.xls";
623
		String sheetName = "my sheet";
624
		FileInputStream is = new FileInputStream(filename);
625
		POIFSFileSystem fs = new POIFSFileSystem(is);
626
		HSSFWorkbook wb = new HSSFWorkbook(fs);
627
		HSSFSheet sheet = wb.getSheet(sheetName);
628
		
629
		// autoSize the first column and check its size before the merged region (1,0,1,1) is set:
630
		// it has to be based on the 2nd row width
631
		sheet.autoSizeColumn((short)0);
632
		assertEquals("Column autosized with only one row: wrong width", (short)7169, sheet.getColumnWidth((short)0));
633
		
634
		//create a region over the 2nd row and auto size the first column
635
		sheet.addMergedRegion(new Region(1,(short)0,1,(short)1));
636
		sheet.autoSizeColumn((short)0);
637
		ByteArrayOutputStream out = new ByteArrayOutputStream();
638
		wb.write(out);
639
		out.close();
640
		
641
		// check that the autoSized column width has ignored the 2nd row 
642
		// because it is included in a merged region (Excel like behavior)
643
		HSSFWorkbook wb2 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
644
		HSSFSheet sheet2 = wb2.getSheet(sheetName);
645
		assertEquals((short)3024, sheet2.getColumnWidth((short)0));
646
		
647
		// remove the 2nd row merged region and check that the 2nd row value is used to the autoSizeColumn width
648
		sheet2.removeMergedRegion(1);
649
		sheet2.autoSizeColumn((short)0);
650
		out = new ByteArrayOutputStream();
651
		wb2.write(out);
652
		out.close();
653
		HSSFWorkbook wb3 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
654
		HSSFSheet sheet3 = wb3.getSheet(sheetName);
655
		assertEquals((short)7169, sheet3.getColumnWidth((short)0));
656
    }
618
657
619
	public static void main(java.lang.String[] args) {
658
	public static void main(java.lang.String[] args) {
620
		 junit.textui.TestRunner.run(TestHSSFSheet.class);
659
		 junit.textui.TestRunner.run(TestHSSFSheet.class);

Return to bug 43902