Fix for bug 45805 - 16-bit signed/unsigned problems in HSSFSheet.getColWidth() etc

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@696075 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-09-16 21:38:47 +00:00
parent 02959a5b39
commit 2741d5a72f
13 changed files with 159 additions and 143 deletions

View File

@ -1,4 +1,3 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
@ -16,8 +15,6 @@
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hssf.contrib.view; package org.apache.poi.hssf.contrib.view;
import java.awt.*; import java.awt.*;
@ -47,10 +44,10 @@ public class SViewerPanel extends JPanel {
* *
*/ */
private static final int magicCharFactor = 7; private static final int magicCharFactor = 7;
/** Reference to the woorkbook that is being displayed*/ /** Reference to the wookbook that is being displayed*/
private HSSFWorkbook wb; /* package */ HSSFWorkbook wb;
/** Reference to the tabs component*/ /** Reference to the tabs component*/
private JTabbedPane sheetPane; /* package */ JTabbedPane sheetPane;
/** Reference to the cell renderer that is used to render all cells*/ /** Reference to the cell renderer that is used to render all cells*/
private SVTableCellRenderer cellRenderer; private SVTableCellRenderer cellRenderer;
/** Reference to the cell editor that is used to edit all cells. /** Reference to the cell editor that is used to edit all cells.
@ -106,7 +103,7 @@ public class SViewerPanel extends JPanel {
TableColumnModel columns = sheetView.getColumnModel(); TableColumnModel columns = sheetView.getColumnModel();
for (int i=0; i< columns.getColumnCount(); i++) { for (int i=0; i< columns.getColumnCount(); i++) {
TableColumn column = columns.getColumn(i); TableColumn column = columns.getColumn(i);
short width = sheet.getColumnWidth((short)i); int width = sheet.getColWidth(i);
//256 is because the width is in 256ths of a character //256 is because the width is in 256ths of a character
column.setPreferredWidth(width/256*magicCharFactor); column.setPreferredWidth(width/256*magicCharFactor);
} }
@ -208,7 +205,7 @@ public class SViewerPanel extends JPanel {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
int tabIndex = sheetPane.getSelectedIndex(); int tabIndex = sheetPane.getSelectedIndex();
if (tabIndex != -1) { if (tabIndex != -1) {
String newSheetName = (String)JOptionPane.showInputDialog(sheetPane, "Enter a new Sheetname", "Rename Sheet", JOptionPane.QUESTION_MESSAGE); String newSheetName = JOptionPane.showInputDialog(sheetPane, "Enter a new Sheetname", "Rename Sheet", JOptionPane.QUESTION_MESSAGE);
if (newSheetName != null) { if (newSheetName != null) {
wb.setSheetName(tabIndex, newSheetName); wb.setSheetName(tabIndex, newSheetName);
sheetPane.setTitleAt(tabIndex, newSheetName); sheetPane.setTitleAt(tabIndex, newSheetName);

View File

@ -37,6 +37,7 @@
<!-- Don't forget to update status.xml too! --> <!-- Don't forget to update status.xml too! -->
<release version="3.2-alpha1" date="2008-??-??"> <release version="3.2-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">45805 - Fixed 16-bit signed/unsigned bug in HSSFSheet.getColWidth etc</action>
<action dev="POI-DEVELOPERS" type="fix">45780 - Fixed HSSFSheet.shiftRows to also update Area refs</action> <action dev="POI-DEVELOPERS" type="fix">45780 - Fixed HSSFSheet.shiftRows to also update Area refs</action>
<action dev="POI-DEVELOPERS" type="fix">45804 - Update HSMF to handle Outlook 3.0 msg files, which have a different string chunk type</action> <action dev="POI-DEVELOPERS" type="fix">45804 - Update HSMF to handle Outlook 3.0 msg files, which have a different string chunk type</action>
<action dev="POI-DEVELOPERS" type="add">Expose the name of Named Cell Styles via HSSFCellStyle (normally held on the parent style though)</action> <action dev="POI-DEVELOPERS" type="add">Expose the name of Named Cell Styles via HSSFCellStyle (normally held on the parent style though)</action>

View File

@ -34,6 +34,7 @@
<!-- Don't forget to update changes.xml too! --> <!-- Don't forget to update changes.xml too! -->
<changes> <changes>
<release version="3.2-alpha1" date="2008-??-??"> <release version="3.2-alpha1" date="2008-??-??">
<action dev="POI-DEVELOPERS" type="fix">45805 - Fixed 16-bit signed/unsigned bug in HSSFSheet.getColWidth etc</action>
<action dev="POI-DEVELOPERS" type="fix">45780 - Fixed HSSFSheet.shiftRows to also update Area refs</action> <action dev="POI-DEVELOPERS" type="fix">45780 - Fixed HSSFSheet.shiftRows to also update Area refs</action>
<action dev="POI-DEVELOPERS" type="fix">45804 - Update HSMF to handle Outlook 3.0 msg files, which have a different string chunk type</action> <action dev="POI-DEVELOPERS" type="fix">45804 - Update HSMF to handle Outlook 3.0 msg files, which have a different string chunk type</action>
<action dev="POI-DEVELOPERS" type="add">Expose the name of Named Cell Styles via HSSFCellStyle (normally held on the parent style though)</action> <action dev="POI-DEVELOPERS" type="add">Expose the name of Named Cell Styles via HSSFCellStyle (normally held on the parent style though)</action>

View File

@ -134,8 +134,7 @@ public class HSSF
c = r.createCell(cellnum + 1, c = r.createCell(cellnum + 1,
HSSFCell.CELL_TYPE_STRING); HSSFCell.CELL_TYPE_STRING);
c.setCellValue(new HSSFRichTextString("TEST")); c.setCellValue(new HSSFRichTextString("TEST"));
s.setColumnWidth(( short ) (cellnum + 1), s.setColWidth(cellnum + 1, (int)(50 * 8 / 0.05));
( short ) ((50 * 8) / (( double ) 1 / 20)));
if ((rownum % 2) == 0) if ((rownum % 2) == 0)
{ {
c.setCellStyle(cs2); c.setCellStyle(cs2);

View File

@ -973,9 +973,7 @@ public final class Sheet implements Model {
* get the default column width for the sheet (if the columns do not define their own width) * get the default column width for the sheet (if the columns do not define their own width)
* @return default column width * @return default column width
*/ */
public int getDefaultColumnWidth() {
public short getDefaultColumnWidth()
{
return defaultcolwidth.getColWidth(); return defaultcolwidth.getColWidth();
} }
@ -1009,9 +1007,7 @@ public final class Sheet implements Model {
* set the default column width for the sheet (if the columns do not define their own width) * set the default column width for the sheet (if the columns do not define their own width)
* @param dcw default column width * @param dcw default column width
*/ */
public void setDefaultColumnWidth(int dcw) {
public void setDefaultColumnWidth(short dcw)
{
defaultcolwidth.setColWidth(dcw); defaultcolwidth.setColWidth(dcw);
} }
@ -1043,15 +1039,15 @@ public final class Sheet implements Model {
* @return column width in units of 1/256th of a character width * @return column width in units of 1/256th of a character width
*/ */
public short getColumnWidth(short columnIndex) { public int getColumnWidth(int columnIndex) {
ColumnInfoRecord ci = _columnInfos.findColumnInfo(columnIndex); ColumnInfoRecord ci = _columnInfos.findColumnInfo(columnIndex);
if (ci != null) { if (ci != null) {
return (short)ci.getColumnWidth(); return ci.getColumnWidth();
} }
//default column width is measured in characters //default column width is measured in characters
//multiply //multiply
return (short)(256*defaultcolwidth.getColWidth()); return (256*defaultcolwidth.getColWidth());
} }
/** /**
@ -1084,8 +1080,8 @@ public final class Sheet implements Model {
* @param width * @param width
* (in units of 1/256th of a character width) * (in units of 1/256th of a character width)
*/ */
public void setColumnWidth(short column, short width) { public void setColumnWidth(int column, int width) {
setColumn( column, new Short(width), null, null, null); setColumn(column, null, new Integer(width), null, null, null);
} }
/** /**
@ -1096,7 +1092,7 @@ public final class Sheet implements Model {
* @see #setColumnHidden(short,boolean) * @see #setColumnHidden(short,boolean)
* @return whether the column is hidden or not. * @return whether the column is hidden or not.
*/ */
public boolean isColumnHidden(short columnIndex) { public boolean isColumnHidden(int columnIndex) {
ColumnInfoRecord cir = _columnInfos.findColumnInfo(columnIndex); ColumnInfoRecord cir = _columnInfos.findColumnInfo(columnIndex);
if (cir == null) { if (cir == null) {
return false; return false;
@ -1109,16 +1105,14 @@ public final class Sheet implements Model {
* @param column - the column number * @param column - the column number
* @param hidden - whether the column is hidden or not * @param hidden - whether the column is hidden or not
*/ */
public void setColumnHidden(short column, boolean hidden) public void setColumnHidden(int column, boolean hidden) {
{ setColumn( column, null, null, null, Boolean.valueOf(hidden), null);
setColumn( column, null, null, new Boolean(hidden), null); }
public void setDefaultColumnStyle(int column, int styleIndex) {
setColumn(column, new Short((short)styleIndex), null, null, null, null);
} }
public void setColumn(short column, Short width, Integer level, Boolean hidden, Boolean collapsed) { private void setColumn(int column, Short xfStyle, Integer width, Integer level, Boolean hidden, Boolean collapsed) {
_columnInfos.setColumn( column, null, width, level, hidden, collapsed );
}
public void setColumn(short column, Short xfStyle, Short width, Integer level, Boolean hidden, Boolean collapsed) {
_columnInfos.setColumn( column, xfStyle, width, level, hidden, collapsed ); _columnInfos.setColumn( column, xfStyle, width, level, hidden, collapsed );
} }

View File

@ -40,7 +40,7 @@ public final class ColumnInfoRecord extends Record {
private static final BitField outlevel = BitFieldFactory.getInstance(0x0700); private static final BitField outlevel = BitFieldFactory.getInstance(0x0700);
private static final BitField collapsed = BitFieldFactory.getInstance(0x1000); private static final BitField collapsed = BitFieldFactory.getInstance(0x1000);
// Excel seems write values 2, 10, and 260, even though spec says "must be zero" // Excel seems write values 2, 10, and 260, even though spec says "must be zero"
private short field_6_reserved; private int field_6_reserved;
/** /**
* Creates a column info record with default width and format * Creates a column info record with default width and format
@ -64,14 +64,14 @@ public final class ColumnInfoRecord extends Record {
protected void fillFields(RecordInputStream in) protected void fillFields(RecordInputStream in)
{ {
field_1_first_col = in.readShort(); field_1_first_col = in.readUShort();
field_2_last_col = in.readShort(); field_2_last_col = in.readUShort();
field_3_col_width = in.readShort(); field_3_col_width = in.readUShort();
field_4_xf_index = in.readShort(); field_4_xf_index = in.readUShort();
field_5_options = in.readShort(); field_5_options = in.readUShort();
switch(in.remaining()) { switch(in.remaining()) {
case 2: // usual case case 2: // usual case
field_6_reserved = in.readShort(); field_6_reserved = in.readUShort();
break; break;
case 1: case 1:
// often COLINFO gets encoded 1 byte short // often COLINFO gets encoded 1 byte short

View File

@ -1,4 +1,3 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
@ -16,13 +15,12 @@
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
/** /**
* Title: Default Column Width Record<P> * Title: Default Column Width Record (0x0055) <P>
* Description: Specifies the default width for columns that have no specific * Description: Specifies the default width for columns that have no specific
* width set.<P> * width set.<P>
* REFERENCE: PG 302 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P> * REFERENCE: PG 302 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
@ -30,12 +28,9 @@ import org.apache.poi.util.LittleEndian;
* @author Jason Height (jheight at chariot dot net dot au) * @author Jason Height (jheight at chariot dot net dot au)
* @version 2.0-pre * @version 2.0-pre
*/ */
public final class DefaultColWidthRecord extends Record {
public class DefaultColWidthRecord public final static short sid = 0x0055;
extends Record private int field_1_col_width;
{
public final static short sid = 0x55;
private short field_1_col_width;
public DefaultColWidthRecord() public DefaultColWidthRecord()
{ {
@ -61,17 +56,17 @@ public class DefaultColWidthRecord
protected void fillFields(RecordInputStream in) protected void fillFields(RecordInputStream in)
{ {
field_1_col_width = in.readShort(); field_1_col_width = in.readUShort();
} }
/** /**
* set the default column width * set the default column width
* @param height defaultwidth for columns * @param width defaultwidth for columns
*/ */
public void setColWidth(short height) public void setColWidth(int width)
{ {
field_1_col_width = height; field_1_col_width = width;
} }
/** /**
@ -79,7 +74,7 @@ public class DefaultColWidthRecord
* @return defaultwidth for columns * @return defaultwidth for columns
*/ */
public short getColWidth() public int getColWidth()
{ {
return field_1_col_width; return field_1_col_width;
} }
@ -97,9 +92,9 @@ public class DefaultColWidthRecord
public int serialize(int offset, byte [] data) public int serialize(int offset, byte [] data)
{ {
LittleEndian.putShort(data, 0 + offset, sid); LittleEndian.putUShort(data, 0 + offset, sid);
LittleEndian.putShort(data, 2 + offset, ( short ) 0x2); LittleEndian.putUShort(data, 2 + offset, 0x2);
LittleEndian.putShort(data, 4 + offset, getColWidth()); LittleEndian.putUShort(data, 4 + offset, getColWidth());
return getRecordSize(); return getRecordSize();
} }

View File

@ -299,7 +299,7 @@ public final class ColumnInfoRecordsAggregate extends RecordAggregate {
} }
public void setColumn(int targetColumnIx, Short xfIndex, Short width, public void setColumn(int targetColumnIx, Short xfIndex, Integer width,
Integer level, Boolean hidden, Boolean collapsed) { Integer level, Boolean hidden, Boolean collapsed) {
ColumnInfoRecord ci = null; ColumnInfoRecord ci = null;
int k = 0; int k = 0;
@ -389,13 +389,13 @@ public final class ColumnInfoRecordsAggregate extends RecordAggregate {
/** /**
* Sets all non null fields into the <code>ci</code> parameter. * Sets all non null fields into the <code>ci</code> parameter.
*/ */
private static void setColumnInfoFields( ColumnInfoRecord ci, Short xfStyle, Short width, private static void setColumnInfoFields(ColumnInfoRecord ci, Short xfStyle, Integer width,
Integer level, Boolean hidden, Boolean collapsed) { Integer level, Boolean hidden, Boolean collapsed) {
if (xfStyle != null) { if (xfStyle != null) {
ci.setXFIndex(xfStyle.shortValue()); ci.setXFIndex(xfStyle.shortValue());
} }
if (width != null) { if (width != null) {
ci.setColumnWidth(width.shortValue()); ci.setColumnWidth(width.intValue());
} }
if (level != null) { if (level != null) {
ci.setOutlineLevel( level.shortValue() ); ci.setOutlineLevel( level.shortValue() );

View File

@ -162,7 +162,7 @@ public class HSSFPicture
private float getColumnWidthInPixels(short column){ private float getColumnWidthInPixels(short column){
short cw = patriarch.sheet.getColumnWidth(column); int cw = patriarch.sheet.getColWidth(column);
float px = getPixelWidth(column); float px = getPixelWidth(column);
return cw/px; return cw/px;
@ -180,8 +180,8 @@ public class HSSFPicture
private float getPixelWidth(short column){ private float getPixelWidth(short column){
int def = patriarch.sheet.getDefaultColumnWidth()*256; int def = patriarch.sheet.getDefaultColWidth()*256;
short cw = patriarch.sheet.getColumnWidth(column); int cw = patriarch.sheet.getColWidth(column);
return cw == def ? PX_DEFAULT : PX_MODIFIED; return cw == def ? PX_DEFAULT : PX_MODIFIED;
} }

View File

@ -389,48 +389,81 @@ public final class HSSFSheet {
} }
/**
* @deprecated (Sep 2008) use {@link #setColumnHidden(int, boolean)}
*/
public void setColumnHidden(short columnIndex, boolean hidden) {
setColumnHidden(columnIndex & 0xFFFF, hidden);
}
/**
* @deprecated (Sep 2008) use {@link #isColumnHidden(int)}
*/
public boolean isColumnHidden(short columnIndex) {
return isColumnHidden(columnIndex & 0xFFFF);
}
/**
* @deprecated (Sep 2008) use {@link #setColWidth(int, int)}
*/
public void setColumnWidth(short columnIndex, short width) {
setColWidth(columnIndex & 0xFFFF, width & 0xFFFF);
}
/**
* @deprecated (Sep 2008) use {@link #getColWidth(int)}
*/
public short getColumnWidth(short columnIndex) {
return (short)getColWidth(columnIndex & 0xFFFF);
}
/**
* @deprecated (Sep 2008) use {@link #getDefaultColWidth()}
*/
public short getDefaultColumnWidth() {
return (short) getDefaultColWidth();
}
/**
* @deprecated (Sep 2008) use {@link #setDefaultColWidth(int)}
*/
public void setDefaultColumnWidth(short width) {
setDefaultColWidth(width & 0xFFFF);
}
/** /**
* Get the visibility state for a given column. * Get the visibility state for a given column.
* @param column - the column to get (0-based) * @param columnIndex - the column to get (0-based)
* @param hidden - the visiblity state of the column * @param hidden - the visiblity state of the column
*/ */
public void setColumnHidden(int columnIndex, boolean hidden) {
public void setColumnHidden(short column, boolean hidden) sheet.setColumnHidden(columnIndex, hidden);
{
sheet.setColumnHidden(column, hidden);
} }
/** /**
* Get the hidden state for a given column. * Get the hidden state for a given column.
* @param column - the column to set (0-based) * @param columnIndex - the column to set (0-based)
* @return hidden - the visiblity state of the column * @return hidden - <code>false</code> if the column is visible
*/ */
public boolean isColumnHidden(int columnIndex) {
public boolean isColumnHidden(short column) return sheet.isColumnHidden(columnIndex);
{
return sheet.isColumnHidden(column);
} }
/** /**
* set the width (in units of 1/256th of a character width) * set the width (in units of 1/256th of a character width)
* @param column - the column to set (0-based) * @param columnIndex - the column to set (0-based)
* @param width - the width in units of 1/256th of a character width * @param width - the width in units of 1/256th of a character width
*/ */
public void setColWidth(int columnIndex, int width) {
public void setColumnWidth(short column, short width) sheet.setColumnWidth(columnIndex, width);
{
sheet.setColumnWidth(column, width);
} }
/** /**
* get the width (in units of 1/256th of a character width ) * get the width (in units of 1/256th of a character width )
* @param column - the column to set (0-based) * @param columnIndex - the column to set (0-based)
* @return width - the width in units of 1/256th of a character width * @return width - the width in units of 1/256th of a character width
*/ */
public int getColWidth(int columnIndex) {
public short getColumnWidth(short column) return sheet.getColumnWidth(columnIndex);
{
return sheet.getColumnWidth(column);
} }
/** /**
@ -438,20 +471,25 @@ public final class HSSFSheet {
* characters * characters
* @return default column width * @return default column width
*/ */
public int getDefaultColWidth() {
public short getDefaultColumnWidth()
{
return sheet.getDefaultColumnWidth(); return sheet.getDefaultColumnWidth();
} }
/**
* set the default column width for the sheet (if the columns do not define their own width) in
* characters
* @param width default column width
*/
public void setDefaultColWidth(int width) {
sheet.setDefaultColumnWidth(width);
}
/** /**
* get the default row height for the sheet (if the rows do not define their own height) in * get the default row height for the sheet (if the rows do not define their own height) in
* twips (1/20 of a point) * twips (1/20 of a point)
* @return default row height * @return default row height
*/ */
public short getDefaultRowHeight() {
public short getDefaultRowHeight()
{
return sheet.getDefaultRowHeight(); return sheet.getDefaultRowHeight();
} }
@ -466,17 +504,6 @@ public final class HSSFSheet {
return (sheet.getDefaultRowHeight() / 20); return (sheet.getDefaultRowHeight() / 20);
} }
/**
* set the default column width for the sheet (if the columns do not define their own width) in
* characters
* @param width default column width
*/
public void setDefaultColumnWidth(short width)
{
sheet.setDefaultColumnWidth(width);
}
/** /**
* set the default row height for the sheet (if the rows do not define their own height) in * set the default row height for the sheet (if the rows do not define their own height) in
* twips (1/20 of a point) * twips (1/20 of a point)
@ -999,7 +1026,7 @@ public final class HSSFSheet {
/** /**
* Sets the protection on enabled or disabled * Sets the protection on enabled or disabled
* @param protect true => protection enabled; false => protection disabled * @param protect true => protection enabled; false => protection disabled
* @deprecated use protectSheet(String, boolean, boolean) * @deprecated (Jul 2007) use {@link #protectSheet(String)}
*/ */
public void setProtect(boolean protect) { public void setProtect(boolean protect) {
getSheet().getProtect().setProtect(protect); getSheet().getProtect().setProtect(protect);
@ -1624,7 +1651,7 @@ public final class HSSFSheet {
* @param style the style to set * @param style the style to set
*/ */
public void setDefaultColumnStyle(short column, HSSFCellStyle style) { public void setDefaultColumnStyle(short column, HSSFCellStyle style) {
sheet.setColumn(column, new Short(style.getIndex()), null, null, null, null); sheet.setDefaultColumnStyle(column, style.getIndex());
} }
/** /**

View File

@ -202,24 +202,23 @@ public final class TestBugs extends TestCase {
col_cnt = c; col_cnt = c;
rw_cnt = r; rw_cnt = r;
HSSFRow rw = null ; HSSFRow rw ;
HSSFCell cell =null;
rw = sheet.createRow(0) ; rw = sheet.createRow(0) ;
//Header row //Header row
for(int j=0; j<col_cnt; j++){ for(int j=0; j<col_cnt; j++){
cell = rw.createCell(j) ; HSSFCell cell = rw.createCell(j) ;
setCellText(cell, "Col " + (j+1)) ; setCellText(cell, "Col " + (j+1)) ;
} }
for(int i=1; i<rw_cnt; i++){ for(int i=1; i<rw_cnt; i++){
rw = sheet.createRow(i) ; rw = sheet.createRow(i) ;
for(int j=0; j<col_cnt; j++){ for(int j=0; j<col_cnt; j++){
cell = rw.createCell(j) ; HSSFCell cell = rw.createCell(j) ;
setCellText(cell, "Row:" + (i+1) + ",Column:" + (j+1)) ; setCellText(cell, "Row:" + (i+1) + ",Column:" + (j+1)) ;
} }
} }
sheet.setDefaultColumnWidth((short) 18) ; sheet.setDefaultColWidth(18) ;
writeTestOutputFileForViewing(wb, "test22568"); writeTestOutputFileForViewing(wb, "test22568");
} }

View File

@ -35,7 +35,6 @@ import org.apache.poi.hssf.record.RecordFormatException;
import org.apache.poi.hssf.util.CellRangeAddress; import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.CellRangeAddressList; import org.apache.poi.hssf.util.CellRangeAddressList;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.Region;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/** /**
@ -269,11 +268,11 @@ public final class TestDataValidation extends TestCase {
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
row.createCell(i).setCellStyle(_style_4); row.createCell(i).setCellStyle(_style_4);
if (i == 2 || i == 3 || i == 4) { if (i == 2 || i == 3 || i == 4) {
sheet.setColumnWidth((short) i, (short) 3500); sheet.setColWidth(i, 3500);
} else if (i == 5) { } else if (i == 5) {
sheet.setColumnWidth((short) i, (short) 10000); sheet.setColWidth(i, 10000);
} else { } else {
sheet.setColumnWidth((short) i, (short) 8000); sheet.setColWidth(i, 8000);
} }
} }
HSSFCell cell = row.getCell(0); HSSFCell cell = row.getCell(0);

View File

@ -434,7 +434,7 @@ public final class TestHSSFSheet extends TestCase {
// This will tell us that cloneSheet, and by extension, // This will tell us that cloneSheet, and by extension,
// the list forms of createSheet leave us with an accessible // the list forms of createSheet leave us with an accessible
// ProtectRecord. // ProtectRecord.
hssfSheet.setProtect(true); hssfSheet.protectSheet("secret");
Sheet cloned = sheet.cloneSheet(); Sheet cloned = sheet.cloneSheet();
assertNotNull(cloned.getProtect()); assertNotNull(cloned.getProtect());
assertTrue(hssfSheet.getProtect()); assertTrue(hssfSheet.getProtect());
@ -681,8 +681,8 @@ public final class TestHSSFSheet extends TestCase {
// autoSize the first column and check its size before the merged region (1,0,1,1) is set: // 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 // it has to be based on the 2nd row width
sheet.autoSizeColumn((short)0); sheet.autoSizeColumn((short)0);
assertTrue("Column autosized with only one row: wrong width", sheet.getColumnWidth((short)0) >= minWithRow1And2); assertTrue("Column autosized with only one row: wrong width", sheet.getColWidth(0) >= minWithRow1And2);
assertTrue("Column autosized with only one row: wrong width", sheet.getColumnWidth((short)0) <= maxWithRow1And2); assertTrue("Column autosized with only one row: wrong width", sheet.getColWidth(0) <= maxWithRow1And2);
//create a region over the 2nd row and auto size the first column //create a region over the 2nd row and auto size the first column
sheet.addMergedRegion(new CellRangeAddress(1,1,0,1)); sheet.addMergedRegion(new CellRangeAddress(1,1,0,1));
@ -692,16 +692,16 @@ public final class TestHSSFSheet extends TestCase {
// check that the autoSized column width has ignored the 2nd row // check that the autoSized column width has ignored the 2nd row
// because it is included in a merged region (Excel like behavior) // because it is included in a merged region (Excel like behavior)
HSSFSheet sheet2 = wb2.getSheet(sheetName); HSSFSheet sheet2 = wb2.getSheet(sheetName);
assertTrue(sheet2.getColumnWidth((short)0) >= minWithRow1Only); assertTrue(sheet2.getColWidth(0) >= minWithRow1Only);
assertTrue(sheet2.getColumnWidth((short)0) <= maxWithRow1Only); assertTrue(sheet2.getColWidth(0) <= maxWithRow1Only);
// remove the 2nd row merged region and check that the 2nd row value is used to the autoSizeColumn width // remove the 2nd row merged region and check that the 2nd row value is used to the autoSizeColumn width
sheet2.removeMergedRegion(1); sheet2.removeMergedRegion(1);
sheet2.autoSizeColumn((short)0); sheet2.autoSizeColumn((short)0);
HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2); HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
HSSFSheet sheet3 = wb3.getSheet(sheetName); HSSFSheet sheet3 = wb3.getSheet(sheetName);
assertTrue(sheet3.getColumnWidth((short)0) >= minWithRow1And2); assertTrue(sheet3.getColWidth(0) >= minWithRow1And2);
assertTrue(sheet3.getColumnWidth((short)0) <= maxWithRow1And2); assertTrue(sheet3.getColWidth(0) <= maxWithRow1And2);
} }
/** /**
@ -785,49 +785,53 @@ public final class TestHSSFSheet extends TestCase {
HSSFSheet sh = wb.getSheetAt(0); HSSFSheet sh = wb.getSheetAt(0);
for (char i = 'A'; i <= 'S'; i++) { for (char i = 'A'; i <= 'S'; i++) {
int idx = i - 'A'; int idx = i - 'A';
int w = sh.getColumnWidth((short)idx); int w = sh.getColWidth(idx);
assertEquals(ref[idx], w); assertEquals(ref[idx], w);
} }
//the second sheet doesn't have overridden column widths //the second sheet doesn't have overridden column widths
sh = wb.getSheetAt(1); sh = wb.getSheetAt(1);
int def_width = sh.getDefaultColumnWidth(); int def_width = sh.getDefaultColWidth();
for (char i = 'A'; i <= 'S'; i++) { for (char i = 'A'; i <= 'S'; i++) {
int idx = i - 'A'; int idx = i - 'A';
int w = sh.getColumnWidth((short)idx); int w = sh.getColWidth(idx);
//getDefaultColumnWidth returns width measued in characters //getDefaultColumnWidth returns width measured in characters
//getColumnWidth returns width measued in 1/256th units //getColumnWidth returns width measured in 1/256th units
assertEquals(def_width*256, w); assertEquals(def_width*256, w);
} }
//test new workbook //test new workbook
wb = new HSSFWorkbook(); wb = new HSSFWorkbook();
sh = wb.createSheet(); sh = wb.createSheet();
sh.setDefaultColumnWidth((short)10); sh.setDefaultColWidth(10);
assertEquals(10, sh.getDefaultColumnWidth()); assertEquals(10, sh.getDefaultColWidth());
assertEquals(256*10, sh.getColumnWidth((short)0)); assertEquals(256*10, sh.getColWidth(0));
assertEquals(256*10, sh.getColumnWidth((short)1)); assertEquals(256*10, sh.getColWidth(1));
assertEquals(256*10, sh.getColumnWidth((short)2)); assertEquals(256*10, sh.getColWidth(2));
for (char i = 'D'; i <= 'F'; i++) { for (char i = 'D'; i <= 'F'; i++) {
short w = (short)(256*12); short w = (256*12);
sh.setColumnWidth((short)i, w); sh.setColWidth(i, w);
assertEquals(w, sh.getColumnWidth((short)i)); assertEquals(w, sh.getColWidth(i));
} }
//serialize and read again //serialize and read again
wb = HSSFTestDataSamples.writeOutAndReadBack(wb); wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
sh = wb.getSheetAt(0); sh = wb.getSheetAt(0);
assertEquals(10, sh.getDefaultColumnWidth()); assertEquals(10, sh.getDefaultColWidth());
//columns A-C have default width //columns A-C have default width
assertEquals(256*10, sh.getColumnWidth((short)0)); assertEquals(256*10, sh.getColWidth(0));
assertEquals(256*10, sh.getColumnWidth((short)1)); assertEquals(256*10, sh.getColWidth(1));
assertEquals(256*10, sh.getColumnWidth((short)2)); assertEquals(256*10, sh.getColWidth(2));
//columns D-F have custom width //columns D-F have custom width
for (char i = 'D'; i <= 'F'; i++) { for (char i = 'D'; i <= 'F'; i++) {
short w = (short)(256*12); short w = (256*12);
assertEquals(w, sh.getColumnWidth((short)i)); assertEquals(w, sh.getColWidth(i));
} }
// check for 16-bit signed/unsigned error:
sh.setColWidth(0, 40000);
assertEquals(40000, sh.getColWidth(0));
} }
/** /**