Search in sources :

Example 1 with AggregatingUDFFinder

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

the class HSSFWorkbook method addToolPack.

/**
     * Register a new toolpack in this workbook.
     *
     * @param toopack the toolpack to register
     */
@Override
public void addToolPack(UDFFinder toopack) {
    AggregatingUDFFinder udfs = (AggregatingUDFFinder) _udfFinder;
    udfs.add(toopack);
}
Also used : AggregatingUDFFinder(org.apache.poi.ss.formula.udf.AggregatingUDFFinder)

Example 2 with AggregatingUDFFinder

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

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

the class ExcelAntWorkbookUtil method getFunctions.

/**
     * returns a UDFFinder that contains all of the functions added.
     *
     * @return
     */
protected UDFFinder getFunctions() {
    String[] names = new String[xlsMacroList.size()];
    FreeRefFunction[] functions = new FreeRefFunction[xlsMacroList.size()];
    int x = 0;
    for (Map.Entry<String, FreeRefFunction> entry : xlsMacroList.entrySet()) {
        names[x] = entry.getKey();
        functions[x] = entry.getValue();
    }
    UDFFinder udff1 = new DefaultUDFFinder(names, functions);
    UDFFinder udff = new AggregatingUDFFinder(udff1);
    return udff;
}
Also used : AggregatingUDFFinder(org.apache.poi.ss.formula.udf.AggregatingUDFFinder) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder) UDFFinder(org.apache.poi.ss.formula.udf.UDFFinder) AggregatingUDFFinder(org.apache.poi.ss.formula.udf.AggregatingUDFFinder) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) HashMap(java.util.HashMap) Map(java.util.Map) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder)

Example 4 with AggregatingUDFFinder

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

AggregatingUDFFinder (org.apache.poi.ss.formula.udf.AggregatingUDFFinder)4 DefaultUDFFinder (org.apache.poi.ss.formula.udf.DefaultUDFFinder)3 UDFFinder (org.apache.poi.ss.formula.udf.UDFFinder)3 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 FreeRefFunction (org.apache.poi.ss.formula.functions.FreeRefFunction)2 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 Test (org.junit.Test)1