Search code examples
javagoogle-sheets-apinotation

How can I convert an integer to A1 notation?


I am trying to create a method that will find the next column after a given column. For example:

input: A
output: B

It seems quite simple at first. I was just going to use the following method:

public static char nextLetter(char c) {
    c++;
    return c;
}

The problem arises when you get past column Z. In Google Sheets, after column Z, column names are two letters, then three, etc. So after column Z comes AA, after AZ comes BA, after ZZ comes AAA, etc. My next thought was to first figure out the column position in terms of index. So column AA would 27, BA 52, etc.

Finding the index of the column is not the problem I'm facing right now. I need to figure out how to convert that index to the corresponding column name. I was going to try the following method, but I realized that it is also limited to A-Z:

public static char getLetter(int index) {
    return (char) (index + 64);
}

At this point, I am thinking that a recursive method is needed. However, I cannot figure out to set it up. This is as far as I got:

private static void getNotation(int size) {
    int divided = size / 26;
    int remainder = size % 26;

    String notation = "";

    while (divided % 26 > 0) {
        // Here is where the need for a recursive method comes in
    }

}

Does anyone know a good way to convert an integer (index) to the corresponding column name?

EDIT

I just found a very helpful resource on Github which deals with hexavigesimals: https://gist.github.com/pinguet62/9817978


Solution

  • You can do it as follows:

    public class Main {
        public static void main(String[] args) {
            System.out.println(getNextColumn("A"));
            System.out.println(getNextColumn("Z"));
            System.out.println(getNextColumn("AA"));
            System.out.println(getNextColumn("AZ"));
            System.out.println(getNextColumn("ZA"));
            System.out.println(getNextColumn("ZZ"));
            System.out.println(getNextColumn("AAA"));
            System.out.println(getNextColumn("ABA"));
            System.out.println(getNextColumn("ABZ"));
            System.out.println(getNextColumn("ZZZ"));
        }
    
        static String getNextColumn(String column) {
            column = column.toUpperCase();
            StringBuilder sb = new StringBuilder();
            boolean allZ = true;
            for (int i = 0; i < column.length(); i++) {
                if (!(column.charAt(i) == 'Z')) {
                    allZ = false;
                    break;
                }
            }
            if (allZ) {
                for (int i = 0; i <= column.length(); i++) {
                    sb.append('A');
                }
                return sb.toString();
            }
            boolean charAfterZ = false;
            int indexOfZ = 0;
            for (int i = 0; i < column.length() - 1; i++) {
                if (column.charAt(i) == 'Z') {
                    charAfterZ = true;
                    indexOfZ = i;
                    break;
                }
            }
            if (charAfterZ) {
                sb.append(column.substring(0, indexOfZ + 1) + (char) (column.charAt(indexOfZ + 1) + 1));
                if (column.length() > indexOfZ + 2) {
                    sb.append(column.substring(indexOfZ + 1));
                }
                return sb.toString();
            }
    
            char lastChar = column.charAt(column.length() - 1);
    
            if (lastChar == 'Z') {
                sb.append(column.substring(0, column.length() - 2) + (char) (column.charAt(column.length() - 2) + 1) + ""
                        + 'A');
            } else {
                if (column.length() > 1) {
                    sb.append(column.substring(0, column.length() - 1) + ""
                            + (char) (column.charAt(column.length() - 1) + 1));
                } else {
                    sb.append((char) (column.charAt(column.length() - 1) + 1));
                }
            }
            return sb.toString();
        }
    }
    

    Output:

    B
    AA
    AB
    BA
    ZB
    AAA
    AAB
    ABB
    ACA
    AAAA