use of org.apache.poi.ss.formula.FormulaParseException in project poi by apache.
the class Indirect method evaluateIndirect.
private static ValueEval evaluateIndirect(final OperationEvaluationContext ec, String text, boolean isA1style) {
// Search backwards for '!' because sheet names can contain '!'
int plingPos = text.lastIndexOf('!');
String workbookName;
String sheetName;
// whitespace around this gets trimmed OK
String refText;
if (plingPos < 0) {
workbookName = null;
sheetName = null;
refText = text;
} else {
String[] parts = parseWorkbookAndSheetName(text.subSequence(0, plingPos));
if (parts == null) {
return ErrorEval.REF_INVALID;
}
workbookName = parts[0];
sheetName = parts[1];
refText = text.substring(plingPos + 1);
}
if (Table.isStructuredReference.matcher(refText).matches()) {
// The argument is structured reference
Area3DPxg areaPtg = null;
try {
areaPtg = FormulaParser.parseStructuredReference(refText, (FormulaParsingWorkbook) ec.getWorkbook(), ec.getRowIndex());
} catch (FormulaParseException e) {
return ErrorEval.REF_INVALID;
}
return ec.getArea3DEval(areaPtg);
} else {
// The argument is regular reference
String refStrPart1;
String refStrPart2;
int colonPos = refText.indexOf(':');
if (colonPos < 0) {
refStrPart1 = refText.trim();
refStrPart2 = null;
} else {
refStrPart1 = refText.substring(0, colonPos).trim();
refStrPart2 = refText.substring(colonPos + 1).trim();
}
return ec.getDynamicReference(workbookName, sheetName, refStrPart1, refStrPart2, isA1style);
}
}
use of org.apache.poi.ss.formula.FormulaParseException in project poi by apache.
the class TestFormulaParser method testNamedRangeThatLooksLikeCell.
@Test
public void testNamedRangeThatLooksLikeCell() throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
HSSFName name = wb.createName();
name.setRefersToFormula("Sheet1!B1");
name.setNameName("pfy1");
Ptg[] ptgs;
try {
ptgs = HSSFFormulaParser.parse("count(pfy1)", wb);
} catch (IllegalArgumentException e) {
if (e.getMessage().equals("Specified colIx (1012) is out of range")) {
fail("Identified bug 45354");
}
wb.close();
throw e;
}
confirmTokenClasses(ptgs, NamePtg.class, FuncVarPtg.class);
HSSFCell cell = sheet.createRow(0).createCell(0);
cell.setCellFormula("count(pfy1)");
assertEquals("COUNT(pfy1)", cell.getCellFormula());
try {
cell.setCellFormula("count(pf1)");
fail("Expected formula parse execption");
} catch (FormulaParseException e) {
confirmParseException(e, "Specified named range 'pf1' does not exist in the current workbook.");
}
// plain cell ref, col is in range
cell.setCellFormula("count(fp1)");
wb.close();
}
use of org.apache.poi.ss.formula.FormulaParseException in project poi by apache.
the class BaseTestSheetUpdateArrayFormulas method testSetArrayFormula_incorrectFormula.
/**
* Passing an incorrect formula to sheet.setArrayFormula
* should throw FormulaParseException
*/
@Test
public final void testSetArrayFormula_incorrectFormula() throws IOException {
Workbook workbook = _testDataProvider.createWorkbook();
Sheet sheet = workbook.createSheet();
try {
sheet.setArrayFormula("incorrect-formula(C11_C12*D11_D12)", new CellRangeAddress(10, 10, 10, 10));
fail("expected exception");
} catch (FormulaParseException e) {
//expected exception
}
workbook.close();
}
Aggregations