0

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.

Premlatha
  • 1,676
  • 2
  • 20
  • 40

2 Answers2

0

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.

Premlatha
  • 1,676
  • 2
  • 20
  • 40
0

I had to do it in two steps. Step 1 upon sheet creation I said "This sheet has some locking ranges but they don't lock by default"

    $spreadsheet->getActiveSheet()->getProtection()->setSheet(true);
    $spreadsheet->getDefaultStyle()->getProtection()->setLocked(false);

Then ApplyFromArray worked

$spreadsheet->getActiveSheet()->getStyle('A1')->applyFromArray(
    array(
        'protection' => array('locked' => \PhpOffice\PhpSpreadsheet\Style\Protection::PROTECTION_PROTECTED),
    )
);