Skip to content

Instantly share code, notes, and snippets.

@txusko
Created December 30, 2013 16:22
Show Gist options
  • Save txusko/0b20e07d7f076614e01c to your computer and use it in GitHub Desktop.
Save txusko/0b20e07d7f076614e01c to your computer and use it in GitHub Desktop.
Incrementa el string tcCodigo Ej. string lastCodigo = IncrementXLColumn("AA"); -> Devolvera lastCodigo = "AB";
/// <summary>
/// Incrementa el string tcCodigo
/// Ej. string lastCodigo = IncrementXLColumn("AA"); -> Devolvera lastCodigo = "AB";
/// </summary>
/// <param name="tcCodigo"></param>
/// <returns></returns>
protected static string IncrementXLColumn(string tcCodigo)
{
//var parts = System.Text.RegularExpressions.Regex.Matches(Address, @"([A-Z]+)|(\d+)");
var lParts = System.Text.RegularExpressions.Regex.Matches(tcCodigo, @"([A-Z])|([A-Z])");
if (lParts.Count != 2) return null;
if (lParts[1].Value == "Z") return incCol(lParts[0].Value) + "A";
else return lParts[0].Value + incCol(lParts[1].Value);
}
private static string incCol(string tcCol)
{
if (tcCol == "") return "A";
string fPart = tcCol.Substring(0, tcCol.Length - 1);
char lChar = tcCol[tcCol.Length - 1];
if (lChar == 'Z') return incCol(fPart) + "A";
return fPart + ++lChar;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment