Example #1
0
        /**
         * Register a new function in runtime.
         *
         * @param name  the function name
         * @param func  the functoin to register
         * @throws ArgumentException if the function is unknown or already  registered.
         * @since 3.8 beta6
         */

        public static void RegisterFunction(String name, Function func)
        {
            FunctionMetadata metaData = FunctionMetadataRegistry.GetFunctionByName(name);

            if (metaData == null)
            {
                if (AnalysisToolPak.IsATPFunction(name))
                {
                    throw new ArgumentException(name + " is a function from the Excel Analysis Toolpack. " +
                                                "Use AnalysisToolpack.RegisterFunction(String name, FreeRefFunction func) instead.");
                }
                else
                {
                    throw new ArgumentException("Unknown function: " + name);
                }
            }

            int idx = metaData.Index;

            if (functions[idx] is NotImplementedFunction)
            {
                functions[idx] = func;
            }
            else
            {
                throw new ArgumentException("POI already implememts " + name +
                                            ". You cannot override POI's implementations of Excel functions");
            }
        }
Example #2
0
        /**
         * Return a collection of functions that POI does not support
         *
         * @return names of functions NOT supported by POI
         */

        public static List <string> GetNotSupportedFunctionNames()
        {
            List <string> lst = new List <string>();

            lst.AddRange(FunctionEval.GetNotSupportedFunctionNames());
            lst.AddRange(AnalysisToolPak.GetNotSupportedFunctionNames());
            return(lst);
        }
Example #3
0
        /**
         * Return a collection of functions that POI can evaluate
         *
         * @return names of functions supported by POI
         */
        public static IList <String> GetSupportedFunctionNames()
        {
            List <String> lst = new List <String>();

            lst.AddRange(FunctionEval.GetSupportedFunctionNames());
            lst.AddRange(AnalysisToolPak.GetSupportedFunctionNames());
            return(lst.AsReadOnly());
        }
Example #4
0
        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);
        }
Example #5
0
        /**
         * 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);
        }
Example #6
0
        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);
            }
        }