]> source.dussan.org Git - poi.git/commitdiff
Have autoSizeColumn skip over merged regions - bug #43902. Patch from Paolo
authorNick Burch <nick@apache.org>
Wed, 9 Jan 2008 09:55:20 +0000 (09:55 +0000)
committerNick Burch <nick@apache.org>
Wed, 9 Jan 2008 09:55:20 +0000 (09:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@610328 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/testcases/org/apache/poi/hssf/data/43902.xls [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java

index 5855a4937eee332647ee841bc20ffa654809bace..6fc113c085760f50b991c666824377fa3a15ce51 100644 (file)
@@ -1615,7 +1615,13 @@ public class HSSFSheet
         for (Iterator it = rowIterator(); it.hasNext();) {
             HSSFRow row = (HSSFRow) it.next();
             HSSFCell cell = row.getCell(column);
-            if (cell == null) continue;
+
+            boolean isCellInMergedRegion = false;
+            for (int i = 0 ; i < getNumMergedRegions() && ! isCellInMergedRegion; i++) {
+                isCellInMergedRegion = getMergedRegionAt(i).contains(row.getRowNum(), column);
+            }
+
+            if (cell == null | isCellInMergedRegion) continue;
 
             HSSFCellStyle style = cell.getCellStyle();
             HSSFFont font = wb.getFontAt(style.getFontIndex());
diff --git a/src/testcases/org/apache/poi/hssf/data/43902.xls b/src/testcases/org/apache/poi/hssf/data/43902.xls
new file mode 100644 (file)
index 0000000..a67720d
Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/43902.xls differ
index f6f506c57a0022d0058c18659d813ef323e6b371..9a6f9dca000cc3247491b93b61327dd0c2681e67 100644 (file)
@@ -32,6 +32,7 @@ import org.apache.poi.hssf.record.VCenterRecord;
 import org.apache.poi.hssf.record.WSBoolRecord;
 import org.apache.poi.hssf.record.WindowTwoRecord;
 import org.apache.poi.hssf.util.Region;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.TempFile;
 
 /**
@@ -614,6 +615,44 @@ public class TestHSSFSheet
         workbook = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
         assertTrue("No Exceptions while reading file", true);
 
+    }
+    
+    public void testAutoSizeColumn() throws Exception {
+               String filename = System.getProperty("HSSF.testdata.path");
+               filename = filename + "/43902.xls";
+               String sheetName = "my sheet";
+               FileInputStream is = new FileInputStream(filename);
+               POIFSFileSystem fs = new POIFSFileSystem(is);
+               HSSFWorkbook wb = new HSSFWorkbook(fs);
+               HSSFSheet sheet = wb.getSheet(sheetName);
+               
+               // autoSize the first column and check its size before the merged region (1,0,1,1) is set:
+               // it has to be based on the 2nd row width
+               sheet.autoSizeColumn((short)0);
+               assertEquals("Column autosized with only one row: wrong width", (short)7169, sheet.getColumnWidth((short)0));
+               
+               //create a region over the 2nd row and auto size the first column
+               sheet.addMergedRegion(new Region(1,(short)0,1,(short)1));
+               sheet.autoSizeColumn((short)0);
+               ByteArrayOutputStream out = new ByteArrayOutputStream();
+               wb.write(out);
+               out.close();
+               
+               // check that the autoSized column width has ignored the 2nd row 
+               // because it is included in a merged region (Excel like behavior)
+               HSSFWorkbook wb2 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
+               HSSFSheet sheet2 = wb2.getSheet(sheetName);
+               assertEquals((short)3024, sheet2.getColumnWidth((short)0));
+               
+               // remove the 2nd row merged region and check that the 2nd row value is used to the autoSizeColumn width
+               sheet2.removeMergedRegion(1);
+               sheet2.autoSizeColumn((short)0);
+               out = new ByteArrayOutputStream();
+               wb2.write(out);
+               out.close();
+               HSSFWorkbook wb3 = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
+               HSSFSheet sheet3 = wb3.getSheet(sheetName);
+               assertEquals((short)7169, sheet3.getColumnWidth((short)0));
     }
 
        public static void main(java.lang.String[] args) {