[英]Google Sheet API V4 Java, Get Column Letter from Column Number
Hello: Using Google Spreadsheet API V4 on Java
, I want to to get the column letter
given the column number
.您好: Google Spreadsheet API V4 on Java
,我想在给定column number
的情况下获取column letter
。 Example: the column letter for column number 2 is B .示例:第2列的列字母是B 。
Is there some way already provisioned in Google spreadsheet API OR is there any other simple way to go do this OR do i need to write my own conversion?谷歌电子表格 API 中是否已经提供了某种方法,或者是否有任何其他简单的方法可以 go 执行此操作,或者我是否需要编写自己的转换?
Though the Question still is: Is there a native way
in Google Spreadsheet API V4 to get the column letter given the column number? 尽管问题仍然存在:在Google Spreadsheet API V4中是否有native way
来获取给定列号的列字母?
However, for future reference
i am providing below a collation
in case someone needs to implement his own conversion. 但是,为了future reference
我在collation
下方提供了万一有人需要实现自己的转换的情况。
Case 1: Get Column Letter Given the Column Number 情况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);
}
Case 2: Get Column Number Given the Column Letter 情况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);
}
I think you could also just use the R1C1 notation mentioned here:我认为您也可以只使用此处提到的 R1C1 表示法:
https://developers.google.com/sheets/api/guides/concepts https://developers.google.com/sheets/api/guides/concepts
This let's you use column numbers instead of letters.这让您可以使用列号而不是字母。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.