/// <summary> /// REQUIREMENT: Variable names must be formatted as: "[column letter][row number]" /// Defines cell variables in the expression tree and subscribes the /// current cell to each variable cell's property changed event. /// </summary> /// <param name="et">Expression tree.</param> /// <param name="currCell">Current spreadsheet cell.</param>\ /// <returns>If there was an error.</returns> private bool HandleCellVariables(ExpressionTree et, SpreadsheetCell currCell) { bool variableError = false; List <string> variableNames = et.GetVariableNames(); foreach (string name in variableNames) { SpreadsheetCell varCell = this.GetCellFromName(name); if (varCell == null) { currCell.SetValue("!(bad reference)"); variableError = true; } else if (varCell == currCell) { currCell.SetValue("!(self reference)"); variableError = true; } else if (this.ContainsCircularReference(currCell, varCell)) { currCell.SetValue("!(circular reference)"); variableError = true; } else { // If the cell's value string cannot be parsed as a double, defaults to 0. double value = 0.0; try { value = double.Parse(varCell.Value); } catch (FormatException) { } et.SetVariable(name, value); currCell.SubToCellChange(varCell); } } return(variableError); }
// Clears out entire spreadsheet public void ClearSheet() { for (int i = 0; i < ColumnCount; i++) { for (int j = 0; j < RowCount; j++) { SpreadsheetCell temp = (SpreadsheetCell)cells[i, j]; temp.Text = null; temp.SetValue(null); } } }
/// <summary> /// Updates (and possibly evaluates) a cell's value. /// </summary> /// <param name="cell">Spreadsheet cell.</param> private void UpdateCellValue(SpreadsheetCell cell) { string newValue = cell.Text; if (newValue.StartsWith("=")) { newValue = this.EvaluateCellExpression(cell).ToString(); } cell.SetValue(newValue); this.CellPropertyChanged?.Invoke(cell, new PropertyChangedEventArgs("Value")); }