Esempio n. 1
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: LN(number)
            // https://support.office.com/en-gb/article/LN-function-81fe1ed7-dac9-4acd-ba1d-07a142c6118f

            if (args.Parameters.Length != 1)
            {
                throw new ArgumentException(
                          $"LN function takes only 1 argument, got {args.Parameters.Length}");
            }

            var arg = args.Parameters[0].Evaluate();

            if (arg is ErrorValue)
            {
                args.Result = arg;
                return;
            }

            var val = Conversion.ToDoubleOrErrorValue(arg);

            if (val is ErrorValue)
            {
                args.Result = val;
                return;
            }

            args.Result = Math.Log((double)val);
        }
Esempio n. 2
0
        private static void MinFunction(IFunctionArgs args, IExcelCaller caller)
        {
            if (args.Parameters.Length < 1)
            {
                throw new ArgumentException(
                          $"MIN function takes at least 1 arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            var numbers = new List <double>();

            foreach (var argsParameter in args.Parameters)
            {
                var arg = argsParameter.Evaluate();
                if (arg is ErrorValue)
                {
                    args.Result = arg;
                    return;
                }

                var val = Conversion.ToDoubleOrErrorValue(arg);
                if (val is ErrorValue)
                {
                    args.Result = val;
                    return;
                }

                numbers.Add((double)val);
            }

            args.Result = numbers.Min();
        }
Esempio n. 3
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: RADIANS(angle)
            // https://support.office.com/en-gb/article/RADIANS-function-ac409508-3d48-45f5-ac02-1497c92de5bf

            if (args.Parameters.Length != 1)
            {
                throw new ArgumentException(
                          $"RADIANS function takes only 1 argument, got {args.Parameters.Length}");
            }

            var arg = args.Parameters[0].Evaluate();

            if (arg is ErrorValue)
            {
                args.Result = arg;
                return;
            }

            var val = Conversion.ToDoubleOrErrorValue(arg);

            if (val is ErrorValue)
            {
                args.Result = val;
                return;
            }

            var angle = (double)val;

            args.Result = Math.PI / 180 * angle;
        }
Esempio n. 4
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: IF(logical_test, value_if_true, [value_if_false])
            // https://support.office.com/en-gb/article/IF-function-69aed7c9-4e8a-4755-a9bc-aa8bbff73be2

            if (args.Parameters.Length != 2 && args.Parameters.Length != 3)
            {
                throw new ArgumentException(
                          "IF function takes 2 or 3 arguments",
                          nameof(args));
            }

            var arg = args.Parameters[0].Evaluate();

            if (arg is ErrorValue)
            {
                args.Result = arg;
                return;
            }

            if (args.Parameters.Length == 3)
            {
                args.Result = Convert.ToBoolean(arg)
                    ? args.Parameters[1].Evaluate()
                    : args.Parameters[2].Evaluate();
            }
            else
            {
                args.Result = Convert.ToBoolean(arg)
                    ? args.Parameters[1].Evaluate()
                    : false;
            }
        }
Esempio n. 5
0
 public static void Map <TArg1, TArg2, TArg3, TResult>(IFunctionArgs <TArg1, TArg2, TArg3, TResult> first, IFunctionArgs <TArg1, TArg2, TArg3, TResult> second)
 {
     second.Arg1        = first.Arg1;
     second.Arg2        = first.Arg2;
     second.Arg3        = first.Arg3;
     second.ReturnValue = first.ReturnValue;
 }
Esempio n. 6
0
        private static void AddThemFunction(IFunctionArgs args, IExcelCaller caller)
        {
            var arg1 = StringConversion.ToDouble(args.Parameters[0].Evaluate());
            var arg2 = StringConversion.ToDouble(args.Parameters[1].Evaluate());

            args.Result = arg1 + arg2;
        }
Esempio n. 7
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: MAX(number1, [number2], ...)
            // https://support.office.com/en-GB/article/MAX-function-e0012414-9ac8-4b34-9a47-73e662c08098

            if (args.Parameters.Length < 1)
            {
                throw new ArgumentException(
                          $"MAX function takes at least 1 arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            var numbers = new List <double>();

            foreach (var argsParameter in args.Parameters)
            {
                var arg = argsParameter.Evaluate();
                if (arg is ErrorValue)
                {
                    args.Result = arg;
                    return;
                }

                var val = Conversion.ToDoubleOrErrorValue(arg);
                if (val is ErrorValue)
                {
                    args.Result = val;
                    return;
                }

                numbers.Add((double)val);
            }

            args.Result = numbers.Max();
        }
Esempio n. 8
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: COUNT(value1, [value2], ...)
            // https://support.office.com/en-gb/article/COUNT-function-a59cd7fc-b623-4d93-87a4-d23bf411294c

            if (args.Parameters.Length < 1)
            {
                throw new ArgumentException(
                          $"COUNT function takes at least 1 arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            var numbers = new List <double>();

            foreach (var argsParameter in args.Parameters)
            {
                var arg = argsParameter.Evaluate();
                if (arg is ErrorValue)
                {
                    args.Result = arg;
                    return;
                }

                var val = Conversion.ToDoubleOrErrorValue(arg);
                if (val is ErrorValue)
                {
                    args.Result = val;
                    return;
                }

                numbers.Add((double)val);
            }

            args.Result = (double)numbers.Count;
        }
Esempio n. 9
0
        private static void LnFunction(IFunctionArgs args, IExcelCaller caller)
        {
            if (args.Parameters.Length != 1)
            {
                throw new ArgumentException(
                          $"LN function takes only 1 argument, got {args.Parameters.Length}");
            }

            var arg = args.Parameters[0].Evaluate();

            if (arg is ErrorValue)
            {
                args.Result = arg;
                return;
            }

            var val = Conversion.ToDoubleOrErrorValue(arg);

            if (val is ErrorValue)
            {
                args.Result = val;
                return;
            }

            args.Result = Math.Log((double)val);
        }
Esempio n. 10
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: AVERAGE(number1, [number2], ...)
            // https://support.office.com/en-GB/article/AVERAGE-function-047bac88-d466-426c-a32b-8f33eb960cf6

            if (args.Parameters.Length < 1)
            {
                throw new ArgumentException(
                          $"AVERAGE function takes at least 1 arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            var numbers = new List <double>();

            foreach (var argsParameter in args.Parameters)
            {
                var arg = argsParameter.Evaluate();
                if (arg is ErrorValue)
                {
                    args.Result = arg;
                    return;
                }

                var val = Conversion.ToDoubleOrErrorValue(arg);
                if (val is ErrorValue)
                {
                    args.Result = val;
                    return;
                }

                numbers.Add((double)val);
            }

            args.Result = numbers.Average();
        }
Esempio n. 11
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: SUM(number1,[number2],...)
            // https://support.office.com/en-GB/article/SUM-function-043e1c7d-7726-4e80-8f32-07b23e057f89

            if (args.Parameters.Length < 1)
            {
                throw new ArgumentException(
                          $"SUM function takes at least 1 arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            var numbers = new List <double>();

            foreach (var argsParameter in args.Parameters)
            {
                var arg = argsParameter.Evaluate();
                if (arg is ErrorValue)
                {
                    args.Result = arg;
                    return;
                }

                var val = Conversion.ToDoubleOrErrorValue(arg);
                if (val is ErrorValue)
                {
                    args.Result = val;
                    return;
                }

                numbers.Add((double)val);
            }

            args.Result = numbers.Sum();
        }
Esempio n. 12
0
        private static void IfErrorFunction(IFunctionArgs args, IExcelCaller caller)
        {
            if (args.Parameters.Length != 2)
            {
                throw new ArgumentException(
                          "IFERROR function takes exactly 2 arguments",
                          nameof(args));
            }

            try
            {
                var arg = args.Parameters[0].Evaluate();
                if (arg is ErrorValue)
                {
                    args.Result = args.Parameters[1].Evaluate();
                    return;
                }

                args.Result = arg;
            }
            catch (Exception)
            {
                args.Result = args.Parameters[1].Evaluate();
            }
        }
Esempio n. 13
0
        public int Proceed(ref CSharpDeveloperMixin instance, IFunctionArgs <int, int, int, int> args)
        {
            //var aspectArgs = new FunctionInterceptionArgsImpl<CSharpDeveloperMixin, int, int, int, int>(instance, args.Method, MethodDecoratorFunctionBinding.singleton, args.Arg1, args.Arg2, args.Arg3);

            //Aspects.traceAspect.OnInvoke(aspectArgs);
            //FunctionArgsMapper.Map<int, int, int, int>(aspectArgs, args);

            //return args.ReturnValue;
            //var binding = MethodDecoratorFunctionBinding.singleton;
            var aspectArgs = new FunctionExecutionArgsImpl <CSharpDeveloperMixin, int, int, int, int>(instance, args.Method, args.Arg1, args.Arg2, args.Arg3);

            Aspects.traceAspect3.OnEntry(aspectArgs);

            try {
                var i = aspectArgs.Arg1;
                var k = aspectArgs.Arg3;
                aspectArgs.ReturnValue = instance.Code(ref i, aspectArgs.Arg2, ref k);
                aspectArgs.Arg1        = i;
                aspectArgs.Arg3        = k;
                Aspects.traceAspect3.OnSuccess(aspectArgs);
            }
            finally {
                Aspects.traceAspect3.OnExit(aspectArgs);
            }

            return(aspectArgs.ReturnValue);
        }
Esempio n. 14
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: IFERROR(value, value_if_error)
            // https://support.office.com/en-gb/article/IFERROR-function-c526fd07-caeb-47b8-8bb6-63f3e417f611

            if (args.Parameters.Length != 2)
            {
                throw new ArgumentException(
                          "IFERROR function takes exactly 2 arguments",
                          nameof(args));
            }

            try
            {
                var arg = args.Parameters[0].Evaluate();
                if (arg is ErrorValue)
                {
                    args.Result = args.Parameters[1].Evaluate();
                    return;
                }

                args.Result = arg;
            }
            catch (Exception)
            {
                args.Result = args.Parameters[1].Evaluate();
            }
        }
Esempio n. 15
0
        private static void OrFunction(IFunctionArgs args, IExcelCaller caller)
        {
            var result = false;

            foreach (var fx in args.Parameters)
            {
                var arg = fx.Evaluate();
                if (arg is ErrorValue)
                {
                    args.Result = arg;
                    return;
                }

                if (!(arg is bool))
                {
                    args.Result = ErrorValue.Num;
                    return;
                }

                if (!(bool)arg)
                {
                    continue;
                }

                result = true;
                break;
            }

            args.Result = result;
        }
Esempio n. 16
0
        private static void IfFunction(IFunctionArgs args, IExcelCaller caller)
        {
            if (args.Parameters.Length != 2 && args.Parameters.Length != 3)
            {
                throw new ArgumentException(
                          "IF function takes 2 or 3 arguments",
                          nameof(args));
            }

            var arg = args.Parameters[0].Evaluate();

            if (arg is ErrorValue)
            {
                args.Result = arg;
                return;
            }

            if (args.Parameters.Length == 3)
            {
                args.Result = Convert.ToBoolean(arg)
                    ? args.Parameters[1].Evaluate()
                    : args.Parameters[2].Evaluate();
            }
            else
            {
                args.Result = Convert.ToBoolean(arg)
                    ? args.Parameters[1].Evaluate()
                    : false;
            }
        }
Esempio n. 17
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: MIN(number1, [number2], ...)
            // https://support.office.com/en-GB/article/MIN-function-61635d12-920f-4ce2-a70f-96f202dcc152

            if (args.Parameters.Length < 1)
            {
                throw new ArgumentException(
                          $"MIN function takes at least 1 arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            var numbers = new List <double>();

            foreach (var argsParameter in args.Parameters)
            {
                var arg = argsParameter.Evaluate();
                if (arg is ErrorValue)
                {
                    args.Result = arg;
                    return;
                }

                var val = Conversion.ToDoubleOrErrorValue(arg);
                if (val is ErrorValue)
                {
                    args.Result = val;
                    return;
                }

                numbers.Add((double)val);
            }

            args.Result = numbers.Min();
        }
Esempio n. 18
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: OR(logical1, [logical2], ...)
            // https://support.office.com/en-gb/article/OR-function-7d17ad14-8700-4281-b308-00b131e22af0

            var result = false;

            foreach (var fx in args.Parameters)
            {
                var arg = fx.Evaluate();
                if (arg is ErrorValue)
                {
                    args.Result = arg;
                    return;
                }

                if (!(arg is bool))
                {
                    args.Result = ErrorValue.Num;
                    return;
                }

                if (!(bool)arg)
                {
                    continue;
                }

                result = true;
                break;
            }

            args.Result = result;
        }        
Esempio n. 19
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: SQRT(number)
            // https://support.office.com/en-gb/article/SQRT-function-654975c2-05c4-4831-9a24-2c65e4040fdf

            if (args.Parameters.Length != 1)
            {
                throw new ArgumentException(
                          $"SQRT function takes only 1 argument, got {args.Parameters.Length}");
            }

            var arg = args.Parameters[0].Evaluate();

            if (arg is ErrorValue)
            {
                args.Result = arg;
                return;
            }

            var val = Conversion.ToDoubleOrErrorValue(arg);

            if (val is ErrorValue)
            {
                args.Result = val;
                return;
            }

            args.Result = Conversion.ErrorValueOnInvalidDouble(Math.Sqrt((double)val));
        }
Esempio n. 20
0
        private static void PiFunction(IFunctionArgs args, IExcelCaller caller)
        {
            if (args.Parameters.Length != 0)
            {
                throw new ArgumentException(
                          $"PI function takes no arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            args.Result = Math.PI;
        }
Esempio n. 21
0
        public int Invoke(ref CSharpDeveloperMixin instance, IFunctionArgs <int, int, int, int> args)
        {
            var i = args.Arg1;
            var k = args.Arg3;

            args.ReturnValue = instance.Code(ref i, args.Arg2, ref k);

            args.Arg1 = i;
            args.Arg3 = k;

            return(args.ReturnValue);
        }
Esempio n. 22
0
        public int Invoke2(ref CSharpDeveloperMixin2 instance, IFunctionArgs <int, int, int, int> args)
        {
            //var i = args.Arg1;
            //var k = args.Arg3;

            args.ReturnValue = instance.Code(args.Arg1, args.Arg2, args.Arg3);

            //args.Arg1 = i;
            //            args.Arg3 = k;

            return(args.ReturnValue);
        }
Esempio n. 23
0
        public int Proceed(ref CSharpDeveloperMixin instance, IFunctionArgs <int, int, int, int> args)
        {
            var arg1 = args.Arg1;
            var arg3 = args.Arg3;

            args.ReturnValue = instance.Code(ref arg1, args.Arg2, ref arg3);

            args.Arg1 = arg1;
            args.Arg3 = arg3;

            return(args.ReturnValue);
        }
Esempio n. 24
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            if (args.Parameters.Length != 1 && args.Parameters.Length != 2)
            {
                throw new ArgumentException(
                          $"WEIGHTED function takes 1 or 2 arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            var arg1 = args.Parameters[0].Evaluate();

            if (arg1 is ErrorValue)
            {
                args.Result = arg1;
                return;
            }
            var arg2 = new object();

            if (args.Parameters.Length == 2)
            {
                arg2 = args.Parameters[1].Evaluate();
                if (arg2 is ErrorValue)
                {
                    args.Result = arg2;
                    return;
                }
            }
            else
            {
                arg2 = 1;
            }

            var val1 = Conversion.ToDoubleOrErrorValue(arg1);

            if (val1 is ErrorValue)
            {
                args.Result = val1;
                return;
            }

            var val2 = Conversion.ToDoubleOrErrorValue(arg2);

            if (val2 is ErrorValue)
            {
                args.Result = val2;
                return;
            }

            args.Result = (double)val1 * (double)val2;
        }
Esempio n. 25
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: PI()
            // https://support.office.com/en-gb/article/PI-function-264199d0-a3ba-46b8-975a-c4a04608989b

            if (args.Parameters.Length != 0)
            {
                throw new ArgumentException(
                          $"PI function takes no arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            args.Result = Math.PI;
        }
Esempio n. 26
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: MEDIAN(number1, [number2], ...)
            // https://support.office.com/en-gb/article/median-function-d0916313-4753-414c-8537-ce85bdd967d2

            if (args.Parameters.Length < 1)
            {
                throw new ArgumentException(
                          $"MEDIAN function takes at least 1 arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            var numbers = new List <double>();

            foreach (var argsParameter in args.Parameters)
            {
                var arg = argsParameter.Evaluate();
                if (arg is ErrorValue)
                {
                    args.Result = arg;
                    return;
                }

                var val = Conversion.ToDoubleOrErrorValue(arg);
                if (val is ErrorValue)
                {
                    args.Result = val;
                    return;
                }

                numbers.Add((double)val);
            }

            double F(double[] assessments)
            {
                Array.Sort(assessments);

                var arrayLength = assessments.Length;

                if (arrayLength % 2 == 0)
                {
                    return((assessments[(arrayLength / 2) - 1] + assessments[arrayLength / 2]) / 2);
                }

                return(assessments[(int)Math.Floor((double)arrayLength / 2)]);
            }

            args.Result = F(numbers.ToArray());
        }
Esempio n. 27
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: ROUNDDOWN(number, num_digits)
            // https://support.office.com/en-gb/article/ROUNDDOWN-function-2ec94c73-241f-4b01-8c6f-17e6d7968f53

            if (args.Parameters.Length != 2)
            {
                throw new ArgumentException("ROUNDDOWN function takes 2 arguments, " +
                                            $"got {args.Parameters.Length}");
            }

            var arg1 = args.Parameters[0].Evaluate();

            if (arg1 is ErrorValue)
            {
                args.Result = arg1;
                return;
            }

            var arg2 = args.Parameters[1].Evaluate();

            if (arg2 is ErrorValue)
            {
                args.Result = arg2;
                return;
            }

            var val = Conversion.ToDoubleOrErrorValue(arg1);

            if (val is ErrorValue)
            {
                args.Result = val;
                return;
            }

            var negative = (double)val < 0;

            // This code snippet based on example at http://stackoverflow.com/a/13483008
            var @decimal = new decimal(Math.Abs((double)val));
            var places   = Convert.ToInt32(arg2);
            var factor   = Round.RoundFactor(places);

            @decimal *= factor;
            @decimal  = Math.Floor(@decimal);
            @decimal /= factor;

            args.Result = Convert.ToDouble(negative ? -@decimal : @decimal);
        }
Esempio n. 28
0
        public void Function(IFunctionArgs args, IExcelCaller caller)
        {
            // Syntax: POWER(number, power)
            // https://support.office.com/en-gb/article/POWER-function-d3f2908b-56f4-4c3f-895a-07fb519c362a

            if (args.Parameters.Length != 2)
            {
                throw new ArgumentException(
                          $"POWER function takes 2 arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            var arg1 = args.Parameters[0].Evaluate();

            if (arg1 is ErrorValue)
            {
                args.Result = arg1;
                return;
            }

            var arg2 = args.Parameters[1].Evaluate();

            if (arg2 is ErrorValue)
            {
                args.Result = arg2;
                return;
            }

            var val1 = Conversion.ToDoubleOrErrorValue(arg1);

            if (val1 is ErrorValue)
            {
                args.Result = val1;
                return;
            }

            var val2 = Conversion.ToDoubleOrErrorValue(arg2);

            if (val2 is ErrorValue)
            {
                args.Result = val2;
                return;
            }

            args.Result = Math.Pow((double)val1, (double)val2);
        }
Esempio n. 29
0
        private static void RoundDownFunction(IFunctionArgs args, IExcelCaller caller)
        {
            if (args.Parameters.Length != 2)
            {
                throw new ArgumentException("ROUNDDOWN function takes 2 arguments, " +
                                            $"got {args.Parameters.Length}");
            }

            var arg1 = args.Parameters[0].Evaluate();

            if (arg1 is ErrorValue)
            {
                args.Result = arg1;
                return;
            }

            var arg2 = args.Parameters[1].Evaluate();

            if (arg2 is ErrorValue)
            {
                args.Result = arg2;
                return;
            }

            var val = Conversion.ToDoubleOrErrorValue(arg1);

            if (val is ErrorValue)
            {
                args.Result = val;
                return;
            }

            var negative = (double)val < 0;

            // This code snippet based on example at http://stackoverflow.com/a/13483008
            var @decimal = new decimal(Math.Abs((double)val));
            var places   = Convert.ToInt32(arg2);
            var factor   = RoundFactor(places);

            @decimal *= factor;
            @decimal  = Math.Floor(@decimal);
            @decimal /= factor;

            args.Result = Convert.ToDouble(negative ? -@decimal : @decimal);
        }
Esempio n. 30
0
        private static void RangeFunction(IFunctionArgs args, IExcelCaller caller)
        {
            if (args.Parameters.Length != 2)
            {
                throw new ArgumentException(
                          $"RANGE function takes 2 arguments, got {args.Parameters.Length}",
                          nameof(args));
            }

            var arg1 = args.Parameters[0].Evaluate();

            if (arg1 is ErrorValue)
            {
                args.Result = arg1;
                return;
            }

            var arg2 = args.Parameters[1].Evaluate();

            if (arg2 is ErrorValue)
            {
                args.Result = arg2;
                return;
            }

            var val1 = Conversion.ToDoubleOrErrorValue(arg1);

            if (val1 is ErrorValue)
            {
                args.Result = val1;
                return;
            }

            var val2 = Conversion.ToDoubleOrErrorValue(arg2);

            if (val2 is ErrorValue)
            {
                args.Result = val2;
                return;
            }

            args.Result = (double)val2 - (double)val1;
        }