I'm creating some client side functions for a mappable spreadsheet export feature.
I'm using jQuery to manage the sort order of the columns, but each column is ordered like an Excel spreadsheet i.e. a b c d e......x y z aa ab ac ad etc etc
How can I generate a number as a letter? Should I define a fixed array of values? Or is there a dynamic way to generate this?
The cell you've selected will contain #N/A because you haven't told Excel which letter to search for. Go to the formula in the bar, click between the quotation marks and type the letter C. Your formula should now read: =LOOKUP("C",A2:B6). Press Enter and the value of the letter.
If want to convert a number to its corresponding alphabet letter like this:- 1 = A 2 = B 3 = C You will need to write a loop, and mod (%) the number by 26 each time through and use the following: String.fromCharCode(num + 64); Convert number to a letter of the alphabet JavaScript example HTML example code:
I could be wrong but I've checked the other functions in this answer and they seem to fail at 26 which should be Z. Remember there are 26 letters in the alphabet not 25. Show activity on this post.
Numbering the letters so A=1, B=2, etc is one of the simplest ways of converting them to numbers. This is called the A1Z26 cipher. However, there are more options such as ASCII codes, tap codes or even the periodic table of elements to decode numbers.
Letter Number (A1Z26) A=1, B=2, C=3. Tool to convert letters to numbers and vice versa. The Letter-to-Number Cipher (or Number-to-Letter Cipher) consists in replacing each letter by its position in the alphabet, for example A=1, B=2, Z=26, hense its over name A1Z26.
I think you're looking for something like this
function colName(n) { var ordA = 'a'.charCodeAt(0); var ordZ = 'z'.charCodeAt(0); var len = ordZ - ordA + 1; var s = ""; while(n >= 0) { s = String.fromCharCode(n % len + ordA) + s; n = Math.floor(n / len) - 1; } return s; } // Example: for(n = 0; n < 125; n++) document.write(n + ":" + colName(n) + "<br>");
This is a very easy way:
function numberToLetters(num) { let letters = '' while (num >= 0) { letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'[num % 26] + letters num = Math.floor(num / 26) - 1 } return letters }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With