Skip to content
the poor coder

column number to letter apps script

Poor Man
Published
Reading time
2 min read
On this page

Column Number to Letter Apps Script#

Have you ever needed to convert a column number to its corresponding letter in Google Sheets? It's a common problem that can be easily solved with an Apps Script function. In this blog post, we'll show you how to do just that!

The Problem#

Let's say you have a Google Sheet with a bunch of columns labeled A, B, C, etc. But instead of referring to columns by their letters, you want to refer to them by their numbers. For example, instead of writing "=A1+B1", you want to write "=1+2". How do you convert the column numbers to letters?

The Solution#

Luckily, Apps Script has a built-in function called "String.fromCharCode()" that can convert ASCII codes to their corresponding letters. We can use this function to convert column numbers to letters. Here's the code:

JavaScript

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

Let's break down how this code works:

  • We start by declaring two variables: "temp" and "letter".
  • We enter a while loop that runs as long as the variable "column" is greater than zero.
  • Inside the while loop, we calculate the remainder of "column - 1" divided by 26. This gives us the index of the letter we need to add to our "letter" variable.
  • We use the "String.fromCharCode()" function to convert the index to its corresponding letter.
  • We add the letter to the beginning of our "letter" variable.
  • We calculate the new value of "column" by subtracting "temp" and 1 from it, and then dividing the result by 26.
  • We return the final value of "letter".

Using the Function#

Now that we have our function, we can use it to convert column numbers to letters in our Google Sheets formulas. Here's an example:

JavaScript

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var columnNumber = 1;
  var columnLetter = columnToLetter(columnNumber);
  var cell = sheet.getRange(columnLetter + '1');
  cell.setValue('Hello, World!');
}

In this example, we're setting the value of cell A1 to "Hello, World!" using the "columnToLetter()" function to convert the column number to its corresponding letter.

Alternative Solution#

Another way to convert column numbers to letters is to use the "num2col()" function from the "tabletop.js" library. Here's an example:

JavaScript

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var columnNumber = 1;
  var columnLetter = Tabletop.num2col(columnNumber);
  var cell = sheet.getRange(columnLetter + '1');
  cell.setValue('Hello, World!');
}

This solution is shorter and simpler, but it requires you to load the "tabletop.js" library into your project.

Conclusion#

Converting column numbers to letters in Google Sheets can be a tricky problem, but with the help of Apps Script, it's easy to solve. Whether you choose to use the built-in "String.fromCharCode()" function or the "num2col()" function from the "tabletop.js" library, you'll be able to convert column numbers to letters in no time!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC