use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.
the class HSSFSheet method setArrayFormula.
@Override
public CellRange<HSSFCell> setArrayFormula(String formula, CellRangeAddress range) {
// make sure the formula parses OK first
int sheetIndex = _workbook.getSheetIndex(this);
Ptg[] ptgs = HSSFFormulaParser.parse(formula, _workbook, FormulaType.ARRAY, sheetIndex);
CellRange<HSSFCell> cells = getCellRange(range);
for (HSSFCell c : cells) {
c.setCellArrayFormula(range);
}
HSSFCell mainArrayFormulaCell = cells.getTopLeftCell();
FormulaRecordAggregate agg = (FormulaRecordAggregate) mainArrayFormulaCell.getCellValueRecord();
agg.setArrayFormula(range, ptgs);
return cells;
}
use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.
the class FormulaExtractor method getPtgs.
public static Ptg[] getPtgs(HSSFCell cell) {
CellValueRecordInterface vr = cell.getCellValueRecord();
if (!(vr instanceof FormulaRecordAggregate)) {
throw new IllegalArgumentException("Not a formula cell");
}
FormulaRecordAggregate fra = (FormulaRecordAggregate) vr;
return fra.getFormulaRecord().getParsedExpression();
}
use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate 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.aggregates.FormulaRecordAggregate in project poi by apache.
the class HSSFEvaluationWorkbook method getFormulaTokens.
@Override
public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
HSSFCell cell = ((HSSFEvaluationCell) evalCell).getHSSFCell();
// re-parsing the formula text also works, but is a waste of time
// return HSSFFormulaParser.parse(cell.getCellFormula(), _uBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
// It is useful within the tests to make sure that all formulas POI can evaluate can also be parsed.
// see HSSFFileHandler.handleFile instead
FormulaRecordAggregate fra = (FormulaRecordAggregate) cell.getCellValueRecord();
return fra.getFormulaTokens();
}
use of org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate in project poi by apache.
the class HSSFCell method getRichStringCellValue.
/**
* get the value of the cell as a string - for numeric cells we throw an exception.
* For blank cells we return an empty string.
* For formulaCells that are not string Formulas, we throw an exception
*/
public HSSFRichTextString getRichStringCellValue() {
switch(_cellType) {
case BLANK:
return new HSSFRichTextString("");
case STRING:
return _stringValue;
default:
throw typeMismatch(CellType.STRING, _cellType, false);
case FORMULA:
break;
}
FormulaRecordAggregate fra = ((FormulaRecordAggregate) _record);
checkFormulaCachedValueType(CellType.STRING, fra.getFormulaRecord());
String strVal = fra.getStringValue();
return new HSSFRichTextString(strVal == null ? "" : strVal);
}
Aggregations