Пример #1
0
        /// <summary>
        /// Here is where each cell that's changed essentially has a delegate that notifies its respective event has changed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Text")
            {
                BasicCell temporary = sender as BasicCell;
                string    cellname  = temporary.GetCellName();
                DeleteDependency(cellname);
                if (temporary.Text != "" && temporary.Text[0] == '=' && temporary.Text.Length > 1)
                {
                    ExpressionTree tree = new ExpressionTree(temporary.Text.Substring(1));
                    SetDependency(cellname, tree.GetVariables());
                }

                EvaluateCell(sender as Cell);
            }
            else if (e.PropertyName == "BGColor")
            {
                CellPropertyChanged(sender, new PropertyChangedEventArgs("BGColor"));
            }
        }
Пример #2
0
        /// <summary>
        /// Function used to evaluate all the cells.
        /// LOGIC LIVES HERE COULDN'T ENCAPSULATE FURTHER
        /// </summary>
        /// <param name="cell"></param>
        private void EvaluateCell(Cell cell)
        {
            BasicCell evalCell = cell as BasicCell;
            bool      error    = false;

            if (string.IsNullOrWhiteSpace(evalCell.Text))
            {
                evalCell.setValue("");
                CellPropertyChanged(cell, new PropertyChangedEventArgs("Value"));
            }

            else if (evalCell.Text.Length > 1 && evalCell.Text[0] == '=')
            {
                string text = evalCell.Text.Substring(1);

                ExpressionTree evalTree  = new ExpressionTree(text);
                string[]       variables = evalTree.GetVariables();

                foreach (string variableName in variables)
                {
                    if (GetCell(variableName) == null)
                    {
                        CellPropertyChanged(cell, new PropertyChangedEventArgs("Value"));
                        error = true;
                        break;
                    }

                    Cell   variableCell = GetCell(variableName);
                    double variableValue;

                    if (string.IsNullOrEmpty(variableCell.Value))
                    {
                        evalTree.SetVariable(variableName, 0);
                    }
                    else if (!double.TryParse(variableCell.Value, out variableValue))
                    {
                        evalTree.SetVariable(variableName, 0);
                    }
                    else
                    {
                        evalTree.SetVariable(variableName, variableValue);
                    }

                    string cellToEval = evalCell.ColumnIndex.ToString() + evalCell.RowIndex.ToString();
                    if (variableName == cellToEval)
                    {
                        evalCell.setValue("!(Self Reference)");
                        CellPropertyChanged(cell, new PropertyChangedEventArgs("Value"));

                        error = true;
                        break;
                    }
                }

                if (error)
                {
                    return;
                }

                foreach (string variableName in variables)
                {
                    if (IsCircularReference(variableName, evalCell.GetCellName()))
                    {
                        evalCell.setValue("!(Circular Reference!)");
                        CellPropertyChanged(cell, new PropertyChangedEventArgs("Value"));

                        error = true;
                        break;
                    }
                }

                if (error)
                {
                    return;
                }
                evalCell.setValue(evalTree.Evaluate().ToString());
                CellPropertyChanged(cell, new PropertyChangedEventArgs("Value"));
            }

            else
            {
                evalCell.setValue(evalCell.Text);
                CellPropertyChanged(cell, new PropertyChangedEventArgs("Value"));
            }

            string cellName = evalCell.GetCellName();

            if (dependencyDict.ContainsKey(cellName))
            {
                foreach (string dependentCell in dependencyDict[cellName])
                {
                    EvaluateCell(GetCell(dependentCell));
                }
            }
        }