Improve how XSSFCell does error stuff

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@642769 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-03-30 16:25:52 +00:00
parent b6316bd72e
commit 7cf696f7c4
6 changed files with 93 additions and 39 deletions

View File

@ -544,6 +544,7 @@ under the License.
<classpath> <classpath>
<path refid="ooxml.classpath"/> <path refid="ooxml.classpath"/>
<pathelement path="${ooxml.output.dir}"/> <pathelement path="${ooxml.output.dir}"/>
<pathelement path="${main.output.test.dir}"/>
<pathelement location="${junit.jar1.dir}"/> <pathelement location="${junit.jar1.dir}"/>
</classpath> </classpath>
</javac> </javac>

View File

@ -46,14 +46,6 @@ public class BoolErrRecord
private byte field_4_bBoolErr; private byte field_4_bBoolErr;
private byte field_5_fError; private byte field_5_fError;
public static final byte NULL = 0;
public static final byte DIV0 = 7;
public static final byte VALUE = 15;
public static final byte REF = 23;
public static final byte NAME = 29;
public static final byte NUM = 36;
public static final byte NA = 42;
/** Creates new BoolErrRecord */ /** Creates new BoolErrRecord */
public BoolErrRecord() public BoolErrRecord()
{ {

View File

@ -71,6 +71,26 @@ public interface Cell {
public final static int CELL_TYPE_ERROR = 5; public final static int CELL_TYPE_ERROR = 5;
public final static class CELL_ERROR_TYPE {
private final byte type;
private final String repr;
private CELL_ERROR_TYPE(int type, String repr) {
this.type = (byte)type;
this.repr = repr;
}
public byte getType() { return type; }
public String getStringRepr() { return repr; }
}
public static final CELL_ERROR_TYPE ERROR_NULL = new CELL_ERROR_TYPE(0, "#NULL!");
public static final CELL_ERROR_TYPE ERROR_DIV0 = new CELL_ERROR_TYPE(7, "#DIV/0!");
public static final CELL_ERROR_TYPE ERROR_VALUE = new CELL_ERROR_TYPE(15, "#VALUE!");
public static final CELL_ERROR_TYPE ERROR_REF = new CELL_ERROR_TYPE(23, "#REF!");
public static final CELL_ERROR_TYPE ERROR_NAME = new CELL_ERROR_TYPE(29, "#NAME?");
public static final CELL_ERROR_TYPE ERROR_NUM = new CELL_ERROR_TYPE(36, "#NUM!");
public static final CELL_ERROR_TYPE ERROR_NA = new CELL_ERROR_TYPE(42, "#N/A");
/** /**
* set the cell's number within the row (0 based) * set the cell's number within the row (0 based)
* @param num short the cell number * @param num short the cell number

View File

@ -20,7 +20,6 @@ package org.apache.poi.xssf.usermodel;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
@ -28,6 +27,8 @@ import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.SharedStringSource; import org.apache.poi.ss.usermodel.SharedStringSource;
import org.apache.poi.ss.usermodel.StylesSource; import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.xssf.util.CellReference; import org.apache.poi.xssf.util.CellReference;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula;
@ -44,6 +45,8 @@ public class XSSFCell implements Cell {
private SharedStringSource sharedStringSource; private SharedStringSource sharedStringSource;
private StylesSource stylesSource; private StylesSource stylesSource;
private POILogger logger = POILogFactory.getLogger(XSSFCell.class);
/** /**
* Create a new XSSFCell. This method is protected to be used only by * Create a new XSSFCell. This method is protected to be used only by
* tests. * tests.
@ -154,7 +157,7 @@ public class XSSFCell implements Cell {
} }
/** /**
* Returns the error type, in the same way that * Returns the error type, in the same way that
* HSSFCell does. See {@link BoolErrRecord} for details * HSSFCell does. See {@link Cell} for details
*/ */
public byte getErrorCellValue() { public byte getErrorCellValue() {
if (STCellType.E != cell.getT()) { if (STCellType.E != cell.getT()) {
@ -162,25 +165,25 @@ public class XSSFCell implements Cell {
} }
if (this.cell.isSetV()) { if (this.cell.isSetV()) {
String errS = this.cell.getV(); String errS = this.cell.getV();
if(errS.equals("#NULL!")) { if(errS.equals(Cell.ERROR_NULL.getStringRepr())) {
return BoolErrRecord.NULL; return Cell.ERROR_NULL.getType();
} }
if(errS.equals("#DIV/0!")) { if(errS.equals(Cell.ERROR_DIV0.getStringRepr())) {
return BoolErrRecord.DIV0; return Cell.ERROR_DIV0.getType();
} }
if(errS.equals("#VALUE!")) { if(errS.equals(Cell.ERROR_VALUE.getStringRepr())) {
return BoolErrRecord.VALUE; return Cell.ERROR_VALUE.getType();
} }
if(errS.equals("#REF!")) { if(errS.equals(Cell.ERROR_REF.getStringRepr())) {
return BoolErrRecord.REF; return Cell.ERROR_REF.getType();
} }
if(errS.equals("#NAME?")) { if(errS.equals(Cell.ERROR_NAME.getStringRepr())) {
return BoolErrRecord.NAME; return Cell.ERROR_NAME.getType();
} }
if(errS.equals("#NUM!")) { if(errS.equals(Cell.ERROR_NUM.getStringRepr())) {
return BoolErrRecord.NUM; return Cell.ERROR_NUM.getType();
} }
return BoolErrRecord.NA; return Cell.ERROR_NA.getType();
} }
return 0; return 0;
} }
@ -224,15 +227,34 @@ public class XSSFCell implements Cell {
} }
public void setCellErrorValue(byte value) { public void setCellErrorValue(byte value) {
if(value == Cell.ERROR_DIV0.getType()) {
setCellErrorValue(Cell.ERROR_DIV0);
} else if(value == Cell.ERROR_NA.getType()) {
setCellErrorValue(Cell.ERROR_NA);
} else if(value == Cell.ERROR_NAME.getType()) {
setCellErrorValue(Cell.ERROR_NAME);
} else if(value == Cell.ERROR_NULL.getType()) {
setCellErrorValue(Cell.ERROR_NULL);
} else if(value == Cell.ERROR_NUM.getType()) {
setCellErrorValue(Cell.ERROR_NUM);
} else if(value == Cell.ERROR_REF.getType()) {
setCellErrorValue(Cell.ERROR_REF);
} else if(value == Cell.ERROR_VALUE.getType()) {
setCellErrorValue(Cell.ERROR_VALUE);
} else {
logger.log(POILogger.WARN, "Unknown error type " + value + " specified, treating as #N/A");
setCellErrorValue(Cell.ERROR_NA);
}
}
public void setCellErrorValue(CELL_ERROR_TYPE errorType) {
if ((this.cell.getT() != STCellType.E) && (this.cell.getT() != STCellType.STR)) if ((this.cell.getT() != STCellType.E) && (this.cell.getT() != STCellType.STR))
{ {
this.cell.setT(STCellType.E); this.cell.setT(STCellType.E);
} }
this.cell.setV(String.valueOf(value)); this.cell.setV( errorType.getStringRepr() );
} }
public void setCellFormula(String formula) { public void setCellFormula(String formula) {
if (this.cell.getT() != STCellType.STR) if (this.cell.getT() != STCellType.STR)
{ {

View File

@ -120,12 +120,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
public String getDefaultFileName() { return DEFAULT_NAME; } public String getDefaultFileName() { return DEFAULT_NAME; }
/** /**
* Load, off the specified core part * Fetches the InputStream to read the contents, based
* of the specified core part
*/ */
private XSSFModel load(PackagePart corePart) throws Exception { public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
Constructor<? extends XSSFModel> c = CLASS.getConstructor(InputStream.class);
XSSFModel model = null;
PackageRelationshipCollection prc = PackageRelationshipCollection prc =
corePart.getRelationshipsByType(REL); corePart.getRelationshipsByType(REL);
Iterator<PackageRelationship> it = prc.iterator(); Iterator<PackageRelationship> it = prc.iterator();
@ -133,17 +131,30 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
PackageRelationship rel = it.next(); PackageRelationship rel = it.next();
PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI()); PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
PackagePart part = corePart.getPackage().getPart(relName); PackagePart part = corePart.getPackage().getPart(relName);
InputStream is = part.getInputStream(); return part.getInputStream();
try {
model = c.newInstance(is);
} finally {
is.close();
}
} else { } else {
log.log(POILogger.WARN, "No part " + DEFAULT_NAME + " found"); log.log(POILogger.WARN, "No part " + DEFAULT_NAME + " found");
return null;
}
}
/**
* Load, off the specified core part
*/
public XSSFModel load(PackagePart corePart) throws Exception {
Constructor<? extends XSSFModel> c = CLASS.getConstructor(InputStream.class);
XSSFModel model = null;
InputStream inp = getContents(corePart);
if(inp != null) {
try {
model = c.newInstance(inp);
} finally {
inp.close();
}
} }
return model; return model;
} }
/** /**
* Save, with the default name * Save, with the default name
* @return The internal reference ID it was saved at, normally then used as an r:id * @return The internal reference ID it was saved at, normally then used as an r:id

View File

@ -109,10 +109,18 @@ public class TestXSSFCell extends TestCase {
public void testSetGetError() throws Exception { public void testSetGetError() throws Exception {
XSSFRow row = createParentObjects(); XSSFRow row = createParentObjects();
XSSFCell cell = new XSSFCell(row); XSSFCell cell = new XSSFCell(row);
cell.setCellErrorValue((byte)255);
assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType());
assertEquals((byte)255, cell.getErrorCellValue()); cell.setCellErrorValue((byte)0);
assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType());
assertEquals((byte)0, cell.getErrorCellValue());
cell.setCellValue(2.2);
assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
cell.setCellErrorValue(Cell.ERROR_NAME);
assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType());
assertEquals(Cell.ERROR_NAME.getType(), cell.getErrorCellValue());
assertEquals(Cell.ERROR_NAME.getStringRepr(), cell.getErrorCellString());
} }
public void testSetGetFormula() throws Exception { public void testSetGetFormula() throws Exception {