Exemple #1
0
        /// <summary>
        /// If text is null, throws an ArgumentNullException.
        ///
        /// Otherwise, if name is null or invalid, throws an InvalidNameException.
        ///
        /// Otherwise, the contents of the named cell becomes text.  The method returns a
        /// list consisting of name plus the names of all other cells whose value depends,
        /// directly or indirectly, on the named cell.
        ///
        /// For example, if name is A1, B1 contains A1*2, and C1 contains B1+A1, the
        /// list {A1, B1, C1} is returned.
        /// </summary>
        protected override IList <string> SetCellContents(string name, string text)
        {
            //Normalizes the name
            String normalizedName = name;

            //Creates a new Cell, with the name and text being passed
            Cell c = new Cell(normalizedName, text);

            //Sets the contents and value of the cell
            c.SetContents(text);
            c.SetValue(Lookup);

            //If the dictionary contains the key name, then it goes here
            if (dictionary.ContainsKey(normalizedName))
            {
                //Sets the value associated with the name in the dictionary with the new Cell
                dictionary[normalizedName] = c;
                //If the contents of the cell is empty, then it removes it from the dictionary
                if ((string)c.GetContents() == "")
                {
                    dictionary.Remove(normalizedName);
                }
                //Replaces the dependees of the name
                graph.ReplaceDependees(normalizedName, new HashSet <string>());
            }
            else
            {
                //If the contents of the cell is not null, then it adds the cell to the dictionary
                if ((string)c.GetContents() != "")
                {
                    dictionary.Add(normalizedName, c);
                }

                //Replaces the dependees of the name
                graph.ReplaceDependees(normalizedName, new HashSet <string>());
            }

            //Gets the orders of the cell recalculated and stores it into a list
            List <string> recalculatedList = new List <string>(GetCellsToRecalculate(normalizedName));

            //Recalculates the values of all the cells
            RecalculateValues(recalculatedList);

            //Sets changed to true
            Changed = true;

            //Returns the list of cells recalculated
            return(recalculatedList);
        }
Exemple #2
0
        /// <summary>
        /// If name is null or invalid, throws an InvalidNameException.
        ///
        /// Otherwise, the contents of the named cell becomes number.  The method returns a
        /// list consisting of name plus the names of all other cells whose value depends,
        /// directly or indirectly, on the named cell.
        ///
        /// For example, if name is A1, B1 contains A1*2, and C1 contains B1+A1, the
        /// list {A1, B1, C1} is returned.
        /// </summary>
        protected override IList <string> SetCellContents(string name, double number)
        {
            //Normalizes the name
            String normalizedName = Normalize(name);
            //Creates a new cell, with the name and number being passed
            Cell c = new Cell(normalizedName, number);

            //Sets the contents of the cell with the number
            c.SetContents(number);

            //Sets the value of the cell which should be the number
            c.SetValue(Lookup);

            //If the dictionary contains the key name, then it goes here
            if (dictionary.ContainsKey(normalizedName))
            {
                //Sets the value associated with the name in the dictionary with the new Cell
                dictionary[normalizedName] = c;

                //Replaces the dependees of the name, with a new HashSet
                graph.ReplaceDependees(normalizedName, new HashSet <string>());
            }
            else
            {
                //Adds the cell to the dictionary
                dictionary.Add(normalizedName, c);

                //Replaces the dependees of the name, with a new HashSet
                graph.ReplaceDependees(normalizedName, new HashSet <string>());
            }

            //Gets the orders of the cell recalculated and stores it into a list
            List <string> recalculatedList = new List <string>(GetCellsToRecalculate(normalizedName));

            //Recalculates the values of all the cells
            RecalculateValues(recalculatedList);

            //Sets changed to true
            Changed = true;

            //Returns the list of cells recalculated
            return(recalculatedList);
        }
Exemple #3
0
        /// <summary>
        /// If the formula parameter is null, throws an ArgumentNullException.
        ///
        /// Otherwise, if name is null or invalid, throws an InvalidNameException.
        ///
        /// Otherwise, if changing the contents of the named cell to be the formula would cause a
        /// circular dependency, throws a CircularException, and no change is made to the spreadsheet.
        ///
        /// Otherwise, the contents of the named cell becomes formula.  The method returns a
        /// list consisting of name plus the names of all other cells whose value depends,
        /// directly or indirectly, on the named cell.
        ///
        /// For example, if name is A1, B1 contains A1*2, and C1 contains B1+A1, the
        /// list {A1, B1, C1} is returned.
        /// </summary>
        protected override IList <string> SetCellContents(string name, Formula formula)
        {
            //Normalizes the name of the Cell
            String normalizedName = Normalize(name);


            //Creates a new Cell passing the name and formula
            Cell c = new Cell(normalizedName, formula);

            //Sets the contents of the cell with the formula
            c.SetContents(formula);
            //Sets the value of the Cell which should be a double if the formula is valid
            //or a FormulaError if the formula is invalid
            c.SetValue(Lookup);

            //A boolean to keep track if a new Entry is made
            bool newEntry = false;
            //Gets the previous dependees of the cell before
            HashSet <string> dependees = new HashSet <string>();
            //A Cell to store the prevCell
            Cell prevCell = null;

            //If the dictionary contains the key name, then goes here
            if (dictionary.ContainsKey(normalizedName))
            {
                //Gets the prevCell
                prevCell = dictionary[normalizedName];
                //Replaces it with the new cell
                dictionary[normalizedName] = c;

                //Gets the prevDependees of the graph
                dependees = new HashSet <string>(graph.GetDependees(normalizedName));
                //Replaces the dependees with the variables of the formula
                graph.ReplaceDependees(normalizedName, formula.GetVariables());
            }
            else
            {
                //Adds the cell to the dictionary
                dictionary.Add(normalizedName, c);

                //Gets the prevDependees of the graph
                dependees = new HashSet <string>(graph.GetDependees(normalizedName));

                //Replaces the dependees with the variables of the formula
                graph.ReplaceDependees(normalizedName, formula.GetVariables());

                //Sets the newEntry boolean to true
                newEntry = true;
            }

            //Creates a new list to be returned
            List <string> recalculatedList;

            try
            {
                //Gets the orders of the cell recalculated and stores it into a list
                recalculatedList = new List <string>(GetCellsToRecalculate(normalizedName));

                //Recalculates the values of all the cells
                RecalculateValues(recalculatedList);
            }
            //Goes in here if we encounter a CircularException
            catch (CircularException ce)
            {
                //If a new Cell was added, we remove it from the dictionary
                if (newEntry == true)
                {
                    dictionary.Remove(normalizedName);
                }
                //Otherwise, we revert the value set to the value before in the Cell
                else
                {
                    dictionary[normalizedName] = prevCell;
                }
                //Replace the dependees of the name to it's original form
                graph.ReplaceDependees(normalizedName, dependees);

                //Throws a CircularException
                throw new CircularException();
            }


            //Sets changed to true
            Changed = true;

            //Returns the list of cells recalculated
            return(recalculatedList);
        }