Cell Protection

Topics: Developer Forum, User Forum
Mar 3, 2008 at 10:17 AM
Edited Mar 3, 2008 at 10:19 AM

first, PHPExcel is very usefull, works fine and is really promising. I did 95% of a project with it in no time, thanks again !

But I can not set up single cell protection. I have read all discussions and issues and the docus and tried long time,
even setting up a default style where the cells are locked didnt seem to work, please help.

Short : Whenever I put in a value in a cell, its not longer protected. I want to put in a value and then protect it (again) and then
save file.

Small example of one way I tried :

$ed = new PHPExcel();
$ed->getActiveSheet()->setCellValue("A1", 33.21);

I tried dozens of other ways...default style, order etc..

Any help would be highly appreciated.
Mar 4, 2008 at 6:54 AM
Here's a snippet from 05featuredemo example:
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);	// Needs to be set to true in order to enable any worksheet protection!
$objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel');

Base rule is: make sure you use $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); to enable / disable any protection measures.
Mar 4, 2008 at 9:58 AM
Edited Mar 6, 2008 at 12:57 PM
Nope, no chance to dance.

I can do what I want, the protection of filled cells is not possible. (Everything else seem to work fine,
well it does not set the meta tags and it puts a PAGESTYLE_ in front of my worksheet title)

Problem Description : Fill a a value into a cell and then protect it, not with .xls writer nor xlsx. writer.

I tried a lot of ways seeming logically, but even the reference to the tutorial of maarenba did not do the trick.

I think the library is not supposed to protect cells that were filled by it.

I am reformating the current example of mine to post it here. Perhaps its a little freaking out detail,
but I double checked it with all tutorials and sources on the web (even chinese).

Then setLocked(PHPExcelStyleProtection::PROTECTION_PROTECTED); IS not WORKING but to UNLOCK cells that were locked with the protectCells.
I will use protectCells then.
There seem to be no difference between the order of setSheet(). It seems to make no difference if
you set setSheet true before or after any security operation.
Jul 10, 2013 at 6:39 PM
maartenda's code works for me but it always locks the entire sheet, even when I only specify a small range of cells. Has anyone else run into this. I only want to lock a single column of my worksheet but it keeps locking the entire thing.
Jul 27, 2013 at 11:51 PM


This will protect the whole sheet and unprotected certain cell that you specify with code line 2 ;)
Aug 12, 2013 at 9:48 AM
How do i load the template after creating the new PHPExcel object so that i lock certain cells in that template?
Aug 12, 2013 at 11:05 AM
Often, it is loading the file that created the object:
$objPHPExcel = PHPExcel_IOFactory::load("05featuredemo.xlsx");