use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class TestBug42464 method process.
private static void process(HSSFRow row, HSSFFormulaEvaluator eval) {
Iterator<Cell> it = row.cellIterator();
while (it.hasNext()) {
HSSFCell cell = (HSSFCell) it.next();
if (cell.getCellTypeEnum() != CellType.FORMULA) {
continue;
}
FormulaRecordAggregate record = (FormulaRecordAggregate) cell.getCellValueRecord();
FormulaRecord r = record.getFormulaRecord();
Ptg[] ptgs = r.getParsedExpression();
String cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex(), false, false).formatAsString();
// if(false && cellRef.equals("BP24")) { // TODO - replace System.out.println()s with asserts
// System.out.print(cellRef);
// System.out.println(" - has " + ptgs.length + " ptgs:");
// for(int i=0; i<ptgs.length; i++) {
// String c = ptgs[i].getClass().toString();
// System.out.println("\t" + c.substring(c.lastIndexOf('.')+1) );
// }
// System.out.println("-> " + cell.getCellFormula());
// }
CellValue evalResult = eval.evaluate(cell);
assertNotNull(evalResult);
}
}
use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class TestValueRecordsAggregate method testSharedFormula.
/**
* Make sure the shared formula DOESNT makes it to the FormulaRecordAggregate when being parsed
* as part of the value records
*/
@Test
public void testSharedFormula() {
List<Record> records = new ArrayList<Record>();
records.add(new FormulaRecord());
records.add(new SharedFormulaRecord());
records.add(new WindowTwoRecord());
constructValueRecord(records);
List<CellValueRecordInterface> cvrs = getValueRecords();
//Ensure that the SharedFormulaRecord has been converted
assertEquals(1, cvrs.size());
CellValueRecordInterface record = cvrs.get(0);
assertNotNull("Row contains a value", record);
assertTrue("First record is a FormulaRecordsAggregate", (record instanceof FormulaRecordAggregate));
}
use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class HSSFCell method getErrorCellValue.
/**
* get the value of the cell as an error code. For strings, numbers, and booleans, we throw an exception.
* For blank cells we return a 0.
*/
@Override
public byte getErrorCellValue() {
switch(_cellType) {
case ERROR:
return ((BoolErrRecord) _record).getErrorValue();
case FORMULA:
FormulaRecord fr = ((FormulaRecordAggregate) _record).getFormulaRecord();
checkFormulaCachedValueType(CellType.ERROR, fr);
return (byte) fr.getCachedErrorValue();
default:
throw typeMismatch(CellType.ERROR, _cellType, false);
}
}
use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class HSSFCell method setCellFormula.
public void setCellFormula(String formula) {
if (isPartOfArrayFormulaGroup()) {
notifyArrayFormulaChanging();
}
int row = _record.getRow();
short col = _record.getColumn();
short styleIndex = _record.getXFIndex();
if (formula == null) {
notifyFormulaChanging();
setCellType(CellType.BLANK, false, row, col, styleIndex);
return;
}
int sheetIndex = _book.getSheetIndex(_sheet);
Ptg[] ptgs = HSSFFormulaParser.parse(formula, _book, FormulaType.CELL, sheetIndex);
setCellType(CellType.FORMULA, false, row, col, styleIndex);
FormulaRecordAggregate agg = (FormulaRecordAggregate) _record;
FormulaRecord frec = agg.getFormulaRecord();
frec.setOptions((short) 2);
frec.setValue(0);
//only set to default if there is no extended format index already set
if (agg.getXFIndex() == (short) 0) {
agg.setXFIndex((short) 0x0f);
}
agg.setParsedExpression(ptgs);
}
use of org.apache.poi.hssf.record.FormulaRecord in project poi by apache.
the class HSSFCell method convertCellValueToString.
private String convertCellValueToString() {
switch(_cellType) {
case BLANK:
return "";
case BOOLEAN:
return ((BoolErrRecord) _record).getBooleanValue() ? "TRUE" : "FALSE";
case STRING:
int sstIndex = ((LabelSSTRecord) _record).getSSTIndex();
return _book.getWorkbook().getSSTString(sstIndex).getString();
case NUMERIC:
return NumberToTextConverter.toText(((NumberRecord) _record).getValue());
case ERROR:
return FormulaError.forInt(((BoolErrRecord) _record).getErrorValue()).getString();
case FORMULA:
// just use cached formula result instead
break;
default:
throw new IllegalStateException("Unexpected cell type (" + _cellType + ")");
}
FormulaRecordAggregate fra = ((FormulaRecordAggregate) _record);
FormulaRecord fr = fra.getFormulaRecord();
switch(CellType.forInt(fr.getCachedResultType())) {
case BOOLEAN:
return fr.getCachedBooleanValue() ? "TRUE" : "FALSE";
case STRING:
return fra.getStringValue();
case NUMERIC:
return NumberToTextConverter.toText(fr.getValue());
case ERROR:
return FormulaError.forInt(fr.getCachedErrorValue()).getString();
default:
throw new IllegalStateException("Unexpected formula result type (" + _cellType + ")");
}
}
Aggregations