use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class TestFormulaRecordAggregate method testArrayFormulas.
public void testArrayFormulas() {
int rownum = 4;
int colnum = 4;
FormulaRecord fr = new FormulaRecord();
fr.setRow(rownum);
fr.setColumn((short) colnum);
FormulaRecordAggregate agg = new FormulaRecordAggregate(fr, null, SharedValueManager.createEmpty());
Ptg[] ptgsForCell = { new ExpPtg(rownum, colnum) };
agg.setParsedExpression(ptgsForCell);
String formula = "SUM(A1:A3*B1:B3)";
Ptg[] ptgs = HSSFFormulaParser.parse(formula, null, FormulaType.ARRAY, 0);
agg.setArrayFormula(new CellRangeAddress(rownum, rownum, colnum, colnum), ptgs);
assertTrue(agg.isPartOfArrayFormula());
assertEquals("E5", agg.getArrayFormulaRange().formatAsString());
Ptg[] ptg = agg.getFormulaTokens();
String fmlaSer = FormulaRenderer.toFormulaString(null, ptg);
assertEquals(formula, fmlaSer);
agg.removeArrayFormula(rownum, colnum);
assertFalse(agg.isPartOfArrayFormula());
}
use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class TestFormulaRecordAggregate method testExtraStringRecord_bug46213.
/**
* Sometimes a {@link StringRecord} appears after a {@link FormulaRecord} even though the
* formula has evaluated to a text value. This might be more likely to occur when the formula
* <i>can</i> evaluate to a text value.<br/>
* Bug 46213 attachment 22874 has such an extra {@link StringRecord} at stream offset 0x5765.
* This file seems to open in Excel (2007) with no trouble. When it is re-saved, Excel omits
* the extra record. POI should do the same.
*/
public void testExtraStringRecord_bug46213() {
FormulaRecord fr = new FormulaRecord();
fr.setValue(2.0);
StringRecord sr = new StringRecord();
sr.setString("NA");
SharedValueManager svm = SharedValueManager.createEmpty();
FormulaRecordAggregate fra;
try {
fra = new FormulaRecordAggregate(fr, sr, svm);
} catch (RecordFormatException e) {
if ("String record was supplied but formula record flag is not set".equals(e.getMessage())) {
throw new AssertionFailedError("Identified bug 46213");
}
throw e;
}
RecordCollector rc = new RecordCollector();
fra.visitContainedRecords(rc);
Record[] vraRecs = rc.getRecords();
assertEquals(1, vraRecs.length);
assertEquals(fr, vraRecs[0]);
}
use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class HSSFCell method getBooleanCellValue.
/**
* get the value of the cell as a boolean. For strings, numbers, and errors, we throw an exception.
* For blank cells we return a false.
*/
@Override
public boolean getBooleanCellValue() {
switch(_cellType) {
case BLANK:
return false;
case BOOLEAN:
return ((BoolErrRecord) _record).getBooleanValue();
case FORMULA:
break;
default:
throw typeMismatch(CellType.BOOLEAN, _cellType, false);
}
FormulaRecord fr = ((FormulaRecordAggregate) _record).getFormulaRecord();
checkFormulaCachedValueType(CellType.BOOLEAN, fr);
return fr.getCachedBooleanValue();
}
use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class HSSFCell method getNumericCellValue.
/**
* Get the value of the cell as a number.
* For strings we throw an exception.
* For blank cells we return a 0.
* See {@link HSSFDataFormatter} for turning this
* number into a string similar to that which
* Excel would render this number as.
*/
public double getNumericCellValue() {
switch(_cellType) {
case BLANK:
return 0.0;
case NUMERIC:
return ((NumberRecord) _record).getValue();
default:
throw typeMismatch(CellType.NUMERIC, _cellType, false);
case FORMULA:
break;
}
FormulaRecord fr = ((FormulaRecordAggregate) _record).getFormulaRecord();
checkFormulaCachedValueType(CellType.NUMERIC, fr);
return fr.getValue();
}
use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class HSSFCell method convertCellValueToBoolean.
/**
* Chooses a new boolean value for the cell when its type is changing.<p/>
*
* Usually the caller is calling setCellType() with the intention of calling
* setCellValue(boolean) straight afterwards. This method only exists to give
* the cell a somewhat reasonable value until the setCellValue() call (if at all).
* TODO - perhaps a method like setCellTypeAndValue(int, Object) should be introduced to avoid this
*/
private boolean convertCellValueToBoolean() {
switch(_cellType) {
case BOOLEAN:
return ((BoolErrRecord) _record).getBooleanValue();
case STRING:
int sstIndex = ((LabelSSTRecord) _record).getSSTIndex();
String text = _book.getWorkbook().getSSTString(sstIndex).getString();
return Boolean.valueOf(text).booleanValue();
case NUMERIC:
return ((NumberRecord) _record).getValue() != 0;
case FORMULA:
// use cached formula result if it's the right type:
FormulaRecord fr = ((FormulaRecordAggregate) _record).getFormulaRecord();
checkFormulaCachedValueType(CellType.BOOLEAN, fr);
return fr.getCachedBooleanValue();
// These choices are not well justified.
case ERROR:
case BLANK:
return false;
}
throw new RuntimeException("Unexpected cell type (" + _cellType + ")");
}
Aggregations