phpspreadsheetphpoffice

How to lock particular cell using phpspreadsheet


Initially all cells in worksheet are locked. Then, I unlock all the cells and try to lock only some of the cells.

$spreadsheet->getActiveSheet()->getStyle('A1')->getLocked()->applyFromArray(
      [
          'locked' => TRUE,
          'hidden' => FALSE
     ]);

But, end up with this error.

Uncaught Error: Call to undefined method PhpOffice\PhpSpreadsheet\Style\Style::getLocked()

Thanks in advance.


Solution

  • This code able to lock the cell we want.

    $sheet->getStyle('A1')->getProtection()->setLocked(\PhpOffice\PhpSpreadsheet\Style\Protection::PROTECTION_INHERIT);
    

    or

     $sheet->getStyle('A1')->getProtection()->setLocked(\PhpOffice\PhpSpreadsheet\Style\Protection::PROTECTION_PROTECTED);
    

    Still, I would like to know why applyFromArray not working to lock the cell. Anyone know the answer, can post your answer. Thanks.