public IMathSpreadsheet Read() { var spreadsheet = new MathSpreadsheet(new Parser(), new Tokenizer()); System.Console.WriteLine("Load spreadsheet from file."); using (var sIn = new StreamReader(new FileStream(File, FileMode.Open))) { var(columnsCount, rowsCount) = ParseHeader(sIn.ReadLine()); spreadsheet.SetSize(columnsCount, rowsCount); for (var rowNumber = 1; rowNumber <= rowsCount; rowNumber++) { for (var columnNumber = 1; columnNumber <= columnsCount; columnNumber++) { var cellValue = sIn.ReadLine(); spreadsheet.SetValue(columnNumber, rowNumber, cellValue); } } } return(spreadsheet); }
public IMathSpreadsheet Read() { var spreadsheet = new MathSpreadsheet(new Parser(), new Tokenizer()); System.Console.WriteLine("Please provide spreadsheet size."); System.Console.Write("Columns count: "); if (!int.TryParse(System.Console.ReadLine(), out var columnsCount)) { throw new SpreadsheetFormatException("Invalid columns count."); } System.Console.Write("Rows count: "); if (!int.TryParse(System.Console.ReadLine(), out var rowsCount)) { throw new SpreadsheetFormatException("Invalid rows count"); } var showInteractiveInput = columnsCount <= ColumnsThreshold && rowsCount <= RowsThreshold; if (!showInteractiveInput) { System.Console.WriteLine($"Spreadsheet size exceed {ColumnsThreshold} x {RowsThreshold} interactive output will be hidden because spreadsheet will not feet in console."); } spreadsheet.SetSize(columnsCount, rowsCount); System.Console.WriteLine(string.Empty); var left = System.Console.CursorLeft; var top = System.Console.CursorTop; for (var rowNumber = 1; rowNumber <= rowsCount; rowNumber++) { for (var columnNumber = 1; columnNumber <= columnsCount; columnNumber++) { if (showInteractiveInput) { CleanConsole(left, top); System.Console.WriteLine(Print.PrintSpreadsheet(spreadsheet, true)); } System.Console.Write($"Please enter { CellPosition.CoordinatesToKey(columnNumber, rowNumber) }: "); var cellValue = System.Console.ReadLine(); spreadsheet.SetValue(columnNumber, rowNumber, cellValue); } } if (showInteractiveInput) { CleanConsole(left, top); System.Console.WriteLine(Print.PrintSpreadsheet(spreadsheet, true)); } return(spreadsheet); }
public void Calculate_SpreadsheetWithoutCellReferences_AllCellsCalculated() { _spreadsheet.SetSize(2, 3); for (var row = 1; row <= _spreadsheet.RowsCount; row++) { for (var col = 1; col <= _spreadsheet.ColumnsCount; col++) { _spreadsheet.SetValue(col, row, $"{col} + {row}"); } } _spreadsheet.Calculate(); for (var row = 1; row <= _spreadsheet.RowsCount; row++) { for (var col = 1; col <= _spreadsheet.ColumnsCount; col++) { var cell = _spreadsheet.GetValue(col, row); Assert.Equal(col + row, int.Parse(cell.ResultValue)); } } }
public void SetCell_CellIsOutOfSpreadsheetBoundaries_ThrowIndexOutOfRangeException(int columnNumber, int rowNumber) { _spreadsheet.SetSize(1, 1); Assert.Throws <IndexOutOfRangeException>(() => _spreadsheet.SetValue(rowNumber, columnNumber, "x")); }