Skip to content

Instantly share code, notes, and snippets.

@AlfonsoUceda
Last active April 4, 2025 08:17
Show Gist options
  • Save AlfonsoUceda/42811a2527010d0063df0782214d39d8 to your computer and use it in GitHub Desktop.
Save AlfonsoUceda/42811a2527010d0063df0782214d39d8 to your computer and use it in GitHub Desktop.
Convert excel columns to integers
CHAR_TO_INDEX = ('A'..'Z').to_enum.with_index.to_h.transform_values {|v| v+1}
def getNumber(column)
total_indices = CHAR_TO_INDEX.size
chars_array = column.chars
last_char = chars_array.pop
result = 0
chars_array.each_with_index do |char, index|
result += total_indices * CHAR_TO_INDEX[char] * (index + 1)
end
result += CHAR_TO_INDEX[column.chars[-1]]
end
puts getNumber('ZA')
puts getNumber('ZZ')
puts getNumber('A')
puts getNumber('Z')
puts getNumber('AA')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment