public void TestRegisterInRuntime() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = (HSSFSheet)wb.CreateSheet("Sheet1"); HSSFRow row = (HSSFRow)sheet.CreateRow(0); HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb); HSSFCell cellA = (HSSFCell)row.CreateCell(0); cellA.CellFormula = ("FISHER(A5)"); CellValue cv; try { //NPOI //Run it twice in NUnit Gui Window, the first passed but the second failed. //Maybe the function was cached. Ignore it. cv = fe.Evaluate(cellA); Assert.Fail("expectecd exception"); } catch (NotImplementedException) { ; } FunctionEval.RegisterFunction("FISHER", new Function1());/*Function() { * public ValueEval Evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) { * return ErrorEval.NA; * } * });*/ cv = fe.Evaluate(cellA); Assert.AreEqual(ErrorEval.NA.ErrorCode, cv.ErrorValue); HSSFCell cellB = (HSSFCell)row.CreateCell(1); cellB.CellFormula = ("CUBEMEMBERPROPERTY(A5)"); try { cv = fe.Evaluate(cellB); Assert.Fail("expectecd exception"); } catch (NotImplementedException) { ; } AnalysisToolPak.RegisterFunction("CUBEMEMBERPROPERTY", new FreeRefFunction1());/*FreeRefFunction() { * public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) { * return ErrorEval.NUM_ERROR; * } * });*/ cv = fe.Evaluate(cellB); Assert.AreEqual(ErrorEval.NUM_ERROR.ErrorCode, cv.ErrorValue); }
/** * 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.RegisterFunction(name, func); }
public void TestExceptions() { NPOI.SS.Formula.Functions.Function func = new Function2(); try { FunctionEval.RegisterFunction("SUM", func); Assert.Fail("expectecd exception"); } catch (ArgumentException e) { Assert.AreEqual("POI already implememts SUM" + ". You cannot override POI's implementations of Excel functions", e.Message); } try { FunctionEval.RegisterFunction("SUMXXX", func); Assert.Fail("expectecd exception"); } catch (ArgumentException e) { Assert.AreEqual("Unknown function: SUMXXX", e.Message); } try { FunctionEval.RegisterFunction("ISODD", func); Assert.Fail("expectecd exception"); } catch (ArgumentException e) { Assert.AreEqual("ISODD is a function from the Excel Analysis Toolpack. " + "Use AnalysisToolpack.RegisterFunction(String name, FreeRefFunction func) instead.", e.Message); } FreeRefFunction atpFunc = new FreeRefFunction2();/*FreeRefFunction() { * public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) { * return ErrorEval.NUM_ERROR; * } * };*/ try { AnalysisToolPak.RegisterFunction("ISODD", atpFunc); Assert.Fail("expectecd exception"); } catch (ArgumentException e) { Assert.AreEqual("POI already implememts ISODD" + ". You cannot override POI's implementations of Excel functions", e.Message); } try { AnalysisToolPak.RegisterFunction("ISODDXXX", atpFunc); Assert.Fail("expectecd exception"); } catch (ArgumentException e) { Assert.AreEqual("ISODDXXX is not a function from the Excel Analysis Toolpack.", e.Message); } try { AnalysisToolPak.RegisterFunction("SUM", atpFunc); Assert.Fail("expectecd exception"); } catch (ArgumentException e) { Assert.AreEqual("SUM is a built-in Excel function. " + "Use FunctoinEval.RegisterFunction(String name, Function func) instead.", e.Message); } }