Search in sources :

Example 1 with UDFFinder

use of org.apache.poi.ss.formula.udf.UDFFinder in project poi by apache.

the class TestExcelAntWorkbookUtil method testAddFunctionClassName.

public void testAddFunctionClassName() throws Exception {
    fixture = new ExcelAntWorkbookUtilTestHelper(mortgageCalculatorFileName);
    assertNotNull(fixture);
    fixture.addFunction("h2_ZFactor", CalculateMortgageFunction.class.getName());
    UDFFinder functions = fixture.getFunctions();
    assertNotNull(functions);
    assertNotNull(functions.findFunction("h2_ZFactor"));
}
Also used : CalculateMortgageFunction(org.apache.poi.ss.examples.formula.CalculateMortgageFunction) UDFFinder(org.apache.poi.ss.formula.udf.UDFFinder)

Example 2 with UDFFinder

use of org.apache.poi.ss.formula.udf.UDFFinder 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)

Example 3 with UDFFinder

use of org.apache.poi.ss.formula.udf.UDFFinder 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 4 with UDFFinder

use of org.apache.poi.ss.formula.udf.UDFFinder in project poi by apache.

the class TestExcelAntWorkbookUtil method testAddFunctionInvalidClassName.

public void testAddFunctionInvalidClassName() throws Exception {
    fixture = new ExcelAntWorkbookUtilTestHelper(mortgageCalculatorFileName);
    assertNotNull(fixture);
    fixture.addFunction("h2_ZFactor", String.class.getName());
    UDFFinder functions = fixture.getFunctions();
    assertNotNull(functions);
    assertNull(functions.findFunction("h2_ZFactor"));
}
Also used : UDFFinder(org.apache.poi.ss.formula.udf.UDFFinder)

Example 5 with UDFFinder

use of org.apache.poi.ss.formula.udf.UDFFinder in project poi by apache.

the class TestExcelAntWorkbookUtil method testAddFunction.

public void testAddFunction() {
    fixture = new ExcelAntWorkbookUtilTestHelper(mortgageCalculatorFileName);
    assertNotNull(fixture);
    fixture.addFunction("h2_ZFactor", new CalculateMortgageFunction());
    UDFFinder functions = fixture.getFunctions();
    assertNotNull(functions);
    assertNotNull(functions.findFunction("h2_ZFactor"));
}
Also used : CalculateMortgageFunction(org.apache.poi.ss.examples.formula.CalculateMortgageFunction) UDFFinder(org.apache.poi.ss.formula.udf.UDFFinder)

Aggregations

UDFFinder (org.apache.poi.ss.formula.udf.UDFFinder)7 DefaultUDFFinder (org.apache.poi.ss.formula.udf.DefaultUDFFinder)4 FreeRefFunction (org.apache.poi.ss.formula.functions.FreeRefFunction)3 AggregatingUDFFinder (org.apache.poi.ss.formula.udf.AggregatingUDFFinder)3 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 CalculateMortgageFunction (org.apache.poi.ss.examples.formula.CalculateMortgageFunction)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)1 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)1 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)1 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)1 TestHSSFWorkbook (org.apache.poi.hssf.usermodel.TestHSSFWorkbook)1 OperationEvaluationContext (org.apache.poi.ss.formula.OperationEvaluationContext)1 Cell (org.apache.poi.ss.usermodel.Cell)1 CellValue (org.apache.poi.ss.usermodel.CellValue)1 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1