… просто нужно решение для PHP . Вот что я придумал:
/**
* Calculates the column number for a given column name.
*
* @param string $columnName the column name: "A", "B", …, "Y", "Z", "AA", "AB" … "AZ", "BA", … "ZZ", "AAA", …
*
* @return int the column number for the given column name: 1 for "A", 2 for "B", …, 25 for "Y", 26 for "Z", 27 for "AA", … 52 for "AZ", 53 for "BA", … 703 for "AAA", …
*/
function getColumnNumber($columnName){
// the function's result
$columnNumber = 0;
// at first we need to lower-case the string because we calculate with the ASCII value of (lower-case) "a"
$columnName = strtolower($columnName);
// ASCII value of letter "a"
$aAsciiValue = ord('a') - 1;
// iterate all characters by splitting the column name
foreach (str_split($columnName) as $character) {
// determine ASCII value of current character and substract with that one from letter "a"
$characterNumberValue = ord($character) - $aAsciiValue;
// through iteration and multiplying we finally get the previous letters' values on base 26
// then we just add the current character's number value
$columnNumber = $columnNumber * 26 + $characterNumberValue;
}
// return the result
return $columnNumber;
}
Конечно, скрипт можно немного сократить, просто объединив некоторые вещи в одну строку кода в цикле foreach:
// …
$columnNumber = $columnNumber * 26 + ord($character) - ord('a') + 1;
// …