Esempio n. 1
0
            /// <summary>
            /// evalue the cell.
            /// </summary>
            /// <param name="input">input location.</param>
            /// <returns>the value.</returns>
            public override string Evaluate(string input)
            {
                ExpressionTree exp     = new ExpressionTree(input);
                string         pattern = @"[A-Z](?:50|[1-4][0-9]|[1-9])";

                SpreadsheetCell target;

                foreach (Match m in Regex.Matches(input, pattern))
                {
                    try
                    {
                        target = this.parentSpreadsheet.GetCell(m.Value);

                        exp.SetVariable(m.Value, Convert.ToDouble(target.Value));

                        target.PropertyChanged += (sender, e) => this.NotifyPropertyChanged(nameof(this.Value));
                        target.PropertyChanged += this.RefrencePropertyChanged;
                    }
                    catch
                    {
                        return("!CellNotDefined");
                    }
                }

                return(exp.Evaluate().ToString());
            }
Esempio n. 2
0
        /// <summary>
        /// envent to change cell.
        /// </summary>
        /// <param name="sender">sender.</param>
        /// <param name="e">e.</param>
        private void CellPropertyChanges(object sender, PropertyChangedEventArgs e)
        {
            SpreadsheetCell spreadsheet = sender as SpreadsheetCell;

            Dictionary <int, char> alp = new Dictionary <int, char>();
            int count = 0;

            for (char i = 'A'; i <= 'Z'; i++)
            {
                alp[count] = i;
                count++;
            }

            string changeCell = alp[spreadsheet.ColumIndex].ToString() + (spreadsheet.RowIndex + 1).ToString();

            if (e.PropertyName == "Text")
            {
                if (spreadsheet.Text.StartsWith("=") == false)
                {
                    spreadsheet.Value = spreadsheet.Text;
                }
                else
                {
                    // if equal to the expreesion which need to cal by expression tree.
                    if (spreadsheet.Text.Length > 3)
                    {
                        string         expression = spreadsheet.Text.Substring(1);
                        ExpressionTree tree       = new ExpressionTree(expression);

                        List <string> strlist = tree.GetVaribles(expression);
                        foreach (string var in strlist)
                        {
                            SpreadsheetCell cell = this.GetSingleValue(var);
                            if (cell != null)
                            {
                                if (this.GetSingleValue(changeCell) == this.GetSingleValue(var))
                                {
                                    spreadsheet.Value = "!self reference";
                                }
                                else if (this.GetSingleValue(var).Value != "!self reference")
                                {
                                    double value = Convert.ToDouble(this.GetSingleValue(var).Value);
                                    tree.SetVariable(var, value);
                                    spreadsheet.Value = tree.Evaluate().ToString();
                                }
                                else
                                {
                                    spreadsheet.Value = Convert.ToString(0);
                                }

                                cell.PropertyChanged += spreadsheet.CellPropertyChanged;
                            }
                            else
                            {
                                spreadsheet.Value = "!bad reference";
                            }
                        }
                    }

                    // if = like B1,A1 just single cell value.
                    else
                    {
                        string          name = spreadsheet.Text.Substring(1);
                        SpreadsheetCell cell = this.GetSingleValue(name);

                        // cell will be null if catch FormatException case which return to null,if not, just normall to get value else will be bad reference.
                        if (cell != null)
                        {
                            // if equal self cell value
                            if (this.GetSingleValue(changeCell) == this.GetSingleValue(name))
                            {
                                spreadsheet.Value = "!self reference";
                            }

                            // if equal the cell value which is no self reference, else is equal zero.
                            else if (this.GetSingleValue(name).Value != "!self reference")
                            {
                                spreadsheet.Value = this.GetSingleValue(name).Value;
                            }
                            else
                            {
                                spreadsheet.Value = Convert.ToString(0);
                            }

                            cell.PropertyChanged += spreadsheet.CellPropertyChanged;
                        }
                        else
                        {
                            spreadsheet.Value = "!bad reference";
                        }
                    }
                }
            }

            this.CellPropertyChanged?.Invoke(sender, new PropertyChangedEventArgs("Value"));
        }