WhiteLine
WhiteLine

Reputation: 1989

PHPOffice/PhpSpreadsheet: Reset cell style

In the docs

i didn't find any useful function for my need.

Is it possible to "reset" / "delete the entire formatting" of a single cell? Return the cell to its original state.

Upvotes: 1

Views: 1979

Answers (2)

CAVASIN Florian
CAVASIN Florian

Reputation: 142

Instead of "undoing" all of your previous style changes, you can duplicate a cell style (that you didn't change) to your desired reset cell/range.

@param string $range Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")

function resetStyle(\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $worksheet, string $range): void
{
    $worksheet->duplicateStyle(
        $worksheet->getStyle('ZZ99999'),
        $range
    );
}

Upvotes: 0

Martin Zeitler
Martin Zeitler

Reputation: 76849

explicitly undoing the previous format should work:

$ws->getStyle('A1')->applyFromArray(array(
    'font' => array('bold' => false)
));

when importing, one can also skip formats altogether: $reader->setReadDataOnly(true);

Upvotes: 5

Related Questions