将列索引转换为相应的列字母 [英] Convert column index into corresponding column letter

查看:136
本文介绍了将列索引转换为相应的列字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Google Spreadsheet列索引转换为其对应的字母值,例如,给定电子表格: .stack.imgur.com / DyaPs.pngalt =

我需要这样做(这个函数显然不存在,它是一个例如):

  getColumnLetterByIndex(4); //这应该返回D
getColumnLetterByIndex(1); //这应该返回A
getColumnLetterByIndex(6); //这应该返回F

现在,我不记得索引是否从 0 或从 1 ,无论如何这个概念应该是清楚的。



我没有发现关于气体文件的任何内容..我是否失明?任何想法?



谢谢

解决方案

(将返回列号> 26的双字母列名称):

 函数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);
}
返回列;
}


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):

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

解决方案

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;
}

这篇关于将列索引转换为相应的列字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆