Exemple #1
0
        /// <summary>
        ///  This method will decide what course of action will need to be taken to
        ///  re-evaluate the odified expression.
        /// </summary>
        /// <param name="targetCell"></param>
        private void HandleCellExpressionChange(SubCell targetCell)
        {
            string text = targetCell.Text;

            /*EVALTUATE EXPRESSION*/
            if (text.Length == 0)
            {
                targetCell.SetValue("");
            }
            else if (text[0] != '=')
            {
                //just set text as normal
                targetCell.SetValue(targetCell.Text);
            }
            else  //text.StartsWith("=") therefore, has cell ref
            {
                bool result;
                try
                {
                    EvaluateExpression(targetCell);       //try to evaluate expression
                }
                catch
                {
                    targetCell.SetValue("#REF!");         //set the value to empty string
                }

                result = VerifyNoCircleRef(targetCell);
                if (result == true)
                {
                    targetCell.SetValue("#CIRC REF!");         //set the value to empty string
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// evaluate the the expression of the target cell. Find and set the value of each variable in the expression,
        /// then set the target cells dependencies. Finally, set the value of the target cell to the result of the
        /// evaluated expression tree.
        /// </summary>
        /// <param name="targetCell"></param>
        private void EvaluateExpression(SubCell targetCell)
        {
            string         targetExpression = targetCell.Text.Substring(1);         //grab entire expression except "="
            ExpressionTree expressionTree   = new ExpressionTree(targetExpression); //create expression tree

            try
            {
                if (expressionTree.Variables.Count == 1)
                {
                    string  variable      = expressionTree.Variables.First();
                    int[]   index         = ConvertNameToLocation(variable);
                    SubCell dependentCell = GetCell(index[0], index[1]); //get cell at name location


                    if (dependencies.ContainsKey(targetCell) == false)
                    {
                        dependencies.Add(targetCell, new HashSet <SubCell>()); //create dependency Cell entry
                    }
                    dependencies[targetCell].Add(dependentCell);               //add dependent cell hash value at key entry

                    if (dependentCell.Value == dependentCell.Text)
                    {
                        targetCell.SetValue(dependentCell.Text);
                    }
                    if (VerifyNoCircleRef(targetCell))
                    {
                        return;
                    }
                }
                else
                {
                    foreach (string variable in expressionTree.Variables)
                    {
                        int[]   index         = ConvertNameToLocation(variable);
                        SubCell dependentCell = GetCell(index[0], index[1]); //get cell at name location


                        if (dependencies.ContainsKey(targetCell) == false)
                        {
                            dependencies.Add(targetCell, new HashSet <SubCell>()); //create dependency Cell entry
                        }
                        dependencies[targetCell].Add(dependentCell);               //add dependent cell hash value at key entry

                        //if the dependent cell value is a constant (double)
                        double result;
                        if (double.TryParse(dependentCell.Value, out result))
                        {
                            expressionTree.SetVariable(variable, result);
                        }
                    }
                    //evaluate the expression tree, and set target cell value
                    targetCell.SetValue(expressionTree.Evaluate().ToString());
                }
            }
            catch
            {
                throw; //throw the exception to HandleCellExpressionChange()
            }
        }