use of org.apache.poi.ss.formula.functions.Function in project poi by apache.
the class TestFunctionRegistry method testRegisterInRuntime.
public void testRegisterInRuntime() {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
HSSFRow row = sheet.createRow(0);
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
HSSFCell cellA = row.createCell(0);
cellA.setCellFormula("FISHER(A5)");
CellValue cv;
try {
cv = fe.evaluate(cellA);
fail("expectecd exception");
} catch (NotImplementedException e) {
}
FunctionEval.registerFunction("FISHER", new Function() {
@Override
public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
return ErrorEval.NA;
}
});
cv = fe.evaluate(cellA);
assertEquals(ErrorEval.NA.getErrorCode(), cv.getErrorValue());
HSSFCell cellB = row.createCell(1);
cellB.setCellFormula("CUBEMEMBERPROPERTY(A5)");
try {
cv = fe.evaluate(cellB);
fail("expectecd exception");
} catch (NotImplementedException e) {
}
AnalysisToolPak.registerFunction("CUBEMEMBERPROPERTY", new FreeRefFunction() {
@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
return ErrorEval.NUM_ERROR;
}
});
cv = fe.evaluate(cellB);
assertEquals(ErrorEval.NUM_ERROR.getErrorCode(), cv.getErrorValue());
}
use of org.apache.poi.ss.formula.functions.Function in project poi by apache.
the class OperationEvaluatorFactory method evaluate.
/**
* returns the OperationEval concrete impl instance corresponding
* to the supplied operationPtg
*/
public static ValueEval evaluate(OperationPtg ptg, ValueEval[] args, OperationEvaluationContext ec) {
if (ptg == null) {
throw new IllegalArgumentException("ptg must not be null");
}
Function result = _instancesByPtgClass.get(ptg);
if (result != null) {
return result.evaluate(args, ec.getRowIndex(), (short) ec.getColumnIndex());
}
if (ptg instanceof AbstractFunctionPtg) {
AbstractFunctionPtg fptg = (AbstractFunctionPtg) ptg;
int functionIndex = fptg.getFunctionIndex();
switch(functionIndex) {
case FunctionMetadataRegistry.FUNCTION_INDEX_INDIRECT:
return Indirect.instance.evaluate(args, ec);
case FunctionMetadataRegistry.FUNCTION_INDEX_EXTERNAL:
return UserDefinedFunction.instance.evaluate(args, ec);
}
return FunctionEval.getBasicFunction(functionIndex).evaluate(args, ec.getRowIndex(), (short) ec.getColumnIndex());
}
throw new RuntimeException("Unexpected operation ptg class (" + ptg.getClass().getName() + ")");
}
use of org.apache.poi.ss.formula.functions.Function in project poi by apache.
the class TestXSSFBugs method bug54436.
@Test
public void bug54436() throws IOException {
Workbook wb = XSSFTestDataSamples.openSampleWorkbook("54436.xlsx");
if (!WorkbookEvaluator.getSupportedFunctionNames().contains("GETPIVOTDATA")) {
Function func = new Function() {
@Override
public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
return ErrorEval.NA;
}
};
WorkbookEvaluator.registerFunction("GETPIVOTDATA", func);
}
wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
wb.close();
}
use of org.apache.poi.ss.formula.functions.Function in project poi by apache.
the class TestFunctionRegistry method testExceptions.
public void testExceptions() {
Function func = new Function() {
@Override
public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
return ErrorEval.NA;
}
};
try {
FunctionEval.registerFunction("SUM", func);
fail("expectecd exception");
} catch (IllegalArgumentException e) {
assertEquals("POI already implememts SUM" + ". You cannot override POI's implementations of Excel functions", e.getMessage());
}
try {
FunctionEval.registerFunction("SUMXXX", func);
fail("expectecd exception");
} catch (IllegalArgumentException e) {
assertEquals("Unknown function: SUMXXX", e.getMessage());
}
try {
FunctionEval.registerFunction("ISODD", func);
fail("expectecd exception");
} catch (IllegalArgumentException e) {
assertEquals("ISODD is a function from the Excel Analysis Toolpack. " + "Use AnalysisToolpack.registerFunction(String name, FreeRefFunction func) instead.", e.getMessage());
}
FreeRefFunction atpFunc = new FreeRefFunction() {
@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
return ErrorEval.NUM_ERROR;
}
};
try {
AnalysisToolPak.registerFunction("ISODD", atpFunc);
fail("expectecd exception");
} catch (IllegalArgumentException e) {
assertEquals("POI already implememts ISODD" + ". You cannot override POI's implementations of Excel functions", e.getMessage());
}
try {
AnalysisToolPak.registerFunction("ISODDXXX", atpFunc);
fail("expectecd exception");
} catch (IllegalArgumentException e) {
assertEquals("ISODDXXX is not a function from the Excel Analysis Toolpack.", e.getMessage());
}
try {
AnalysisToolPak.registerFunction("SUM", atpFunc);
fail("expectecd exception");
} catch (IllegalArgumentException e) {
assertEquals("SUM is a built-in Excel function. " + "Use FunctoinEval.registerFunction(String name, Function func) instead.", e.getMessage());
}
}
Aggregations