W tym przykładzie zastosowano iterator, który może przechodzić od A
do ZZ
.
public static IEnumerable<string> GetColumns()
{
string s = null;
for (char c2 = 'A'; c2 <= 'Z' + 1; c2++)
{
for (char c = 'A'; c <= 'Z'; c++)
{
yield return s + c;
}
s = c2.ToString();
}
}
Próbka ta rozpoczyna się A1
i przechodzi AA1
string currentCell = "A1";
int currentRow = int.Parse(Regex.Match(currentCell, @"\d+").Value);
string currentCol = Regex.Match(currentCell, @"[A-Z]+").Value;
foreach (string column in GetColumns().Where (c => c >= currentCol && currentCol <= "AA"))
{
Console.WriteLine (column + currentRow);
}
Próbka ta rozpoczyna się C5
wylicza i przez następne 26 kolumn.
int columnsToAdd = 26;
currentCell = "C5";
currentRow = int.Parse(Regex.Match(currentCell, @"\d+").Value);
currentCol = Regex.Match(currentCell, @"[A-Z]+").Value;
foreach (string column in GetColumns().Where (c => c >= currentCol))
{
if (columnsToAdd--) == 0)
break;
Console.WriteLine (column + currentRow);
}
Czy znalazłeś już jakiś kod? – BoltClock
To powinna być dość prosta struktura danych, a już znasz funkcje, które chcesz wykonać ... – crashmstr
Nieco powiązane pytanie [tutaj] (https://stackoverflow.com/q/2208688/465053). – RBT