2

I have issues with cell protection. The basic rule for protect should be something like that:

$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);    
$objPHPExcel->getActiveSheet()->protectCells('A3:A13', 'PHPExcel');

if I set protection to true, I can't insert new rows, even if I set insert row to true:

$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);

I'd like to protect the structure and one columns while data are in there, otherwise, I'd like to have all empty rows unprotected.

How to do that?

I tried to unprotect/unprotect columns and rows inside loop - which works ok, but outside the loop, all the cells are protected, which means, I can't insert any new rows. I tried to set protection to true inside the loop, and to false outside the loop, but it doesn't work that way. Here is the code to unprotect all fields except ID:

$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
while($row_data = mysql_fetch_assoc($query)) {
    foreach($row_data as $key=>$value) {

        $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED); 
        if($key != 'id'){
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($col,$row)->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_PROTECTED);
        }

        $col++;
    }
    $row++;
}
4

1 回答 1

4

try something like this (add after your loop)

$objPHPExcel->getActiveSheet()
            ->getStyle('A45:S500')
            ->getProtection()->setLocked(
                PHPExcel_Style_Protection::PROTECTION_UNPROTECTED
            );

(replace range that suits your needs)

于 2013-06-19T06:48:06.710 回答