From: Yegor Kozlov Date: Wed, 2 Apr 2008 10:25:18 +0000 (+0000) Subject: Fixed inconsistency between HSSFSHeet.getColumnWidth and HSSFSheet.getDefaultColumnWi... X-Git-Tag: REL_3_0_3_BETA1~46 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=434ce09afdb94ad59a93ab70f55fe18c96e88cf4;p=poi.git Fixed inconsistency between HSSFSHeet.getColumnWidth and HSSFSheet.getDefaultColumnWidth: getColumnWidth should always return width measured in 1/256th units. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@643834 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 451cde7579..2af25e7481 100644 --- a/src/java/org/apache/poi/hssf/model/Sheet.java +++ b/src/java/org/apache/poi/hssf/model/Sheet.java @@ -1879,12 +1879,12 @@ public class Sheet implements Model } /** - * get the width of a given column in units of 1/20th of a point width (twips?) + * get the width of a given column in units of 1/256th of a character width * @param column index * @see org.apache.poi.hssf.record.DefaultColWidthRecord * @see org.apache.poi.hssf.record.ColumnInfoRecord * @see #setColumnWidth(short,short) - * @return column width in units of 1/20th of a point (twips?) + * @return column width in units of 1/256th of a character width */ public short getColumnWidth(short column) @@ -1912,7 +1912,9 @@ public class Sheet implements Model } else { - retval = defaultcolwidth.getColWidth(); + //default column width is measured in characters + //multiply + retval = (short)(256*defaultcolwidth.getColWidth()); } return retval; } @@ -1951,9 +1953,9 @@ public class Sheet implements Model } /** - * set the width for a given column in 1/20th of a character width units + * set the width for a given column in 1/256th of a character width units * @param column - the column number - * @param width (in units of 1/20th of a character width) + * @param width (in units of 1/256th of a character width) */ public void setColumnWidth(short column, short width) { diff --git a/src/testcases/org/apache/poi/hssf/data/colwidth.xls b/src/testcases/org/apache/poi/hssf/data/colwidth.xls new file mode 100755 index 0000000000..7ffd3a42b4 Binary files /dev/null and b/src/testcases/org/apache/poi/hssf/data/colwidth.xls differ diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index a4a31feabf..13eafa4fd8 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -870,5 +870,65 @@ public class TestHSSFSheet public static void main(java.lang.String[] args) { junit.textui.TestRunner.run(TestHSSFSheet.class); - } + } + + public void testColumnWidth() throws Exception { + //check we can correctly read column widths from a reference workbook + String filename = System.getProperty("HSSF.testdata.path") + "/colwidth.xls"; + HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filename)); + + //reference values + int[] ref = {365, 548, 731, 914, 1097, 1280, 1462, 1645, 1828, 2011, 2194, 2377, 2560, 2742, 2925, 3108, 3291, 3474, 3657}; + + HSSFSheet sh = wb.getSheetAt(0); + for (char i = 'A'; i <= 'S'; i++) { + int idx = i - 'A'; + int w = sh.getColumnWidth((short)idx); + assertEquals(ref[idx], w); + } + + //the second sheet doesn't have overridden column widths + sh = wb.getSheetAt(1); + int def_width = sh.getDefaultColumnWidth(); + for (char i = 'A'; i <= 'S'; i++) { + int idx = i - 'A'; + int w = sh.getColumnWidth((short)idx); + //getDefaultColumnWidth returns width measued in characters + //getColumnWidth returns width measued in 1/256th units + assertEquals(def_width*256, w); + } + + //test new workbook + wb = new HSSFWorkbook(); + sh = wb.createSheet(); + sh.setDefaultColumnWidth((short)10); + assertEquals(10, sh.getDefaultColumnWidth()); + assertEquals(256*10, sh.getColumnWidth((short)0)); + assertEquals(256*10, sh.getColumnWidth((short)1)); + assertEquals(256*10, sh.getColumnWidth((short)2)); + for (char i = 'D'; i <= 'F'; i++) { + short w = (short)(256*12); + sh.setColumnWidth((short)i, w); + assertEquals(w, sh.getColumnWidth((short)i)); + } + + //serialize and read again + ByteArrayOutputStream out = new ByteArrayOutputStream(); + wb.write(out); + out.close(); + + wb = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray())); + sh = wb.getSheetAt(0); + assertEquals(10, sh.getDefaultColumnWidth()); + //columns A-C have default width + assertEquals(256*10, sh.getColumnWidth((short)0)); + assertEquals(256*10, sh.getColumnWidth((short)1)); + assertEquals(256*10, sh.getColumnWidth((short)2)); + //columns D-F have custom wodth + for (char i = 'D'; i <= 'F'; i++) { + short w = (short)(256*12); + assertEquals(w, sh.getColumnWidth((short)i)); + } + + } }