Search in sources :

Example 1 with FreeRefFunction

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

the class UserDefinedFunctionExample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        // e.g. src/examples/src/org/apache/poi/ss/examples/formula/mortgage-calculation.xls Sheet1!B4
        System.out.println("usage: UserDefinedFunctionExample fileName cellId");
        return;
    }
    System.out.println("fileName: " + args[0]);
    System.out.println("cell: " + args[1]);
    File workbookFile = new File(args[0]);
    Workbook workbook = WorkbookFactory.create(workbookFile, null, true);
    try {
        String[] functionNames = { "calculatePayment" };
        FreeRefFunction[] functionImpls = { new CalculateMortgage() };
        UDFFinder udfToolpack = new DefaultUDFFinder(functionNames, functionImpls);
        // register the user-defined function in the workbook
        workbook.addToolPack(udfToolpack);
        FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
        CellReference cr = new CellReference(args[1]);
        String sheetName = cr.getSheetName();
        Sheet sheet = workbook.getSheet(sheetName);
        int rowIdx = cr.getRow();
        int colIdx = cr.getCol();
        Row row = sheet.getRow(rowIdx);
        Cell cell = row.getCell(colIdx);
        CellValue value = evaluator.evaluate(cell);
        System.out.println("returns value: " + value);
    } finally {
        workbook.close();
    }
}
Also used : FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) CellReference(org.apache.poi.ss.util.CellReference) Workbook(org.apache.poi.ss.usermodel.Workbook) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder) UDFFinder(org.apache.poi.ss.formula.udf.UDFFinder) CellValue(org.apache.poi.ss.usermodel.CellValue) Row(org.apache.poi.ss.usermodel.Row) File(java.io.File) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator)

Example 2 with FreeRefFunction

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

the class IndexedUDFFinder method findFunction.

@Override
public FreeRefFunction findFunction(String name) {
    FreeRefFunction func = super.findFunction(name);
    if (func != null) {
        int idx = getFunctionIndex(name);
        _funcMap.put(idx, name);
    }
    return func;
}
Also used : FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction)

Example 3 with FreeRefFunction

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

the class UserDefinedFunction method evaluate.

public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
    int nIncomingArgs = args.length;
    if (nIncomingArgs < 1) {
        throw new RuntimeException("function name argument missing");
    }
    ValueEval nameArg = args[0];
    String functionName;
    if (nameArg instanceof FunctionNameEval) {
        functionName = ((FunctionNameEval) nameArg).getFunctionName();
    } else {
        throw new RuntimeException("First argument should be a NameEval, but got (" + nameArg.getClass().getName() + ")");
    }
    FreeRefFunction targetFunc = ec.findUserDefinedFunction(functionName);
    if (targetFunc == null) {
        throw new NotImplementedFunctionException(functionName);
    }
    int nOutGoingArgs = nIncomingArgs - 1;
    ValueEval[] outGoingArgs = new ValueEval[nOutGoingArgs];
    System.arraycopy(args, 1, outGoingArgs, 0, nOutGoingArgs);
    return targetFunc.evaluate(outGoingArgs, ec);
}
Also used : FunctionNameEval(org.apache.poi.ss.formula.eval.FunctionNameEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) NotImplementedFunctionException(org.apache.poi.ss.formula.eval.NotImplementedFunctionException)

Example 4 with FreeRefFunction

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

the class AnalysisToolPak method getSupportedFunctionNames.

/**
     * Returns a collection of ATP function names implemented by POI.
     *
     * @return an array of supported functions
     * @since 3.8 beta6
     */
public static Collection<String> getSupportedFunctionNames() {
    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 != null && !(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 5 with FreeRefFunction

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

the class TestWorkbook method testAddNameX.

@Test
public void testAddNameX() throws IOException {
    HSSFWorkbook hwb = new HSSFWorkbook();
    InternalWorkbook wb = TestHSSFWorkbook.getInternalWorkbook(hwb);
    assertNotNull(wb.getNameXPtg("ISODD", AggregatingUDFFinder.DEFAULT));
    FreeRefFunction NotImplemented = new FreeRefFunction() {

        @Override
        public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
            throw new RuntimeException("not implemented");
        }
    };
    /*
         * register the two test UDFs in a UDF finder, to be passed to the evaluator
         */
    UDFFinder udff1 = new DefaultUDFFinder(new String[] { "myFunc" }, new FreeRefFunction[] { NotImplemented });
    UDFFinder udff2 = new DefaultUDFFinder(new String[] { "myFunc2" }, new FreeRefFunction[] { NotImplemented });
    UDFFinder udff = new AggregatingUDFFinder(udff1, udff2);
    assertNotNull(wb.getNameXPtg("myFunc", udff));
    assertNotNull(wb.getNameXPtg("myFunc2", udff));
    // myFunc3 is unknown
    assertNull(wb.getNameXPtg("myFunc3", udff));
    hwb.close();
}
Also used : AggregatingUDFFinder(org.apache.poi.ss.formula.udf.AggregatingUDFFinder) OperationEvaluationContext(org.apache.poi.ss.formula.OperationEvaluationContext) AggregatingUDFFinder(org.apache.poi.ss.formula.udf.AggregatingUDFFinder) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder) UDFFinder(org.apache.poi.ss.formula.udf.UDFFinder) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) TestHSSFWorkbook(org.apache.poi.hssf.usermodel.TestHSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder) Test(org.junit.Test)

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