将列索引转换为相应的列字母 [英] Convert column index into corresponding column letter
问题描述
我需要将Google Spreadsheet列索引转换为其对应的字母值,例如,给定电子表格: 我需要这样做(这个函数显然不存在,它是一个例如): 现在,我不记得索引是否从 我没有发现关于气体文件的任何内容..我是否失明?任何想法? 谢谢 (将返回列号> 26的双字母列名称): I need to convert a Google Spreadsheet column index into its corresponding letter value, for example, given a spreadsheet: I need to do this (this function obviously does not exist, it's an example): Now, I don't recall exactly if the index starts from I didn't find anything about this on gas documentation.. am I blind? Any idea? Thank you I wrote these a while back for various purposes (will return the double-letter column names for column numbers > 26):
这篇关于将列索引转换为相应的列字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
getColumnLetterByIndex(4); //这应该返回D
getColumnLetterByIndex(1); //这应该返回A
getColumnLetterByIndex(6); //这应该返回F
0
或从 1
,无论如何这个概念应该是清楚的。
函数columnToLetter(column)
{
var temp,letter ='';
while(column> 0)
{
temp =(column - 1)%26;
letter = String.fromCharCode(temp + 65)+ letter;
column =(column - temp - 1)/ 26;
}
返回信;
}
函数letterToColumn(字母)
{
var column = 0,length = letter.length;
for(var i = 0; i
column + =(letter.charCodeAt(i) - 64)* Math.pow(26,length - i - 1);
}
返回列;
}
getColumnLetterByIndex(4); // this should return "D"
getColumnLetterByIndex(1); // this should return "A"
getColumnLetterByIndex(6); // this should return "F"
0
or from 1
, anyway the concept should be clear.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;
}