From e68a17466f21b85e295af28ae8ab2be67c7bb757 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 2 Apr 2008 10:25:18 +0000 Subject: [PATCH] Fixed inconsistency between HSSFSHeet.getColumnWidth and HSSFSheet.getDefaultColumnWidth: getColumnWidth should always return width measured in 1/256th units. git-svn-id: https://svn.eu.apache.org/repos/asf/poi/trunk@643834 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hssf/model/Sheet.java | 12 ++-- .../org/apache/poi/hssf/data/colwidth.xls | Bin 0 -> 13824 bytes .../apache/poi/hssf/usermodel/TestHSSFSheet.java | 62 ++++++++++++++++++++- 3 files changed, 68 insertions(+), 6 deletions(-) create mode 100755 src/testcases/org/apache/poi/hssf/data/colwidth.xls diff --git a/src/java/org/apache/poi/hssf/model/Sheet.java b/src/java/org/apache/poi/hssf/model/Sheet.java index 451cde7..2af25e7 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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uLeG?9P) i4)OPAx&Gw^(Ru$5k%s|^AtRr_%Hrwz3&-!w7r-BIJwNvV literal 0 HcwPel00001 diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index a4a31fe..13eafa4 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)); + } + + } } -- 2.11.4.GIT