2

How can I disable a couple of cells but keep the rest editable, using the PHPExcel library? I tried a few combinations using:

    $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
    $objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
    $objPHPExcel->getActiveSheet()
        ->getStyle('A1:Z50')
        ->getProtection()->setLocked(
            \PHPExcel_Style_Protection::PROTECTION_UNPROTECTED
        );
    $objPHPExcel->getActiveSheet()
        ->getStyle('C7:E7')
        ->getProtection()->setLocked(
            \PHPExcel_Style_Protection::PROTECTION_PROTECTED
        );
    $objPHPExcel->getActiveSheet()->getProtection()->setSheet(false);
    $objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(false);

but all I can get is all document disabled or enabled. I'm pretty sure this can be achieved easily. Thanks in advance.

androidu
  • 4,678
  • 6
  • 36
  • 51
  • What do you mean by "disable"? Perhaps this may help you? : http://stackoverflow.com/questions/17046207/phpexcel-lock-particular-cell . Also, why are you escaping php excel style? : `->getProtection()->setLocked( \PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);` .. This should be `->getProtection()->setLocked( PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);`, shouldn't it? – briosheje Dec 12 '13 at 12:45
  • 1
    Disable cell editing for a few cells but keep the rest of the document editable. I tried the sample code and is does not work like I need it to. – androidu Dec 12 '13 at 12:50
  • That is how you access a ClassName prepended with the namespace so you don't need to write the 'use' directive in the top of your php class. It's just the way my IDE auto imports php classes. No need to worry about that. – androidu Dec 12 '13 at 12:54
  • Does emulating this [point-and-click logic](http://office.microsoft.com/en-us/excel-help/lock-only-a-few-cells-on-a-worksheet-HA001054825.aspx) not do it? (Your code above appears to follow a different method.) – bishop Dec 12 '13 at 13:24
  • @bishop If I follow those steps, it works, the cells I pick will be frozen. But how do I achieve this in code? – androidu Dec 12 '13 at 13:49
  • Well, each one of those steps has a corresponding API call, so I'd figure you'd just translate from point-and-click to API. Haven't done it myself, so I may be speaking out of turn. That said, are you amenable to a solution that locks the whole sheet but unprotects a few cells? If so, I'll answer with actual code for that, as I know that works. – bishop Dec 12 '13 at 14:19
  • @bishop Why not. As long as it works, I've lost a lot of time already. I accept your help – androidu Dec 12 '13 at 14:32

1 Answers1

3

Solution that protects a whole sheet, while keeping particular cells editable:

$excel->getActiveSheet()->getProtection()->setSheet(true); 
$excel->getActiveSheet()->getStyle('A12:D50')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED); 

Complete example. In the resulting file, I can edit only cells B2, C2, and D2:

$excel = new PHPExcel();
$excel->setActiveSheetIndex(0)
      ->setCellValue('A1', 'Hello')
      ->setCellValue('B2', 'world!')
      ->setCellValue('C1', 'Hello')
      ->setCellValue('D2', 'world!')
  ;
$excel->getActiveSheet()
    ->getProtection()->setSheet(true);
$excel->getActiveSheet()->getStyle('B2:D2')
    ->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);

$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$writer->save(str_replace('.php', '.xls', __FILE__));
Havenard
  • 27,022
  • 5
  • 36
  • 62
bishop
  • 37,830
  • 11
  • 104
  • 139
  • Indeed your snip does work. Then I must be doing something else wrong. Thanks for your help bishop ;) – androidu Dec 12 '13 at 15:13
  • The difference for me is that I don't create a new XLS, I edit an existing template. But that should not matter – androidu Dec 12 '13 at 15:41