From df4c00f034d1d98eddbb83b594d462da52ff8d44 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 9 Jan 2008 09:55:20 +0000 Subject: [PATCH] Have autoSizeColumn skip over merged regions - bug #43902. Patch from Paolo git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@610328 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFSheet.java | 8 +++- .../org/apache/poi/hssf/data/43902.xls | Bin 0 -> 83456 bytes .../poi/hssf/usermodel/TestHSSFSheet.java | 39 ++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/testcases/org/apache/poi/hssf/data/43902.xls diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index 5855a4937e..6fc113c085 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -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 index 0000000000000000000000000000000000000000..a67720d6c410ef5e16a2fbad1199d270d16a431c GIT binary patch literal 83456 zcmeI43wRXe)yHR(?Cd1Q5HX7s>}-^BzX*Z~q7|i}sEFYr6;T=>MuZS@gM=88&1S_g zfw3#mgYNe~GrM84f`TGx?V0pH zJM-Sp@4cNFroOuGf_Ivq>hZ1=b&Qr`r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P#U->_-UimkFrIJ5!Y?1wsF@{>Nqc6#rM|s;+b;Symr}J!;w`!Fc(GmdmhZML^YY>iDuYz)SfHNmu4I7nBOts zY20;{V(=$PaS*lRG_n{;a?dkUTBNb4IZIk3O+p*)Ki%x@>8O6PC(NKd;!7^*W1tD4 zcnj)XAVq&R^Cg+1q7qPlxbD-ne<;dG36*1&rbq>_KTAqC`hEgN!M#SGP)NQs57!H$ z>WsYY7pmWiz|*Zi0cjKa(P)t{dVYi&<``|rFh<~+wP)nluzwip%{OX4JEt?{PygME Nvs1HK$~US1{{bv%){y`J literal 0 HcmV?d00001 diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index f6f506c57a..9a6f9dca00 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -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) { -- 2.39.5