Skip to content

Instantly share code, notes, and snippets.

@mnmldave
Created August 28, 2009 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnmldave/177178 to your computer and use it in GitHub Desktop.
Save mnmldave/177178 to your computer and use it in GitHub Desktop.
#
# Converts a number into a column name like in Excel. For example,
#
# index_to_column(0) = 'A'
# index_to_column(1) = 'B'
# index_to_column(2) = 'C'
# ...
# index_to_column(25) = 'Z'
# index_to_column(26) = 'AA'
# index_to_column(27) = 'AB'
# ...
#
def index_to_column(index)
div, mod = index.divmod(26)
div > 0 ? index_to_column(div - 1) + ((?A) + mod).chr : ((?A) + mod).chr
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment