public static IEnumerable <SpreadsheetCellModel> GetCells(this ISpreadsheetModel model, CellRange range, SpreadsheetCellScanOrder scanOrder = SpreadsheetCellScanOrder.ScanByRows) { var rangeToScan = range.Intersect(model.GetCellRange()); if (scanOrder == SpreadsheetCellScanOrder.ScanByRows) { for (var row = rangeToScan.StartRow; row <= rangeToScan.EndRow; row++) { for (var column = rangeToScan.StartColumn; column <= rangeToScan.EndColumn; column++) { yield return(model.GetCell(row, column)); } } yield break; } if (scanOrder == SpreadsheetCellScanOrder.ScanByColumns) { for (var column = rangeToScan.StartColumn; column <= rangeToScan.EndColumn; column++) { for (var row = rangeToScan.StartRow; row <= rangeToScan.EndRow; row++) { yield return(model.GetCell(row, column)); } } yield break; } throw new InvalidOperationException("Wrong scan order"); }