Search in sources :

Example 1 with Function

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());
}
Also used : NotImplementedException(org.apache.poi.ss.formula.eval.NotImplementedException) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) Function(org.apache.poi.ss.formula.functions.Function) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) CellValue(org.apache.poi.ss.usermodel.CellValue)

Example 2 with Function

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() + ")");
}
Also used : Function(org.apache.poi.ss.formula.functions.Function) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg)

Example 3 with Function

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();
}
Also used : Function(org.apache.poi.ss.formula.functions.Function) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Example 4 with Function

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());
    }
}
Also used : Function(org.apache.poi.ss.formula.functions.Function) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction)

Aggregations

Function (org.apache.poi.ss.formula.functions.Function)4 FreeRefFunction (org.apache.poi.ss.formula.functions.FreeRefFunction)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 NotImplementedException (org.apache.poi.ss.formula.eval.NotImplementedException)1 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)1 AbstractFunctionPtg (org.apache.poi.ss.formula.ptg.AbstractFunctionPtg)1 CellValue (org.apache.poi.ss.usermodel.CellValue)1 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)1 Test (org.junit.Test)1