How to detect and remove hidden columns

Topics: User Forum
Jul 17, 2014 at 6:32 PM
I am using PHPExcel version 1.8 and uploading Excel files using the Excel5 reader object. The Excel files that get uploaded are somewhat inconsistent and one in particular will come in with a column hidden. I need to be able to detect if/when this column is hidden and delete it if it is hidden. I have been able to delete the hidden column by using the following:
$worksheet->removeColumn('B');
However, as I mentioned the column isn't always hidden and therefor needs to be detected prior to deleting. I've tried to use this code below to detect and remove the column only when it's hidden:
if ($worksheet->getColumnDimension('B')->getVisible(false)) {
    $worksheet->removeColumn('B');
}
What am I doing wrong and can someone please help me understand getVisible better since I cannot seem to find much documentation on this. Thank you in advance for any help.
Jul 22, 2014 at 1:30 PM
Well, I ended up finding my own solution. For those of you who have the same question, see below for the work around. I know what the highest column should be, so this is why this works, otherwise if you are just trying to remove any hidden columns this is not the solution.

First thing I did was look for the highest column with:
$highestCol = $worksheet->getHighestDataColumn();
Then, since I know what the highest column should be and I also know that if the highest column is one letter higher than expected that there was a hidden column stored in the file. So, I just checked for this highest letter with:
if ($highestCol == "I") $worksheet->removeColumn('B');
Very simple. If anyone discovers a way to use the original code I posted let me know. I would still like to use the following approach if I could:
if ($worksheet->getColumnDimension('B')->getVisible(false)) {
    $worksheet->removeColumn('B');
}