আমি এমন স্ক্রিপ্টে কাজ করছি যা কিছু এক্সেল নথি উত্পন্ন করে এবং একটি সংখ্যাকে এর কলাম নামের সমতুল্যে রূপান্তর করা দরকার। উদাহরণ স্বরূপ:
1 => A
2 => B
27 => AA
28 => AB
14558 => UMX
আমি ইতিমধ্যে এটি করার জন্য একটি অ্যালগরিদম লিখেছি, তবে আমি এটি করতে সহজ বা দ্রুত উপায় কিনা তা জানতে চাই:
function numberToColumnName($number){
$abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$abc_len = strlen($abc);
$result_len = 1; // how much characters the column's name will have
$pow = 0;
while( ( $pow += pow($abc_len, $result_len) ) < $number ){
$result_len++;
}
$result = "";
$next = false;
// add each character to the result...
for($i = 1; $i<=$result_len; $i++){
$index = ($number % $abc_len) - 1; // calculate the module
// sometimes the index should be decreased by 1
if( $next || $next = false ){
$index--;
}
// this is the point that will be calculated in the next iteration
$number = floor($number / strlen($abc));
// if the index is negative, convert it to positive
if( $next = ($index < 0) ) {
$index = $abc_len + $index;
}
$result = $abc[$index].$result; // concatenate the letter
}
return $result;
}
আপনি এটি করার আরও ভাল উপায় জানেন? এটিকে সহজ রাখার জন্য কিছু আছে? না পারফরম্যান্সের উন্নতি?
সম্পাদনা করুন
ইরকমেক্সেলের বাস্তবায়ন বেশ সূক্ষ্মভাবে কাজ করে। তবে, আমি এই সুন্দর সংক্ষিপ্তটি যুক্ত করতে যাচ্ছি:
function num2alpha($n)
{
for($r = ""; $n >= 0; $n = intval($n / 26) - 1)
$r = chr($n%26 + 0x41) . $r;
return $r;
}