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
This commit is contained in:
Yegor Kozlov 2008-04-02 10:25:18 +00:00
parent 65c16252b7
commit a4cb19f0e8
3 changed files with 68 additions and 6 deletions

View File

@ -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 * @param column index
* @see org.apache.poi.hssf.record.DefaultColWidthRecord * @see org.apache.poi.hssf.record.DefaultColWidthRecord
* @see org.apache.poi.hssf.record.ColumnInfoRecord * @see org.apache.poi.hssf.record.ColumnInfoRecord
* @see #setColumnWidth(short,short) * @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) public short getColumnWidth(short column)
@ -1912,7 +1912,9 @@ public class Sheet implements Model
} }
else else
{ {
retval = defaultcolwidth.getColWidth(); //default column width is measured in characters
//multiply
retval = (short)(256*defaultcolwidth.getColWidth());
} }
return retval; 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 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) public void setColumnWidth(short column, short width)
{ {

Binary file not shown.

View File

@ -871,4 +871,64 @@ public class TestHSSFSheet
public static void main(java.lang.String[] args) { public static void main(java.lang.String[] args) {
junit.textui.TestRunner.run(TestHSSFSheet.class); 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));
}
}
} }