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

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

问题描述

我需要将 Google 电子表格列索引转换为其相应的字母值,例如,给定电子表格:

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"

现在,我不记得索引是从 0 开始还是从 1 开始,反正概念应该很清楚.

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?

谢谢

推荐答案

我出于各种目的写了这些(将返回列号 > 26 的双字母列名):

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天全站免登陆