Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@659525 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-05-23 12:58:56 +00:00
parent 9b8395dd61
commit 8ef6b8561d
5 changed files with 73 additions and 15 deletions

View File

@ -37,9 +37,10 @@
<!-- Don't forget to update status.xml too! --> <!-- Don't forget to update status.xml too! -->
<release version="3.1-final" date="2008-06-??"> <release version="3.1-final" date="2008-06-??">
<action dev="POI-DEVELOPERS" type="add">45025 - improved FormulaParser parse error messages</action> <action dev="POI-DEVELOPERS" type="add">Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level</action>
<action dev="POI-DEVELOPERS" type="add">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action> <action dev="POI-DEVELOPERS" type="fix">45025 - improved FormulaParser parse error messages</action>
<action dev="POI-DEVELOPERS" type="add">45066 - fixed sheet encoding size mismatch problems</action> <action dev="POI-DEVELOPERS" type="fix">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action>
<action dev="POI-DEVELOPERS" type="fix">45066 - fixed sheet encoding size mismatch problems</action>
<action dev="POI-DEVELOPERS" type="add">45003 - Support embeded HDGF visio documents</action> <action dev="POI-DEVELOPERS" type="add">45003 - Support embeded HDGF visio documents</action>
<action dev="POI-DEVELOPERS" type="fix">45001 - Partial fix for HWPF Range.insertBefore() and Range.delete() with unicode characters</action> <action dev="POI-DEVELOPERS" type="fix">45001 - Partial fix for HWPF Range.insertBefore() and Range.delete() with unicode characters</action>
<action dev="POI-DEVELOPERS" type="fix">44977 - Support for AM/PM in excel date formats</action> <action dev="POI-DEVELOPERS" type="fix">44977 - Support for AM/PM in excel date formats</action>

View File

@ -34,9 +34,10 @@
<!-- Don't forget to update changes.xml too! --> <!-- Don't forget to update changes.xml too! -->
<changes> <changes>
<release version="3.1-final" date="2008-06-??"> <release version="3.1-final" date="2008-06-??">
<action dev="POI-DEVELOPERS" type="add">45025 - improved FormulaParser parse error messages</action> <action dev="POI-DEVELOPERS" type="add">Extend the support for specifying a policy to HSSF on missing / blank cells when fetching, to be able to specify the policy at the HSSFWorkbook level</action>
<action dev="POI-DEVELOPERS" type="add">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action> <action dev="POI-DEVELOPERS" type="fix">45025 - improved FormulaParser parse error messages</action>
<action dev="POI-DEVELOPERS" type="add">45066 - fixed sheet encoding size mismatch problems</action> <action dev="POI-DEVELOPERS" type="fix">45046 - allowed EXTERNALBOOK(0x01AE) to be optional in the LinkTable</action>
<action dev="POI-DEVELOPERS" type="fix">45066 - fixed sheet encoding size mismatch problems</action>
<action dev="POI-DEVELOPERS" type="add">45003 - Support embeded HDGF visio documents</action> <action dev="POI-DEVELOPERS" type="add">45003 - Support embeded HDGF visio documents</action>
<action dev="POI-DEVELOPERS" type="fix">45001 - Partial fix for HWPF Range.insertBefore() and Range.delete() with unicode characters</action> <action dev="POI-DEVELOPERS" type="fix">45001 - Partial fix for HWPF Range.insertBefore() and Range.delete() with unicode characters</action>
<action dev="POI-DEVELOPERS" type="fix">44977 - Support for AM/PM in excel date formats</action> <action dev="POI-DEVELOPERS" type="fix">44977 - Support for AM/PM in excel date formats</action>

View File

@ -276,8 +276,23 @@ public final class HSSFRow implements Comparable {
/** /**
* Get the hssfcell representing a given column (logical cell) * Get the hssfcell representing a given column (logical cell)
* 0-based. If you ask for a cell that is not defined.... * 0-based. If you ask for a cell that is not defined, then
* you get a null. * you get a null.
* This is the basic call, with no policies applied
*
* @param cellnum 0 based column number
* @return HSSFCell representing that column or null if undefined.
*/
private HSSFCell retrieveCell(int cellnum) {
if(cellnum<0||cellnum>=cells.length) return null;
return cells[cellnum];
}
/**
* Get the hssfcell representing a given column (logical cell)
* 0-based. If you ask for a cell that is not defined then
* you get a null, unless you have set a different
* {@link MissingCellPolicy} on the base workbook.
* Short method signature provided to retain binary * Short method signature provided to retain binary
* compatibility. * compatibility.
* *
@ -288,17 +303,18 @@ public final class HSSFRow implements Comparable {
int ushortCellNum = cellnum & 0x0000FFFF; // avoid sign extension int ushortCellNum = cellnum & 0x0000FFFF; // avoid sign extension
return getCell(ushortCellNum); return getCell(ushortCellNum);
} }
/** /**
* Get the hssfcell representing a given column (logical cell) * Get the hssfcell representing a given column (logical cell)
* 0-based. If you ask for a cell that is not defined.... * 0-based. If you ask for a cell that is not defined then
* you get a null. * you get a null, unless you have set a different
* {@link MissingCellPolicy} on the base workbook.
* *
* @param cellnum 0 based column number * @param cellnum 0 based column number
* @return HSSFCell representing that column or null if undefined. * @return HSSFCell representing that column or null if undefined.
*/ */
public HSSFCell getCell(int cellnum) { public HSSFCell getCell(int cellnum) {
if(cellnum<0||cellnum>=cells.length) return null; return getCell(cellnum, book.getMissingCellPolicy());
return cells[cellnum];
} }
/** /**
@ -311,7 +327,7 @@ public final class HSSFRow implements Comparable {
* @return representing that column or null if undefined + policy allows. * @return representing that column or null if undefined + policy allows.
*/ */
public HSSFCell getCell(int cellnum, MissingCellPolicy policy) { public HSSFCell getCell(int cellnum, MissingCellPolicy policy) {
HSSFCell cell = getCell(cellnum); HSSFCell cell = retrieveCell(cellnum);
if(policy == RETURN_NULL_AND_BLANK) { if(policy == RETURN_NULL_AND_BLANK) {
return cell; return cell;
} }
@ -335,7 +351,6 @@ public final class HSSFRow implements Comparable {
* get the number of the first cell contained in this row. * get the number of the first cell contained in this row.
* @return short representing the first logical cell in the row, or -1 if the row does not contain any cells. * @return short representing the first logical cell in the row, or -1 if the row does not contain any cells.
*/ */
public short getFirstCellNum() public short getFirstCellNum()
{ {
if (getPhysicalNumberOfCells() == 0) if (getPhysicalNumberOfCells() == 0)

View File

@ -29,6 +29,7 @@ import org.apache.poi.hssf.record.*;
import org.apache.poi.hssf.record.formula.Area3DPtg; import org.apache.poi.hssf.record.formula.Area3DPtg;
import org.apache.poi.hssf.record.formula.MemFuncPtg; import org.apache.poi.hssf.record.formula.MemFuncPtg;
import org.apache.poi.hssf.record.formula.UnionPtg; import org.apache.poi.hssf.record.formula.UnionPtg;
import org.apache.poi.hssf.usermodel.HSSFRow.MissingCellPolicy;
import org.apache.poi.hssf.util.CellReference; import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.hssf.util.SheetReferences; import org.apache.poi.hssf.util.SheetReferences;
import org.apache.poi.poifs.filesystem.*; import org.apache.poi.poifs.filesystem.*;
@ -103,6 +104,13 @@ public class HSSFWorkbook extends POIDocument
*/ */
private HSSFDataFormat formatter; private HSSFDataFormat formatter;
/**
* The policy to apply in the event of missing or
* blank cells when fetching from a row.
* See {@link MissingCellPolicy}
*/
private MissingCellPolicy missingCellPolicy = HSSFRow.RETURN_NULL_AND_BLANK;
/** Extended windows meta file */ /** Extended windows meta file */
public static final int PICTURE_TYPE_EMF = 2; public static final int PICTURE_TYPE_EMF = 2;
@ -345,8 +353,28 @@ public class HSSFWorkbook extends POIDocument
log.log(POILogger.DEBUG, "convertLabelRecords exit"); log.log(POILogger.DEBUG, "convertLabelRecords exit");
} }
/**
* Retrieves the current policy on what to do when
* getting missing or blank cells from a row.
* The default is to return blank and null cells.
* {@link MissingCellPolicy}
*/
public MissingCellPolicy getMissingCellPolicy() {
return missingCellPolicy;
}
/** /**
* Sets the policy on what to do when
* getting missing or blank cells from a row.
* This will then apply to all calls to
* {@link HSSFRow.getCell()}. See
* {@link MissingCellPolicy}
*/
public void setMissingCellPolicy(MissingCellPolicy missingCellPolicy) {
this.missingCellPolicy = missingCellPolicy;
}
/**
* sets the order of appearance for a given sheet. * sets the order of appearance for a given sheet.
* *
* @param sheetname the name of the sheet to reorder * @param sheetname the name of the sheet to reorder

View File

@ -20,6 +20,7 @@ package org.apache.poi.hssf.usermodel;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFRow.MissingCellPolicy;
/** /**
* Test HSSFRow is okay. * Test HSSFRow is okay.
@ -224,7 +225,7 @@ public final class TestHSSFRow extends TestCase {
row.createCell((short)4, HSSFCell.CELL_TYPE_BLANK); row.createCell((short)4, HSSFCell.CELL_TYPE_BLANK);
row.createCell((short)5).setCellValue(4); row.createCell((short)5).setCellValue(4);
// First up, no policy // First up, no policy given, uses default
assertEquals(HSSFCell.CELL_TYPE_STRING, row.getCell(0).getCellType()); assertEquals(HSSFCell.CELL_TYPE_STRING, row.getCell(0).getCellType());
assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType()); assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
assertEquals(null, row.getCell(2)); assertEquals(null, row.getCell(2));
@ -263,5 +264,17 @@ public final class TestHSSFRow extends TestCase {
assertEquals((short)3, row.getCell(3, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum()); assertEquals((short)3, row.getCell(3, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
assertEquals((short)4, row.getCell(4, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum()); assertEquals((short)4, row.getCell(4, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
assertEquals((short)5, row.getCell(5, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum()); assertEquals((short)5, row.getCell(5, HSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
// Now change the cell policy on the workbook, check
// that that is now used if no policy given
book.setMissingCellPolicy(HSSFRow.RETURN_BLANK_AS_NULL);
assertEquals(HSSFCell.CELL_TYPE_STRING, row.getCell(0).getCellType());
assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
assertEquals(null, row.getCell(2));
assertEquals(null, row.getCell(3));
assertEquals(null, row.getCell(4));
assertEquals(HSSFCell.CELL_TYPE_NUMERIC, row.getCell(5).getCellType());
} }
} }