sravis sravis - 14 days ago 5
PHP Question

PHPExcel lock particular cell

In phpexcel i was able to lock cell by

$objPHPExcel->getActiveSheet()->protectCells('A1:D1', 'php');
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);


If i double click on any Cell between A1 to D1 it will ask for password as it should.

But if i double click on any other cell (eg) A2 it says

"The cell or chart that you are trying to change is protected and therefore
read-only".


Its locking whole worksheet, Is it possible to lock only particular cell and leave other cells editable?

Answer

Finally, i found the right way to do it..

$objPHPExcel = new PHPExcel;
$objSheet = $objPHPExcel->getActiveSheet();

//PROTECT THE CELL RANGE

$objSheet->protectCells('A1:B1', 'PHP');

// UNPROTECT THE CELL RANGE

$objSheet->getStyle('A2:B2')->getProtection()
->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);

// PROTECT THE WORKSHEET SHEET

$objSheet->getProtection()->setSheet(true);

This is working perfectly!