/// <summary> /// Creates a new controller that is based off of the passed in window and contains all of /// the data stored in the passed in file. Error checking will have been done by a previous /// controller, so the passed in file will always be readable. /// </summary> /// <param name="_window">The GUI that the controller can interact with</param> /// <param name="filename">The name of the file containing the data to be read</param> public Controller(ISpreadsheetView _window, string filename) : this(_window) { int col, row; Regex r = new Regex(@"^[a-zA-Z][1-9][0-9]?$"); using (StreamReader sw = new StreamReader(filename)) { spreadsheet = new Spreadsheet(sw, r); } foreach (string cell in spreadsheet.GetNamesOfAllNonemptyCells()) { toCoordinates(cell, out col, out row); window.SetValue(col, row, spreadsheet.GetCellValue(cell).ToString()); } window.SetTitle(filename); HandleNewCellSelected(); }
/// <summary> /// Handles getting the contents of a cell /// </summary> private void HandleContent(String content) { //Get row and column locations. int col, row; char colLoc; String cellName; window.GetSelection(out col, out row); //We need to add 65 to column (in ASCII A = 65), and 1 to the row for correct //indicies. col = col + 65; row = row + 1; colLoc = (char)col; //Concat the row and col to a string cellName = colLoc + "" + row; try { ISet <String> cellsToCalc; String parsedContent; parsedContent = content.Substring(1, content.Length - 1); if (double.TryParse(parsedContent, out double result)) { cellsToCalc = model.SetContentsOfCell(cellName, parsedContent); } else { cellsToCalc = model.SetContentsOfCell(cellName, content); } //Get all cells that need recalculated Regex r = new Regex(@"([a-zA-Z]+)(\d+)"); foreach (String cell in cellsToCalc) { //Separate the cell name so it can be used to update values. Match m = r.Match(cell); string charString = m.Groups[1].Value; string numString = m.Groups[2].Value; //Now indicies on the grid both need to be parsed to a char or int. char parsedChar; int parsedNum; char.TryParse(charString, out parsedChar); int.TryParse(numString, out parsedNum); //Temp variables for row, column and cell value; int tempCol, tempRow; tempCol = parsedChar - 65; tempRow = parsedNum - 1; object tempValue = model.GetCellValue(cell); //Set the value of the Cell if (model.GetCellValue(cell).ToString().Equals("SS.FormulaError")) { window.SetValue(tempCol, tempRow, "Formula Error"); } else { window.SetValue(tempCol, tempRow, tempValue.ToString()); } } } catch (Exception) { MessageBox.Show("Failed"); } //TODO }