Last active
April 4, 2025 08:17
-
-
Save AlfonsoUceda/42811a2527010d0063df0782214d39d8 to your computer and use it in GitHub Desktop.
Convert excel columns to integers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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