Reuben
Reuben

Reputation: 4266

PHPExcel: How to remove a row so that $worksheet->getHighestDataRow is adjusted?

How can one remove a row, or a number of rows in PHPExcel (1.7.7) such that the getHighestDataRow value is decremented?

Usage of removeRow() does not seem to actually adjust the getHighestDataRow value.

Upvotes: 4

Views: 1520

Answers (2)

Anne Gunn
Anne Gunn

Reputation: 2447

FWIW, I believe this is now fixed. I'm using PhpExcel 1.8 and the code for removeRow explicitly changes the highestDataRow value, viz:

public function removeRow($pRow = 1, $pNumRows = 1)
{
    if ($pRow >= 1) {
        $highestRow = $this->getHighestDataRow();
        $objReferenceHelper = PHPExcel_ReferenceHelper::getInstance();
        $objReferenceHelper->insertNewBefore('A' . ($pRow + $pNumRows), 0, -$pNumRows, $this);
        for ($r = 0; $r < $pNumRows; ++$r) {
            $this->getCellCacheController()->removeRow($highestRow);
            --$highestRow;
        }
    } else {
        throw new PHPExcel_Exception("Rows to be deleted should at least start from row 1.");
    }
    return $this;
}

So, if this is still a problem for anyone, an update to your version of PhpExcel should solve it.

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212412

Correct, it doesn't. You'll notice that the same applies to columns as well.

Either do it yourself, or ask the library authors to fix this bug in PHPExcel. It's known, but low down on their priority list, so a further request to fix it might boost it up that list.

For an existing work item, see "Trailing empty rows remain after removeRow()". The issue described is more specific, but the cause is the same.

Upvotes: 2

Related Questions