Convert an excel or spreadsheet column letter to its number in Pythonic fashion

前端 未结 17 1222
夕颜
夕颜 2020-12-09 04:18

Is there a more pythonic way of converting excel-style columns to numbers (starting with 1)?

Working code up to two letters:



        
17条回答
  •  既然无缘
    2020-12-09 04:53

    After reading this, I decided to find a way to do it directly in Excel cells. It even accounts for columns after Z.

    Just paste this formula into a cell of any row of any column and it will give you the corresponding number.

    =IF(LEN(SUBSTITUTE(ADDRESS(ROW(),COLUMN(),4),ROW(),""))=2,
     CODE(LEFT(SUBSTITUTE(ADDRESS(ROW(),COLUMN(),4),ROW(),""),1))-64*26)+
     CODE(RIGHT(SUBSTITUTE(ADDRESS(ROW(),COLUMN(),4),ROW(),""),1)-64),
     CODE(SUBSTITUTE(ADDRESS(ROW(),COLUMN(),4),ROW(),""))-64)
    

    The theme here was to grab the letter of the column, get the Code() of it and subtract 64, based on the fact that the ASCII character code for letter A is 64.

提交回复
热议问题