diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 58738599d..da2b6b842 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51504 - avoid NPE when DefaultRowHeight or DefaultColumnWidth records are missing 51502 - Correct Subtotal function javadoc entry Support for hyperlinks in SXSSF 49933 - Word 6/95 documents with sections cause ArrayIndexOutOfBoundsException diff --git a/src/java/org/apache/poi/hssf/model/InternalSheet.java b/src/java/org/apache/poi/hssf/model/InternalSheet.java index 0ad87845d..a6a427262 100644 --- a/src/java/org/apache/poi/hssf/model/InternalSheet.java +++ b/src/java/org/apache/poi/hssf/model/InternalSheet.java @@ -65,12 +65,12 @@ import org.apache.poi.hssf.record.aggregates.DataValidityTable; import org.apache.poi.hssf.record.aggregates.MergedCellsTable; import org.apache.poi.hssf.record.aggregates.PageSettingsBlock; import org.apache.poi.hssf.record.aggregates.RecordAggregate; -import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate; -import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock; import org.apache.poi.hssf.record.aggregates.RecordAggregate.PositionTrackingVisitor; import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; -import org.apache.poi.ss.formula.FormulaShifter; +import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate; +import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock; import org.apache.poi.hssf.util.PaneInformation; +import org.apache.poi.ss.formula.FormulaShifter; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.util.Internal; import org.apache.poi.util.POILogFactory; @@ -110,8 +110,8 @@ public final class InternalSheet { protected PrintGridlinesRecord printGridlines = null; protected GridsetRecord gridset = null; private GutsRecord _gutsRecord; - protected DefaultColWidthRecord defaultcolwidth = null; - protected DefaultRowHeightRecord defaultrowheight = null; + protected DefaultColWidthRecord defaultcolwidth = new DefaultColWidthRecord(); + protected DefaultRowHeightRecord defaultrowheight = new DefaultRowHeightRecord(); private PageSettingsBlock _psBlock; /** @@ -272,7 +272,7 @@ public final class InternalSheet { records.add(rec); continue; } - + if (recSid == EOFRecord.sid) { records.add(rec); break; @@ -723,7 +723,7 @@ public final class InternalSheet { public void removeRow(RowRecord row) { _rowsAggregate.removeRow(row); } - + /** * Get all the value records (from LOC). Records will be returned from the first * record (starting at LOC) which is a value record. @@ -753,7 +753,7 @@ public final class InternalSheet { * subsequent calls will return values in (physical) sequence or NULL when you get to the end. * * @return Array of CellValueRecordInterface representing the remaining value records - * @deprecated use {@link #getValueIterator()} instead + * @deprecated use {@link #getCellValueIterator()} instead */ @Deprecated public CellValueRecordInterface[] getValueRecords() { @@ -934,7 +934,7 @@ public final class InternalSheet { DefaultRowHeightRecord retval = new DefaultRowHeightRecord(); retval.setOptionFlags(( short ) 0); - retval.setRowHeight(( short ) 0xff); + retval.setRowHeight(DefaultRowHeightRecord.DEFAULT_ROW_HEIGHT); return retval; } @@ -955,7 +955,7 @@ public final class InternalSheet { */ private static DefaultColWidthRecord createDefaultColWidth() { DefaultColWidthRecord retval = new DefaultColWidthRecord(); - retval.setColWidth(( short ) 8); + retval.setColWidth(DefaultColWidthRecord.DEFAULT_COLUMN_WIDTH); return retval; } diff --git a/src/java/org/apache/poi/hssf/record/DefaultColWidthRecord.java b/src/java/org/apache/poi/hssf/record/DefaultColWidthRecord.java index e8fe98cc8..6e3160ff6 100644 --- a/src/java/org/apache/poi/hssf/record/DefaultColWidthRecord.java +++ b/src/java/org/apache/poi/hssf/record/DefaultColWidthRecord.java @@ -32,8 +32,14 @@ public final class DefaultColWidthRecord extends StandardRecord { public final static short sid = 0x0055; private int field_1_col_width; + /** + * The default column width is 8 characters + */ + public final static int DEFAULT_COLUMN_WIDTH = 0x0008; + public DefaultColWidthRecord() { + field_1_col_width = DEFAULT_COLUMN_WIDTH; } public DefaultColWidthRecord(RecordInputStream in) diff --git a/src/java/org/apache/poi/hssf/record/DefaultRowHeightRecord.java b/src/java/org/apache/poi/hssf/record/DefaultRowHeightRecord.java index eacebacfc..0d015e09a 100644 --- a/src/java/org/apache/poi/hssf/record/DefaultRowHeightRecord.java +++ b/src/java/org/apache/poi/hssf/record/DefaultRowHeightRecord.java @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - + package org.apache.poi.hssf.record; @@ -38,8 +38,15 @@ public final class DefaultRowHeightRecord private short field_1_option_flags; private short field_2_row_height; + /** + * The default row height for empty rows is 255 twips (255 / 20 == 12.75 points) + */ + public static final short DEFAULT_ROW_HEIGHT = 0xFF; + public DefaultRowHeightRecord() { + field_1_option_flags = 0x0000; + field_2_row_height = DEFAULT_ROW_HEIGHT; } public DefaultRowHeightRecord(RecordInputStream in) diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 1ce0b1d22..85a4cb3b0 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -736,6 +736,28 @@ public final class TestHSSFSheet extends BaseTestSheet { assertEquals(40000, sh.getColumnWidth(0)); } + + public void testDefaultColumnWidth() { + HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook( "12843-1.xls" ); + HSSFSheet sheet = wb.getSheetAt( 7 ); + // shall not be NPE + assertEquals(8, sheet.getDefaultColumnWidth()); + assertEquals(8*256, sheet.getColumnWidth(0)); + + assertEquals(0xFF, sheet.getDefaultRowHeight()); + + wb = HSSFTestDataSamples.openSampleWorkbook( "34775.xls" ); + // second and third sheets miss DefaultColWidthRecord + for(int i = 1; i <= 2; i++){ + int dw = wb.getSheetAt( i ).getDefaultColumnWidth(); + assertEquals(8, dw); + int cw = wb.getSheetAt( i ).getColumnWidth(0); + assertEquals(8*256, cw); + + assertEquals(0xFF, sheet.getDefaultRowHeight()); + } + } + /** * Some utilities write Excel files without the ROW records. * Excel, ooo, and google docs are OK with this.