Anjanesh Lekshminarayanan
Anjanesh

Anjanesh

Most suitable language for performing Excel operations

Adding one to an upper case character using the increment operator

Anjanesh Lekshminarayanan's photo
Anjanesh Lekshminarayanan
·May 13, 2022·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

In which language where you increment a character (well .... it has to be uppercase in this case) by 1 and you get the next character in ASCII (also alphabetic) sequence ?

I don't know if this is supported in any other language but this is doable in PHP (even in older versions).

$column = 'B';
echo ++$column;

will output C

But these won't work :

  • $column = $column + 1; - will throw string to integer operation error - TypeError: 'str' object cannot be interpreted as an integer
  • Decrement operators : $column-- will not give you the previous charcater in sequence. Will not throw any errors though. Will retain value as 'B'
  • Lowercase won't work either $column = 'b';

This is what I did in a real use-case : (I got the XLSX sheet's data map to a PHP array using another script)

xlsx.png

<?php
$data = [
    'B' => [5=>105,15,205,22,35,332],
    'D' => [5=>95,95,99,32,442,95],
    'E' => [5=>62,89,51,55,32,95],
    'F' => [5=>66,78,44,9,329,55],
    'G' => [5=>55,100,80,63,951,32],
    'H' => [5=>10,55,591,32,200,80]    
];
?>
<table border="1">
<?php
for ($i = 5; $i <= 10; $i++)
{
    echo "<tr>";

    for ($j = 'B'; $j !== 'I'; $j++)
    {
        # echo $j;

        if ($j == 'C') continue;

        echo "<td>";        

        if (!isset($data[$j][$i])) continue;
        $value = $data[$j][$i];

        echo $value;

        echo "</td>";
    }

    echo "</tr>";
}
?>
</table>

xlsx-table.png

Isn't this just so cool ?

 
Share this