Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#
# 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
You can’t perform that action at this time.