Exemplo n.º 1
0
 /// <summary>
 /// If name is null or invalid, throws an InvalidNameException.
 ///
 /// Otherwise, returns the contents (as opposed to the value) of the named cell.  The return
 /// value should be either a string, a double, or a Formula.
 /// </summary>
 public override object GetCellContents(string name)
 {
     if (Cell.IsValidName(name) && cells.ContainsKey(name))
     {
         return(cells[name].content);
     }
     else
     {
         throw new InvalidNameException();
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// If the 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.  (No change is made to the spreadsheet.)
        ///
        /// Otherwise, the contents of the named cell becomes formula.  The method returns a
        /// Set 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
        /// set {A1, B1, C1} is returned.
        /// </summary>
        private ISet <string> SetContents(string name, object content)
        {
            // Make sure the content is not null
            if (content == null)
            {
                throw new ArgumentNullException("Make sure the formula is not null.");
            }

            if (name == null || !Cell.IsValidName(name))
            {
                throw new InvalidNameException();
            }

            // Every time we set the contents, remove all of the dependents and dependees for this name.
            cellRelations.ReplaceDependents(name, new List <string>());

            // Set the value to the content. Value will change if content is a formula.
            object value = content;


            // Now, handle formula-specific actions
            if (content is Formula)
            {
                // First make sure that adding this new content would not create a circular dependency.
                Formula contentAsFormula = (Formula)content;
                checkForCircularDependency(name, contentAsFormula);

                // Add in all of the dependencies for this cell
                List <string> variablesInFormula = contentAsFormula.GetVariables().ToList();
                foreach (string v in variablesInFormula)
                {
                    cellRelations.AddDependency(name, v);
                }

                // Get the value and content prepped for cell creation
                value   = contentAsFormula.Evaluate(lookup);
                content = contentAsFormula.ToString();
            }


            // If the cell already exists, just change the content and value.
            if (cells.ContainsKey(name))
            {
                // If they are setting the content to "", remove the cell.
                if (value.ToString().Equals(""))
                {
                    cells.Remove(name);
                }
                else
                {
                    cells[name].content = content;
                    cells[name].value   = value;
                }
            }
            // Otherwise, create a new cell object with the proper name, content, and value
            // This will also check that the name of the cell is valid or not.
            else
            {
                Cell cellForName = new Cell(name, content, value);
                cells.Add(name, cellForName);
            }

            // Get all of the dependees for the named cell and return them.
            HashSet <string> dependsOnNamedCell = new HashSet <string>()
            {
                name
            };
            List <string> namedCellDependees = GetCellsToRecalculate(name).ToList();

            foreach (string dep in namedCellDependees)
            {
                dependsOnNamedCell.Add(dep);
            }
            return(dependsOnNamedCell);
        }