public static void Register(CalcEngine ce)
 {
     //ce.RegisterFunction("DAVERAGE", 3, Daverage); // Returns the average of selected database entries
     //ce.RegisterFunction("DCOUNT", 1, Dcount); // Counts the cells that contain numbers in a database
     //ce.RegisterFunction("DCOUNTA", 1, Dcounta); // Counts nonblank cells in a database
     //ce.RegisterFunction("DGET", 1, Dget); // Extracts from a database a single record that matches the specified criteria
     //ce.RegisterFunction("DMAX", 1, Dmax); // Returns the maximum value from selected database entries
     //ce.RegisterFunction("DMIN", 1, Dmin); // Returns the minimum value from selected database entries
     //ce.RegisterFunction("DPRODUCT", 1, Dproduct); // Multiplies the values in a particular field of records that match the criteria in a database
     //ce.RegisterFunction("DSTDEV", 1, Dstdev); // Estimates the standard deviation based on a sample of selected database entries
     //ce.RegisterFunction("DSTDEVP", 1, Dstdevp); // Calculates the standard deviation based on the entire population of selected database entries
     //ce.RegisterFunction("DSUM", 1, Dsum); // Adds the numbers in the field column of records in the database that match the criteria
     //ce.RegisterFunction("DVAR", 1, Dvar); // Estimates variance based on a sample from selected database entries
     //ce.RegisterFunction("DVARP", 1, Dvarp); // Calculates variance based on the entire population of selected database entries
 }
 public override MathFunction Interpret()
 {
     var calcEngine = new CalcEngine();
     foreach (var variable in Variables)
     {
         calcEngine.Variables.Add(variable, 0);
     }
     return vals =>
     {
         for (var i = 0; i < vals.Length; i++)
         {
             calcEngine.Variables[Variables[i]] = vals[i];
         }
         return (double) calcEngine.Evaluate(Expression);
     };
 }
        private static List<object> GetArguments(CalcEngine calcEngine, string args)
        {
            var allArgs = calcEngine.SplitArgsPreservingQuotedCommas(args).ToList();
            var finalArgs = new List<object>();

            for (var i = 0; i < allArgs.Count; ++i)
            {
                var arg = allArgs[i];
                calcEngine.AdjustRangeArg(ref arg);
                if (IsRangeArgument(arg))
                {
                    finalArgs.Add((object) GetArguments(calcEngine, string.Join(",", calcEngine.GetCellsFromArgs(arg))).ToArray());
                }
                else
                {
                    finalArgs.Add(calcEngine.ComputeIsRef(arg) == "TRUE" ? calcEngine.GetValueFromArg(arg) : arg);
                }
            }

            return finalArgs;
        }
        public static void Register(CalcEngine ce)
        {
            ce.RegisterFunction("DATE", 3, Date); // Returns the serial number of a particular date
            ce.RegisterFunction("DATEVALUE", 1, Datevalue); // Converts a date in the form of text to a serial number
            ce.RegisterFunction("DAY", 1, Day); // Converts a serial number to a day of the month
            ce.RegisterFunction("DAYS360", 2, 3, Days360); // Calculates the number of days between two dates based on a 360-day year
            ce.RegisterFunction("EDATE", 2, Edate); // Returns the serial number of the date that is the indicated number of months before or after the start date
            ce.RegisterFunction("EOMONTH", 2, Eomonth); // Returns the serial number of the last day of the month before or after a specified number of months
            ce.RegisterFunction("HOUR", 1, Hour); // Converts a serial number to an hour
            ce.RegisterFunction("MINUTE", 1, Minute); // Converts a serial number to a minute
            ce.RegisterFunction("MONTH", 1, Month); // Converts a serial number to a month
            ce.RegisterFunction("NETWORKDAYS", 2, 3, Networkdays); // Returns the number of whole workdays between two dates
            ce.RegisterFunction("NOW", 0, Now); // Returns the serial number of the current date and time
            ce.RegisterFunction("SECOND", 1, Second); // Converts a serial number to a second
            ce.RegisterFunction("TIME", 3, Time); // Returns the serial number of a particular time
            ce.RegisterFunction("TIMEVALUE", 1, Timevalue); // Converts a time in the form of text to a serial number
            ce.RegisterFunction("TODAY", 0, Today); // Returns the serial number of today's date
            ce.RegisterFunction("WEEKDAY", 1, 2, Weekday); // Converts a serial number to a day of the week
            ce.RegisterFunction("WEEKNUM", 1, 2, Weeknum); // Converts a serial number to a number representing where the week falls numerically with a year
            ce.RegisterFunction("WORKDAY", 2, 3, Workday); // Returns the serial number of the date before or after a specified number of workdays
            ce.RegisterFunction("YEAR", 1, Year); // Converts a serial number to a year
            ce.RegisterFunction("YEARFRAC", 2, 3, Yearfrac); // Returns the year fraction representing the number of whole days between start_date and end_date

        }
 public static void RegisterAllMethods(CalcEngine calcEngine)
 {
     WrapAllMethods(calcEngine).ToList().ForEach(x => calcEngine.AddFunction(x.Name,x.LibraryFunction));
 }
 public static IEnumerable<LibraryFunctionEx> WrapAllMethods(CalcEngine calcEngine)
 {
     return FindAllMethods().Select(m => new LibraryFunctionEx() { Name = m.Name, LibraryFunction = WrapMethod(calcEngine,m) } );
 }
        private static CalcEngine.LibraryFunction WrapMethod(CalcEngine calcEngine,MethodInfo method)
        {
            if (method.DeclaringType == null)
                return null;

            var parameters = method.GetParameters().ToList();
            var maxParameterCount = parameters.Count;
            var nonOptionalParameterCount = parameters.Count(p => !p.IsOptional);

            Func<string, string> libFunc = args =>
            {
                var allArgs = GetArguments(calcEngine, args);
                var ret = "Error";
                if (allArgs.Count < nonOptionalParameterCount || allArgs.Count > maxParameterCount)
                    return ret;

                var finalArgs = new List<object>();
                parameters.ForEach(x => finalArgs.Add(Type.Missing) );

                for (var i = 0; i < allArgs.Count; ++i)
                {
                    try
                    {
                        var paramType = parameters[i].ParameterType;
                        if (paramType.IsArray)
                        {
                            finalArgs[i] = CreateArray(allArgs[i], paramType.GetElementType());
                        }
                        else
                        {
                            finalArgs[i] = RemoveCharacter(Convert.ChangeType(allArgs[i], paramType),"\"");
                        }
                    }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
                }


                try
                {
                    ret = (string)method.Invoke(null, finalArgs.ToArray());
                }
                catch (Exception ex)
                {
                    return ex.Message; 
                }

                return ret;
            };

            return libFunc.Invoke;
        }
        private void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            this.AssociatedObject.gridpanel.Children.Add(gridControl1);

            Brush headerBrush = ColorHelper.CreateFrozenSolidColorBrush(128, Colors.CadetBlue);

            gridControl1.Model.HeaderStyle.Background = headerBrush;

            gridControl1.Model.HeaderStyle.Borders.All        = new Pen(Brushes.White, 1);
            gridControl1.Model.TableStyle.Borders.All         = new Pen(Brushes.White, 0);
            gridControl1.Model.HeaderStyle.Font.FontWeight    = FontWeights.Bold;
            gridControl1.Model.TableStyle.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            gridControl1.Model.TableStyle.VerticalAlignment   = System.Windows.VerticalAlignment.Center;
            gridControl1.Model.QueryCellInfo += new GridQueryCellInfoEventHandler(Model_QueryCellInfo);

            this.viewModel.PopulateDataTableValues();

            gridControl1.Model.RowCount    = this.viewModel.dt.Rows.Count + 1;
            gridControl1.Model.ColumnCount = this.viewModel.dt.Columns.Count + 1;
            gridControl1.Model.BaseStylesMap["Row Header"].StyleInfo.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            gridControl1.Model.BaseStylesMap["Row Header"].StyleInfo.VerticalAlignment   = VerticalAlignment.Center;

            CalcEngine.ResetSheetFamilyID();
            this.viewModel.engine = new CalcEngine(gridControl1);
            List <string> formulaNameCollection = GetFunctionNames();

            var dpiXProperty = typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static);
            var dpiYProperty = typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static);

            var dpiX = (int)dpiXProperty.GetValue(null, null);
            var dpiY = (int)dpiYProperty.GetValue(null, null);

            if (dpiX > 100)
            {
                this.AssociatedObject.desc.Height               = 120;
                this.AssociatedObject.sytx.Height               = 60;
                this.AssociatedObject.t1.Width                  = 150;
                this.AssociatedObject.desc.Width                = 300;
                this.AssociatedObject.sytx.Width                = 300;
                this.AssociatedObject.res.Width                 = 300;
                this.AssociatedObject.res.FontSize              = 21;
                this.AssociatedObject.cbx1.Height               = this.AssociatedObject.t1.Height = 30;
                gridControl1.Model.RowHeights.DefaultLineSize   = 35;
                gridControl1.Model.ColumnWidths.DefaultLineSize = 75;
            }
            else
            {
                this.AssociatedObject.desc.Height               = 150;
                this.AssociatedObject.sytx.Height               = 90;
                this.AssociatedObject.t1.Width                  = 220;
                this.AssociatedObject.desc.Width                = 500;
                this.AssociatedObject.sytx.Width                = 500;
                this.AssociatedObject.res.Width                 = 500;
                this.AssociatedObject.cbx1.Width                = 200;
                this.AssociatedObject.cbx1.Height               = this.AssociatedObject.t1.Height = 30;
                this.AssociatedObject.buttonAdv.Height          = 30;
                gridControl1.Model.RowHeights.DefaultLineSize   = 45;
                gridControl1.Model.ColumnWidths.DefaultLineSize = 85;
            }


            this.AssociatedObject.t1.KeyDown               += new KeyEventHandler(t1_KeyDown);
            this.AssociatedObject.tempTextBlock.Text        = "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG";
            this.AssociatedObject.functionCountLable.Text   = "CALCULATE SUPPORTS " + formulaNameCollection.Count.ToString() + " FORMULAS.";
            this.AssociatedObject.cbx1.AutoCompleteSource   = formulaNameCollection;
            this.AssociatedObject.cbx1.SelectedItemChanged += Cbx1_SelectedItemChanged;
            this.AssociatedObject.t1.TextChanged           += new TextChangedEventHandler(t1_TextChanged);
            this.AssociatedObject.cbx1.SelectedItem         = formulaNameCollection[369]; //select of SUM function
            this.AssociatedObject.t1.LostFocus             += new RoutedEventHandler(t1_LostFocus);
            this.AssociatedObject.t1.GotFocus              += new RoutedEventHandler(t1_GotFocus);
        }
 public void Setup()
 {
     engine = new CalcEngine();
 }