您好:使用Google Spreadsheet API V4 on Java
,我希望column letter
获得column number
。示例:列号 2 的列字母为 B 。
Google电子表格API中是否已经设置了一些方法或还有其他简单的方法吗 OR 我是否需要编写自己的转换?
答案 0 :(得分:2)
虽然问题仍然是: Google Spreadsheet API V4中是否有native way
来获取给定列号的列字母?
但是,对于future reference
我提供的collation
以下,以防有人需要实施自己的转化。
案例1:根据列号获取列字母
public void ColumnNumberToLetter() {
int inputColumnNumber = 49;
String outputColumnName = "";
int Base = 26;
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int TempNumber = inputColumnNumber;
while (TempNumber > 0) {
int position = TempNumber % Base;
outputColumnName = (position == 0 ? 'Z' : chars.charAt(position > 0 ? position - 1 : 0)) + outputColumnName;
TempNumber = (TempNumber - 1) / Base;
}
System.out.println("ColumnNumberToLetter :" + inputColumnNumber + " = " + outputColumnName);
}
案例2:根据列号获取列号
public void ColumnLetterToNumber() {
String inputColumnName = "AW";
int outputColumnNumber = 0;
if (inputColumnName == null || inputColumnName.length() == 0) {
throw new IllegalArgumentException("Input is not valid!");
}
int i = inputColumnName.length() - 1;
int t = 0;
while (i >= 0) {
char curr = inputColumnName.charAt(i);
outputColumnNumber = outputColumnNumber + (int) Math.pow(26, t) * (curr - 'A' + 1);
t++;
i--;
}
System.out.println("ColumnLetterToNumber : " + inputColumnName + " = " + outputColumnNumber);
}