Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript excel column letter to number

/**
 * Converts a spreadsheet column letter to a number. eg. A -> 1
 * 
 * @param {String} str - (required) The spreadsheet column letter to convert to a number.
 * 
 * @returns {(Number|undefined)} The column number or undefined.
 */
function convertLetterToNumber(str) {
  if ((typeof str === "string" || str instanceof String) && /^[a-zA-Z]+$/.test(str)) {
    str = str.toUpperCase();
    let out = 0,
      len = str.length;
    for (pos = 0; pos < len; pos++) {
      out += (str.charCodeAt(pos) - 64) * Math.pow(26, len - pos - 1);
    }
    return out;
  } else {
    return undefined;
  }
}

convertLetterToNumber("A"); // 1
convertLetterToNumber("b"); // 2
convertLetterToNumber("Ba"); // 53
convertLetterToNumber("dE"); // 109
convertLetterToNumber("ZZZZ"); // 475254

convertLetterToNumber("A"); // 1
convertLetterToNumber("b"); // 2
convertLetterToNumber("Ba"); // 53
convertLetterToNumber("dE"); // 109
convertLetterToNumber("ZZZZ"); // 475254
Comment

PREVIOUS NEXT
Code Example
Javascript :: feather client 
Javascript :: discord.js get all members with role 
Javascript :: unexpected token export type react bottontab navigation 
Javascript :: get day name from date javascript 
Javascript :: get filename from url js 
Javascript :: var data ="<tr<td"+ data.data[i].name"</td"+"<td"+ data.data[i].email"</td"+"<td"+ data.data[i].create_at"</td</tr"; 
Javascript :: angular declare variable in a file 
Javascript :: angular delete from array by name 
Javascript :: cypress how to get element length 
Javascript :: add element to array using splice 
Javascript :: RFC 3339 format js 
Javascript :: set datetimepicker javascript 
Javascript :: nextjs socket 
Javascript :: phpmyadmin is not working in ubuntu 20.04 
Javascript :: https with express 
Javascript :: url redirect javascript 
Javascript :: redirect to html page in javascript 
Javascript :: html call javascript variable 
Javascript :: javascript parentnode 
Javascript :: javascript prompt 
Javascript :: jquery enable disable textbox 
Javascript :: sequelize update record 
Javascript :: javascript combine array of arrays 
Javascript :: Regex match word js 
Javascript :: array filter falsy values 
Javascript :: how to print the value of variable in javascript in html 
Javascript :: avascript sleep 1 second 
Javascript :: jquery open image in new tab 
Javascript :: form submit programmatically 
Javascript :: function use for placing bet 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =