Search in sources :

Example 6 with FreeRefFunction

use of org.apache.poi.ss.formula.functions.FreeRefFunction 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 7 with FreeRefFunction

use of org.apache.poi.ss.formula.functions.FreeRefFunction in project poi by apache.

the class AnalysisToolPak method getNotSupportedFunctionNames.

/**
     * Returns a collection of ATP function names NOT implemented by POI.
     *
     * @return an array of not supported functions
     * @since 3.8 beta6
     */
public static Collection<String> getNotSupportedFunctionNames() {
    AnalysisToolPak inst = (AnalysisToolPak) instance;
    Collection<String> lst = new TreeSet<String>();
    for (Map.Entry<String, FreeRefFunction> me : inst._functionsByName.entrySet()) {
        FreeRefFunction func = me.getValue();
        if (func instanceof NotImplemented) {
            lst.add(me.getKey());
        }
    }
    return Collections.unmodifiableCollection(lst);
}
Also used : TreeSet(java.util.TreeSet) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with FreeRefFunction

use of org.apache.poi.ss.formula.functions.FreeRefFunction in project poi by apache.

the class AnalysisToolPak method r.

private static void r(Map<String, FreeRefFunction> m, String functionName, FreeRefFunction pFunc) {
    FreeRefFunction func = pFunc == null ? new NotImplemented(functionName) : pFunc;
    m.put(functionName, func);
}
Also used : FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction)

Example 9 with FreeRefFunction

use of org.apache.poi.ss.formula.functions.FreeRefFunction in project poi by apache.

the class AnalysisToolPak method registerFunction.

/**
     * Register a ATP function in runtime.
     *
     * @param name  the function name
     * @param func  the functoin to register
     * @throws IllegalArgumentException if the function is unknown or already  registered.
     * @since 3.8 beta6
     */
public static void registerFunction(String name, FreeRefFunction func) {
    AnalysisToolPak inst = (AnalysisToolPak) instance;
    if (!isATPFunction(name)) {
        FunctionMetadata metaData = FunctionMetadataRegistry.getFunctionByName(name);
        if (metaData != null) {
            throw new IllegalArgumentException(name + " is a built-in Excel function. " + "Use FunctoinEval.registerFunction(String name, Function func) instead.");
        }
        throw new IllegalArgumentException(name + " is not a function from the Excel Analysis Toolpack.");
    }
    FreeRefFunction f = inst.findFunction(name);
    if (f != null && !(f instanceof NotImplemented)) {
        throw new IllegalArgumentException("POI already implememts " + name + ". You cannot override POI's implementations of Excel functions");
    }
    // FIXME: inconsistent case-sensitivity
    inst._functionsByName.put(name, func);
}
Also used : FunctionMetadata(org.apache.poi.ss.formula.function.FunctionMetadata) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction)

Example 10 with FreeRefFunction

use of org.apache.poi.ss.formula.functions.FreeRefFunction 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

FreeRefFunction (org.apache.poi.ss.formula.functions.FreeRefFunction)13 HashMap (java.util.HashMap)3 Map (java.util.Map)3 DefaultUDFFinder (org.apache.poi.ss.formula.udf.DefaultUDFFinder)3 UDFFinder (org.apache.poi.ss.formula.udf.UDFFinder)3 TreeSet (java.util.TreeSet)2 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)2 Function (org.apache.poi.ss.formula.functions.Function)2 AggregatingUDFFinder (org.apache.poi.ss.formula.udf.AggregatingUDFFinder)2 CellValue (org.apache.poi.ss.usermodel.CellValue)2 File (java.io.File)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 TestHSSFWorkbook (org.apache.poi.hssf.usermodel.TestHSSFWorkbook)1 OperationEvaluationContext (org.apache.poi.ss.formula.OperationEvaluationContext)1 FunctionNameEval (org.apache.poi.ss.formula.eval.FunctionNameEval)1 NotImplementedException (org.apache.poi.ss.formula.eval.NotImplementedException)1 NotImplementedFunctionException (org.apache.poi.ss.formula.eval.NotImplementedFunctionException)1 FunctionMetadata (org.apache.poi.ss.formula.function.FunctionMetadata)1 NameXPxg (org.apache.poi.ss.formula.ptg.NameXPxg)1 IndexedUDFFinder (org.apache.poi.ss.formula.udf.IndexedUDFFinder)1