PHP Classes

More than 26 columns

Recommend this page to a friend!

      PHP MySQL XLSX Export  >  All threads  >  More than 26 columns  >  (Un) Subscribe thread alerts  
Subject:More than 26 columns
Summary:Here is the solution
Messages:5
Author:Laurent Belloeil
Date:2016-06-22 13:26:09
 

  1. More than 26 columns   Reply   Report abuse  
Picture of Laurent Belloeil Laurent Belloeil - 2016-06-22 13:26:09
To get column letters !

protected function getColumnLetter( $number )
{
$prefix = '';
$prefNum = intval( $number/26 );
if( $prefNum > 0 )
{
$prefix = chr( $prefNum+64 );
$number = fmod( $number, 26 );
}

return $prefix.chr( $number+65 );
}

Regards

  2. Re: More than 26 columns   Reply   Report abuse  
Picture of Gianluca Zanferrari Gianluca Zanferrari - 2016-06-22 14:17:46 - In reply to message 1 from Laurent Belloeil
nice!

  3. Re: More than 26 columns   Reply   Report abuse  
Picture of Laurent Belloeil Laurent Belloeil - 2016-06-22 14:22:06 - In reply to message 1 from Laurent Belloeil
Oops, this works only for 26*26 columns !

Here is the function for infinite columns :

protected function getColumnLetter( $number )
{
$prefix = '';
$suffix = '';
$prefNum = intval( $number/26 );
if( $prefNum > 25 )
{
$prefix = $this->getColumnLetter( $prefNum % 26 );
}else{
}
if( $prefNum > 0 )
{
$prefix .= $this->getColumnLetter( $prefNum % 26 );
$suffix .= $this->getColumnLetter( fmod( $number, 26 ) );
}else{
$suffix .= chr( $number+65 );
}
return $prefix.$suffix;
}


  4. Re: More than 26 columns   Reply   Report abuse  
Picture of Laurent Belloeil Laurent Belloeil - 2016-06-22 14:32:54 - In reply to message 3 from Laurent Belloeil
Nope !

It jumps from AAZ to BBA !!! Grrrr...

  5. Re: More than 26 columns   Reply   Report abuse  
Picture of Laurent Belloeil Laurent Belloeil - 2016-06-22 15:27:45 - In reply to message 4 from Laurent Belloeil
I think I got it, more simple than I thought !

protected function getColumnLetter( $number )
{
$prefix = '';
$suffix = '';
$prefNum = intval( $number/26 );
if( $prefNum > 25 )
{
$prefix = $this->getColumnLetter( $prefNum );
}
$suffix = chr( fmod( $number, 26 )+65 );
return $prefix.$suffix;
}