Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert column index into corresponding column letter

I need to convert a Google Spreadsheet column index into its corresponding letter value, for example, given a spreadsheet:

enter image description here

I need to do this (this function obviously does not exist, it's an example):

getColumnLetterByIndex(4);  // this should return "D" getColumnLetterByIndex(1);  // this should return "A" getColumnLetterByIndex(6);  // this should return "F" 

Now, I don't recall exactly if the index starts from 0 or from 1, anyway the concept should be clear.

I didn't find anything about this on gas documentation.. am I blind? Any idea?

Thank you

like image 709
BeNdErR Avatar asked Jan 20 '14 08:01

BeNdErR


People also ask

How do I return a column letter in Excel?

If you have Professor Excel Tools already, you can simply use the =PROFEXColumn() function to return the column letter.

How do I convert a number to a column letter in Excel VBA?

Do-While Loop is another way to convert column number to letter in VBA Excel. Similarly, call the UDF in the dataset, pass the cell reference number as the argument, press Enter and drag the row to convert the column number to letter in Excel with the Do-While Loop in VBA.


2 Answers

I wrote these a while back for various purposes (will return the double-letter column names for column numbers > 26):

function columnToLetter(column) {   var temp, letter = '';   while (column > 0)   {     temp = (column - 1) % 26;     letter = String.fromCharCode(temp + 65) + letter;     column = (column - temp - 1) / 26;   }   return letter; }  function letterToColumn(letter) {   var column = 0, length = letter.length;   for (var i = 0; i < length; i++)   {     column += (letter.charCodeAt(i) - 64) * Math.pow(26, length - i - 1);   }   return column; } 
like image 173
AdamL Avatar answered Sep 30 '22 21:09

AdamL


This works good

=REGEXEXTRACT(ADDRESS(ROW(); COLUMN()); "[A-Z]+") 

even for columns beyond Z.

Demo of function

Simply replace COLUMN() with your column number. The value of ROW() doesn't matter.

like image 34
wronex Avatar answered Sep 30 '22 19:09

wronex