1. fixed HSSFCell.setCellFormula to call HSSFFormulaParser.parse before any cell modifications, if it fails the cell must be left in the state prior to the invocation. 2. added @throws javadoc to HSSFRow.createCell and XSSFRow.createCell, see bug #10393 3. fixed incorrect condition type in CFRuleRecord if the rule is a formula

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@726049 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-12-12 15:36:05 +00:00
parent 015af7c141
commit 13860fc475
6 changed files with 296 additions and 266 deletions

View File

@ -125,8 +125,6 @@ public final class CFRuleRecord extends StandardRecord {
private CFRuleRecord(byte conditionType, byte comparisonOperation, Ptg[] formula1, Ptg[] formula2) { private CFRuleRecord(byte conditionType, byte comparisonOperation, Ptg[] formula1, Ptg[] formula2) {
this(conditionType, comparisonOperation); this(conditionType, comparisonOperation);
field_1_condition_type = CONDITION_TYPE_CELL_VALUE_IS;
field_2_comparison_operator = comparisonOperation;
field_17_formula1 = Formula.create(formula1); field_17_formula1 = Formula.create(formula1);
field_18_formula2 = Formula.create(formula2); field_18_formula2 = Formula.create(formula2);
} }
@ -485,6 +483,7 @@ public final class CFRuleRecord extends StandardRecord {
public String toString() { public String toString() {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer();
buffer.append("[CFRULE]\n"); buffer.append("[CFRULE]\n");
buffer.append(" .condition_type ="+field_1_condition_type);
buffer.append(" OPTION FLAGS=0x"+Integer.toHexString(getOptions())); buffer.append(" OPTION FLAGS=0x"+Integer.toHexString(getOptions()));
if (false) { if (false) {
if (containsFontFormattingBlock()) { if (containsFontFormattingBlock()) {

View File

@ -578,6 +578,7 @@ public class HSSFCell implements Cell {
setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex); setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
return; return;
} }
Ptg[] ptgs = HSSFFormulaParser.parse(formula, book);
setCellType(CELL_TYPE_FORMULA, false, row, col, styleIndex); setCellType(CELL_TYPE_FORMULA, false, row, col, styleIndex);
FormulaRecordAggregate agg = (FormulaRecordAggregate) record; FormulaRecordAggregate agg = (FormulaRecordAggregate) record;
FormulaRecord frec = agg.getFormulaRecord(); FormulaRecord frec = agg.getFormulaRecord();
@ -588,7 +589,6 @@ public class HSSFCell implements Cell {
if (agg.getXFIndex() == (short)0) { if (agg.getXFIndex() == (short)0) {
agg.setXFIndex((short) 0x0f); agg.setXFIndex((short) 0x0f);
} }
Ptg[] ptgs = HSSFFormulaParser.parse(formula, book);
agg.setParsedExpression(ptgs); agg.setParsedExpression(ptgs);
} }
/** /**
@ -896,11 +896,11 @@ public class HSSFCell implements Cell {
*/ */
private void checkBounds(int cellNum) { private void checkBounds(int cellNum) {
if (cellNum > 255) { if (cellNum > 255) {
throw new RuntimeException("You cannot have more than 255 columns "+ throw new IllegalArgumentException("You cannot have more than 255 columns "+
"in a given row (IV). Because Excel can't handle it"); "in a given row (IV). Because Excel can't handle it");
} }
else if (cellNum < 0) { else if (cellNum < 0) {
throw new RuntimeException("You cannot reference columns with an index of less then 0."); throw new IllegalArgumentException("You cannot reference columns with an index of less then 0.");
} }
} }

View File

@ -114,6 +114,8 @@ public final class HSSFRow implements Comparable, Row {
* @param column - the column number this cell represents * @param column - the column number this cell represents
* *
* @return HSSFCell a high level representation of the created cell. * @return HSSFCell a high level representation of the created cell.
* @throws IllegalArgumentException if columnIndex < 0 or greater than 255,
* the maximum number of columns supported by the Excel binary format (.xls)
*/ */
public HSSFCell createCell(int column) public HSSFCell createCell(int column)
{ {
@ -129,6 +131,8 @@ public final class HSSFRow implements Comparable, Row {
* @param columnIndex - the column number this cell represents * @param columnIndex - the column number this cell represents
* *
* @return HSSFCell a high level representation of the created cell. * @return HSSFCell a high level representation of the created cell.
* @throws IllegalArgumentException if columnIndex < 0 or greater than 255,
* the maximum number of columns supported by the Excel binary format (.xls)
*/ */
public HSSFCell createCell(int columnIndex, int type) public HSSFCell createCell(int columnIndex, int type)
{ {

View File

@ -33,7 +33,8 @@ public interface Row extends Iterable<Cell> {
* *
* @param column - the column number this cell represents * @param column - the column number this cell represents
* @return Cell a high level representation of the created cell. * @return Cell a high level representation of the created cell.
* @throws IllegalArgumentException if columnIndex < 0 * @throws IllegalArgumentException if columnIndex < 0 or greater than the maximum number of supported columns
* (255 for *.xls, 1048576 for *.xlsx)
*/ */
Cell createCell(int column); Cell createCell(int column);
@ -45,7 +46,8 @@ public interface Row extends Iterable<Cell> {
* *
* @param column - the column number this cell represents * @param column - the column number this cell represents
* @return Cell a high level representation of the created cell. * @return Cell a high level representation of the created cell.
* @throws IllegalArgumentException if columnIndex < 0 * @throws IllegalArgumentException if columnIndex < 0 or greate than a maximum number of supported columns
* (255 for *.xls, 1048576 for *.xlsx)
*/ */
Cell createCell(int column, int type); Cell createCell(int column, int type);

View File

@ -134,7 +134,8 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
* </p> * </p>
* @param columnIndex - the column number this cell represents * @param columnIndex - the column number this cell represents
* @return Cell a high level representation of the created cell. * @return Cell a high level representation of the created cell.
* @throws IllegalArgumentException if columnIndex < 0 * @throws IllegalArgumentException if columnIndex < 0 or greater than 16384,
* the maximum number of columns supported by the SpreadsheetML format (.xlsx)
*/ */
public XSSFCell createCell(int columnIndex) { public XSSFCell createCell(int columnIndex) {
return createCell(columnIndex, Cell.CELL_TYPE_BLANK); return createCell(columnIndex, Cell.CELL_TYPE_BLANK);
@ -146,7 +147,8 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
* @param columnIndex - the column number this cell represents * @param columnIndex - the column number this cell represents
* @param type - the cell's data type * @param type - the cell's data type
* @return XSSFCell a high level representation of the created cell. * @return XSSFCell a high level representation of the created cell.
* @throws IllegalArgumentException if columnIndex < 0 or if the specified cell type is invalid * @throws IllegalArgumentException if the specified cell type is invalid, columnIndex < 0
* or greater than 16384, the maximum number of columns supported by the SpreadsheetML format (.xlsx)
* @see Cell#CELL_TYPE_BLANK * @see Cell#CELL_TYPE_BLANK
* @see Cell#CELL_TYPE_BOOLEAN * @see Cell#CELL_TYPE_BOOLEAN
* @see Cell#CELL_TYPE_ERROR * @see Cell#CELL_TYPE_ERROR

View File

@ -30,6 +30,7 @@ import org.apache.poi.hssf.record.formula.RefPtg;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.ss.formula.Formula;
/** /**
* Tests the serialization and deserialization of the TestCFRuleRecord * Tests the serialization and deserialization of the TestCFRuleRecord
@ -39,6 +40,28 @@ import org.apache.poi.util.LittleEndian;
*/ */
public final class TestCFRuleRecord extends TestCase public final class TestCFRuleRecord extends TestCase
{ {
public void testConstructors ()
{
HSSFWorkbook workbook = new HSSFWorkbook();
CFRuleRecord rule1 = CFRuleRecord.create(workbook, "7");
assertEquals(CFRuleRecord.CONDITION_TYPE_FORMULA, rule1.getConditionType());
assertEquals(ComparisonOperator.NO_COMPARISON, rule1.getComparisonOperation());
assertNotNull(rule1.getParsedExpression1());
assertSame(Ptg.EMPTY_PTG_ARRAY, rule1.getParsedExpression2());
CFRuleRecord rule2 = CFRuleRecord.create(workbook, ComparisonOperator.BETWEEN, "2", "5");
assertEquals(CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS, rule2.getConditionType());
assertEquals(ComparisonOperator.BETWEEN, rule2.getComparisonOperation());
assertNotNull(rule2.getParsedExpression1());
assertNotNull(rule2.getParsedExpression2());
CFRuleRecord rule3 = CFRuleRecord.create(workbook, ComparisonOperator.EQUAL, null, null);
assertEquals(CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS, rule3.getConditionType());
assertEquals(ComparisonOperator.EQUAL, rule3.getComparisonOperation());
assertSame(Ptg.EMPTY_PTG_ARRAY, rule3.getParsedExpression2());
assertSame(Ptg.EMPTY_PTG_ARRAY, rule3.getParsedExpression2());
}
public void testCreateCFRuleRecord () public void testCreateCFRuleRecord ()
{ {