Search in sources :

Example 6 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)

Example 7 with UDFFinder

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

the class TestExternalFunction method testInvoke.

/**
	 * Checks that an external function can get invoked from the formula
	 * evaluator.
	 */
public void testInvoke() {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testNames.xls");
    HSSFSheet sheet = wb.getSheetAt(0);
    /**
		 * register the two test UDFs in a UDF finder, to be passed to the evaluator
		 */
    UDFFinder udff1 = new DefaultUDFFinder(new String[] { "myFunc" }, new FreeRefFunction[] { new MyFunc() });
    UDFFinder udff2 = new DefaultUDFFinder(new String[] { "myFunc2" }, new FreeRefFunction[] { new MyFunc2() });
    UDFFinder udff = new AggregatingUDFFinder(udff1, udff2);
    HSSFRow row = sheet.getRow(0);
    // =myFunc("_")
    HSSFCell myFuncCell = row.getCell(1);
    // =myFunc2("_")
    HSSFCell myFunc2Cell = row.getCell(2);
    HSSFFormulaEvaluator fe = HSSFFormulaEvaluator.create(wb, null, udff);
    assertEquals("_abc", fe.evaluate(myFuncCell).getStringValue());
    assertEquals("_abc2", fe.evaluate(myFunc2Cell).getStringValue());
}
Also used : AggregatingUDFFinder(org.apache.poi.ss.formula.udf.AggregatingUDFFinder) HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder) AggregatingUDFFinder(org.apache.poi.ss.formula.udf.AggregatingUDFFinder) UDFFinder(org.apache.poi.ss.formula.udf.UDFFinder) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder)

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