5

I want to protect particular cell content from being amended. When I tried to protect whole sheet, no problem.

$sheet->getProtection()->setSheet(true)->setDeleteRows(true);

But, could not set protection for individual cell. I tried the following codes.

1

$sheet->protectCellsByColumnAndRow(0, 1, 100, 100, 'asdf');

2

$sheet->protectCells('A1','password',false);

Thanks in advance.

Premlatha
  • 1,676
  • 2
  • 20
  • 40

1 Answers1

9

Here is the solution. First, enable the worksheet protection. Then, unlock all the cell by change the default style of spreadsheet's protection. After that, lock the cell you want by specify the cell's coordinate. The worksheet protection applied to locked cell only. So, the cell that you locked could not be edited anymore when you open that worksheet.

$spreadsheet->getActiveSheet()->getProtection()->setSheet(true);
$spreadsheet->getDefaultStyle()->getProtection()->setLocked(false);
$sheet->getStyle('A1')->getProtection()->setLocked(\PhpOffice\PhpSpreadsheet\Style\Protection::PROTECTION_PROTECTED);

link-unlock all cells and link-lock individual cell had helped me.

Premlatha
  • 1,676
  • 2
  • 20
  • 40