clearing a row in PHPExcel

Topics: Developer Forum
Mar 4, 2012 at 9:32 AM

I'm trying to clear a previously written row in a spreadsheet and have been trying this approch, but it doesn't seem to work for me, I think its the way I'm using the $row and $col in the SetCellValue but I've tried all variations and none work!

 

$row=1;

            if ($EmpTotHours<39)     {
                $highestColumn = $objWorksheet->getHighestColumn();
                for ($col = 0; $col <= $highestColumnIndex; ++$col) {
                    $objWorksheet->SetCellValue($col.','.$nrow,'');
                }
            }

 

any help greatly appreciated!

Cyril.

Coordinator
Mar 4, 2012 at 10:08 AM

$highestColumn !== $highestColumnIndex

getHighestColumn returns a column name, e.g. 'A', 'B', 'AA', etc; not a number.

if ($EmpTotHours<39) {
    $highestColumn = $objWorksheet->getHighestColumn(); 
    $highestColumn++;
    for ($col = 'A'; $col != $highestColumn; ++$col) {
        $objWorksheet->SetCellValue($col.$nrow,'');
    }
}

You're code is trying to set each of the cells in the row to an empty string value, is this what you want to do?

 

Mar 4, 2012 at 10:43 AM

Hi Mark,

 

yes, basically a row is written to the sheet (the number of columns is unknown), and it may be the case that this row then needs to be erased  or cleared, so really I don't care about the columns, I simply want to clear the contents of row x, maybe theres a simpler way of doing that?

 

thanks

Cyril

Coordinator
Mar 4, 2012 at 7:49 PM

if ($EmpTotHours<39) { 
    $highestColumnNumber = PHPExcel_Cell::columnIndexFromString($objWorksheet->getHighestColumn()); 
    $emptyCells = array_fill(1,$highestColumnNumber,''); 
    $objWorksheet->fromArray($emptyCells,NULL,'A'.$nrow); 
}