Shankar Akunuri
Shankar Akunuri

Reputation: 187

column index from column name in PHPExcel

i have just started using PHPExcel-1.7.7..

i'm having a problem in getting column index from column name like

A -> 1, H-> 8, L-> 12

like that..

thanks in advance..

Upvotes: 1

Views: 4073

Answers (2)

Ales
Ales

Reputation: 441

public static function lettersToInt(string $string): int {
    $letters = str_split($string);
    $lastLetter = array_pop($letters); // Last letter is defining number between 0-26
    $index = 0;
    $alphabet = range('A', 'Z');
    foreach($letters as $letter) {
        // Letters before last letter are defining how many times to add 26.
        $multiplier = array_search($letter, $alphabet) + 1;
        $index = $index + 26 * $multiplier;
    }
    $index = $index + array_search($lastLetter, $alphabet);
    return $index;
}

I have tested it for two letter column names. My brain was refusing to solve three letters (maybe not complicated, but no more time in project budget :) )

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212402

So what's the problem?

$column = 'IV';
$columnIndex = PHPExcel_Cell::columnIndexFromString($column);

And why are you using version 1.7.7 when version 1.7.9 is the latest production release?

Upvotes: 6

Related Questions