I am trying to create an algorithm that will implicitly handle every cell identifier possible; using A1 reference style. The A1 reference style is typically limited to columns (A - IV), and rows (0 - 65,536). This is the format used to generate Excel, and Numeric spreadsheets. I understand why these limitations are in place, but I am trying to wrap my head around the "correct" way to name the columns; procedurally. More specifically, I am trying to understand how to procedurally generate the correct names for each column. The result would be a dictionary with columns A-Z, then AA-ZZ, then AAA-ZZZ, and so on. The cells of this "spreadsheet" would be something like AHD129, or A1, or XAPRC100204.
It all started with trying to generate the cells of a chessboard, which was relatively easy:
board = {}
for file in 'ABCDEFGH':
for rank in range(0, 8):
cell = file + str(rank + 1)
board[cell] = ''
This handles every combination of a chess board (A1 - H8), and places it all into a dictionary. The problem is that it is highly explicit.
We could implicitly handle every possible combination (A1 - Z26), like so...
def generate(units):
cells = {}
alphabet = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
for column in range(0, units):
char = alphabet[column]
for row in range(0, units):
cell = char + str(row + 1)
cells[cell] = ''
return cells
This is better, because it allows us to generate a "chessboard", within a given range (A1-Z26). You can pass this function any integer from 1 to 26, but an error will be thrown when (units > 26). If we divided the "chessboard" by 27 units, then we would need to generate (A1-AA27).
unit = 1 -> 26 | result A1 - Z26
unit = 1 -> 52 | result A1 - AZ52
unit = 1 -> 78 | result A1 - BZ78
unit = 1 -> 104| result A1 - CZ104
My math may be off by a few integers, but I am basically just trying to create a representation of a square. Columns and Rows are ALWAYS equal, which is why I was approaching this with nested for loops. I know that I could write a massive conditional block to handle each range of 26, beyond the initial 26 characters, something like:
if column in range(0, len(alphabet)):
char = alphabet[i]
elif column in range(len(alphabet), len(alphabet) * 2):
char = 'A' + alphabet[i]
elif column in range(len(alphabet) * 2, len(alphabet) * 3):
char = 'B' + alphabet[i]
...but that would be counter-intuitive. I am trying to handle an arbitrary integer (such as: 1,000,000, or 63,432). I am assuming (welp) that it has something do with "brute-forcing" the column name by means of a while loop, but I also know that this is Python. i.e. there is an extremely easy way to solve this that I have never come across before.
I don't want anyone to "write my code" for me, but could somebody please point me in the right direction? How does one go about procedurally generating column names in an A1 reference style spreadsheet format? What is the best way to construct a string from a given list of characters in Python?
Please do not suggest a library to solve this, as I am trying to understand the underlying solution. Sorry for any incorrect terminology, and please correct my words if I have used anything incorrectly. Any help, or consideration, would be greatly appreciated. I am more than willing to revise this entire question for future viewers.