Example #1
0
        /// <summary>
        /// Event handler for cell text property change.
        /// </summary>
        /// <param name="sender">The cell that been changed.</param>
        private void TextChangeHandler(object sender)
        {
            // cell that been changed in UI.
            BaseCell spreadSheetCell = sender as BaseCell;

            // the spreadsheet cell that corresponds to the changed cell.
            string newFormula = spreadSheetCell.Text;

            if (!string.IsNullOrEmpty(newFormula))
            {
                newFormula = Regex.Replace(newFormula, @"s", string.Empty);
            }

            ExpressionTree expTree;

            if (string.IsNullOrEmpty(newFormula))
            {
                spreadSheetCell.ChangeValue(string.Empty);
            }
            else if (newFormula[0] != '=')
            {
                // if the new formula is a  value
                // change the cell value to changed cell text.
                double number;
                string newValue = string.Empty;
                if (double.TryParse(newFormula, out number))
                {
                    // if the new value is a double
                    newValue = newFormula;
                }
                else
                {
                    // if the new value is not a double
                    newValue = "0";
                }

                spreadSheetCell.ChangeValue(newValue);
            }
            else
            {
                // if the new formula is an expression.
                // pass the new formula into the expression tree to evaluate it
                string         expression = newFormula.Substring(1);
                Queue <string> varQueue   = this.GetVarNames(expression);
                expTree = new ExpressionTree(expression);
                this.UpdateExpTree(spreadSheetCell, expTree, varQueue);

                // change cell value to the evaluated result of given expression.
                spreadSheetCell.ChangeValue(expTree.Evaluate().ToString());
            }

            // update cells that subscribe to this cell in spreadsheet
            this.UpdateSubCells(spreadSheetCell);
        }
Example #2
0
 /// <summary>
 /// Update the subscribe cells to given spread sheet cell.
 /// </summary>
 /// <param name="spreadSheetCell">given spreadsheet cell.</param>
 private void UpdateSubCells(BaseCell spreadSheetCell)
 {
     for (int i = 0; i < spreadSheetCell.Subscriber.Count; ++i)
     {
         string         curSubCellIndex = spreadSheetCell.Subscriber[i];
         int            col             = Convert.ToInt32(curSubCellIndex[0]) - 65;
         int            row             = Convert.ToInt32(curSubCellIndex.Substring(1)) - 1;
         BaseCell       curSubCell      = this.spreadSheet[row, col];
         string         exp             = curSubCell.Text.Substring(1);
         Queue <string> varQueue        = this.GetVarNames(exp);
         ExpressionTree expressionTree  = new ExpressionTree(exp);
         this.UpdateExpTree(curSubCell, expressionTree, varQueue);
         curSubCell.ChangeValue(expressionTree.Evaluate().ToString());
     }
 }
Example #3
0
        /// <summary>
        /// Event handler for cell text property change.
        /// </summary>
        /// <param name="sender">The cell that been changed.</param>
        private void TextChangeHandler(object sender)
        {
            // cell that been changed in UI.
            BaseCell spreadSheetCell = sender as BaseCell;

            // the spreadsheet cell that corresponds to the changed cell.
            string newFormula = spreadSheetCell.Text;

            if (!string.IsNullOrEmpty(newFormula))
            {
                newFormula = Regex.Replace(newFormula, @"s", string.Empty);
            }

            ExpressionTree expTree;

            if (string.IsNullOrEmpty(newFormula))
            {
                spreadSheetCell.ChangeValue(string.Empty);
            }
            else if (newFormula[0] != '=')
            {
                // if the new formula is a  value
                // change the cell value to changed cell text.
                double number;
                string newValue = string.Empty;
                if (double.TryParse(newFormula, out number))
                {
                    // if the new value is a double
                    newValue = newFormula;
                }
                else
                {
                    // if the new value is not a double
                    newValue = "0";
                }

                spreadSheetCell.ChangeValue(newValue);
            }
            else
            {
                // if the new formula is an expression.
                // pass the new formula into the expression tree to evaluate it
                string expression = newFormula.Substring(1);

                // if the text reference to cell itself, throw an exception and set text to "circular Reference".
                string spreadSheetCellIndex = Convert.ToChar(spreadSheetCell.ColumnIndex + 65) + (spreadSheetCell.RowIndex + 1).ToString();

                if (expression.Contains(spreadSheetCellIndex))
                {
                    spreadSheetCell.Text = "CircularRef";
                    throw new System.ArgumentException("Circular reference occurs", "text");
                }

                Queue <string> varQueue = this.GetVarNames(expression);
                expTree = new ExpressionTree(expression);
                this.UpdateExpTree(spreadSheetCell, expTree, varQueue);

                // change cell value to the evaluated result of given expression.
                spreadSheetCell.ChangeValue(expTree.Evaluate().ToString());
            }

            // update cells that subscribe to this cell in spreadsheet
            this.UpdateSubCells(spreadSheetCell);
        }
Example #4
0
        /// <summary>
        /// Cell property changed event handler. change of a value of a cell.
        /// </summary>
        /// <param name="sender">Sender of event.</param>
        /// <param name="e">What is changed.</param>
        private void HandleCellPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            // cell that been changed in UI.
            BaseCell uICell = sender as BaseCell;

            // the spreadsheet cell that corresponds to the changed cell.
            BaseCell       spreadSheetCell = this.spreadSheet[uICell.RowIndex, uICell.ColumnIndex];
            string         newFormula      = spreadSheetCell.Text;
            ExpressionTree expTree;

            // remove all whitespace in the input formula:
            newFormula = Regex.Replace(newFormula, @"s", string.Empty);
            if (newFormula[0] != '=')
            {
                // if the newformula is a  value
                // change the cell value to changed cell text.
                double number;
                string newValue = string.Empty;
                if (double.TryParse(newFormula, out number))
                {
                    // if the new value is a double
                    newValue = newFormula;
                }
                else
                {
                    // if the new value is not a double
                    newValue = "0";
                }

                spreadSheetCell.ChangeValue(newValue);

                // refresh subscriber cells
                for (int i = 0; i < spreadSheetCell.Subscriber.Count; ++i)
                {
                    string   curSubCellIndex      = spreadSheetCell.Subscriber[i];
                    int      col                  = Convert.ToInt32(curSubCellIndex[0]) - 65;
                    int      row                  = Convert.ToInt32(curSubCellIndex.Substring(1)) - 1;
                    BaseCell curSubscriber        = this.spreadSheet[row, col];
                    string   spreadSheetCellIndex = Convert.ToChar(spreadSheetCell.ColumnIndex + 65) + Convert.ToString(spreadSheetCell.RowIndex + 1);

                    if (curSubscriber.Text.Contains(spreadSheetCellIndex))
                    {
                        // if spread sheet cell is part of subscriber expression.
                        Queue <string> varQueue = this.GetVarNames(curSubscriber.Text.Substring(1));
                        expTree = new ExpressionTree(curSubscriber.Text.Substring(1));

                        // get the variable name and corresponding value in the spreadsheet
                        this.UpdateExpTree(spreadSheetCell, expTree, ref col, ref row, varQueue);
                        curSubscriber.ChangeValue(expTree.Evaluate().ToString());
                    }
                }
            }
            else
            {
                // if the newformula is an expression.
                // pass the newformula into the expression tree to evaulate it
                string         expression = newFormula.Substring(1);
                Queue <string> varQueue   = this.GetVarNames(expression);
                expTree = new ExpressionTree(expression);
                this.UpdateExpTree(spreadSheetCell, expTree, varQueue);

                // change cell value to the evaluated result of given expression.
                spreadSheetCell.ChangeValue(expTree.Evaluate().ToString());
            }
        }