12_CellProtection.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. use PhpOffice\PhpSpreadsheet\Style\Protection;
  4. require __DIR__ . '/../Header.php';
  5. // Create new Spreadsheet object
  6. $helper->log('Create new Spreadsheet object');
  7. $spreadsheet = new Spreadsheet();
  8. // Set document properties
  9. $helper->log('Set document properties');
  10. $spreadsheet->getProperties()->setCreator('Mark Baker')
  11. ->setLastModifiedBy('Mark Baker')
  12. ->setTitle('Office 2007 XLSX Test Document')
  13. ->setSubject('Office 2007 XLSX Test Document')
  14. ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
  15. ->setKeywords('office 2007 openxml php')
  16. ->setCategory('Test result file');
  17. // Add some data
  18. $helper->log('Add some data');
  19. $spreadsheet->setActiveSheetIndex(0);
  20. $spreadsheet->getActiveSheet()->setCellValue('A1', 'Crouching');
  21. $spreadsheet->getActiveSheet()->setCellValue('B1', 'Tiger');
  22. $spreadsheet->getActiveSheet()->setCellValue('A2', 'Hidden');
  23. $spreadsheet->getActiveSheet()->setCellValue('B2', 'Dragon');
  24. // Rename worksheet
  25. $helper->log('Rename worksheet');
  26. $spreadsheet->getActiveSheet()->setTitle('Simple');
  27. // Set document security
  28. $helper->log('Set cell protection');
  29. // Set sheet security
  30. $helper->log('Set sheet security');
  31. $spreadsheet->getActiveSheet()->getProtection()->setSheet(true);
  32. $spreadsheet->getActiveSheet()
  33. ->getStyle('A2:B2')
  34. ->getProtection()->setLocked(
  35. Protection::PROTECTION_UNPROTECTED
  36. );
  37. // Save
  38. $helper->write($spreadsheet, __FILE__);