|
| 1 | +<?php |
| 2 | + |
| 3 | +require __DIR__ . '/../Header.php'; |
| 4 | + |
| 5 | +use PhpOffice\PhpSpreadsheet\RichText\RichText; |
| 6 | +use PhpOffice\PhpSpreadsheet\RichText\TextElement; |
| 7 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 8 | + |
| 9 | +$spreadsheet = new Spreadsheet(); |
| 10 | + |
| 11 | +$helper->log('First sheet - protected, sorts not allowed'); |
| 12 | +$sheet = $spreadsheet->getActiveSheet(); |
| 13 | +$sheet->setTitle('sorttrue'); |
| 14 | +$sheet->getCell('A1')->setValue(10); |
| 15 | +$sheet->getCell('A2')->setValue(5); |
| 16 | +$sheet->getCell('B1')->setValue(15); |
| 17 | +$protection = $sheet->getProtection(); |
| 18 | +$protection->setPassword('testpassword'); |
| 19 | +$protection->setSheet(true); |
| 20 | +$protection->setInsertRows(true); |
| 21 | +$protection->setFormatCells(true); |
| 22 | +$protection->setObjects(true); |
| 23 | +$protection->setAutoFilter(false); |
| 24 | +$protection->setSort(true); |
| 25 | +$comment = $sheet->getComment('A1'); |
| 26 | +$text = new RichText(); |
| 27 | +$text->addText(new TextElement('Sort options should be grayed out. Sheet password to remove protections is testpassword for all sheets.')); |
| 28 | +$comment->setText($text)->setHeight('120pt')->setWidth('120pt'); |
| 29 | + |
| 30 | +$helper->log('Second sheet - protected, sorts allowed, but no permitted range defined'); |
| 31 | +$sheet = $spreadsheet->createSheet(); |
| 32 | +$sheet->setTitle('sortfalse'); |
| 33 | +$sheet->getCell('A1')->setValue(10); |
| 34 | +$sheet->getCell('A2')->setValue(5); |
| 35 | +$sheet->getCell('B1')->setValue(15); |
| 36 | +$protection = $sheet->getProtection(); |
| 37 | +$protection->setPassword('testpassword'); |
| 38 | +$protection->setSheet(true); |
| 39 | +$protection->setInsertRows(true); |
| 40 | +$protection->setFormatCells(true); |
| 41 | +$protection->setObjects(true); |
| 42 | +$protection->setAutoFilter(false); |
| 43 | +$protection->setSort(false); |
| 44 | +$comment = $sheet->getComment('A1'); |
| 45 | +$text = new RichText(); |
| 46 | +$text->addText(new TextElement('Sort options not grayed out, but no permissible sort range.')); |
| 47 | +$comment->setText($text)->setHeight('120pt')->setWidth('120pt'); |
| 48 | + |
| 49 | +$helper->log('Third sheet - protected, sorts allowed, but only on permitted range A:A, no range password needed'); |
| 50 | +$sheet = $spreadsheet->createSheet(); |
| 51 | +$sheet->setTitle('sortfalsenocolpw'); |
| 52 | +$sheet->getCell('A1')->setValue(10); |
| 53 | +$sheet->getCell('A2')->setValue(5); |
| 54 | +$sheet->getCell('C1')->setValue(15); |
| 55 | +$protection = $sheet->getProtection(); |
| 56 | +$protection->setPassword('testpassword'); |
| 57 | +$protection->setSheet(true); |
| 58 | +$protection->setInsertRows(true); |
| 59 | +$protection->setFormatCells(true); |
| 60 | +$protection->setObjects(true); |
| 61 | +$protection->setAutoFilter(false); |
| 62 | +$protection->setSort(false); |
| 63 | +$sheet->protectCells('A:A'); |
| 64 | +$comment = $sheet->getComment('A1'); |
| 65 | +$text = new RichText(); |
| 66 | +$text->addText(new TextElement('Column A may be sorted without a password. No sort for any other column.')); |
| 67 | +$comment->setText($text)->setHeight('120pt')->setWidth('120pt'); |
| 68 | + |
| 69 | +$helper->log('Fourth sheet - protected, sorts allowed, but only on permitted range A:A, and range password needed'); |
| 70 | +$sheet = $spreadsheet->createSheet(); |
| 71 | +$sheet->setTitle('sortfalsecolpw'); |
| 72 | +$sheet->getCell('A1')->setValue(10); |
| 73 | +$sheet->getCell('A2')->setValue(5); |
| 74 | +$sheet->getCell('C1')->setValue(15); |
| 75 | +$protection = $sheet->getProtection(); |
| 76 | +$protection->setPassword('testpassword'); |
| 77 | +$protection->setSheet(true); |
| 78 | +$protection->setInsertRows(true); |
| 79 | +$protection->setFormatCells(true); |
| 80 | +$protection->setObjects(true); |
| 81 | +$protection->setAutoFilter(false); |
| 82 | +$protection->setSort(false); |
| 83 | +$sheet->protectCells('A:A', 'sortpw', false, 'sortrange'); |
| 84 | +$comment = $sheet->getComment('A1'); |
| 85 | +$text = new RichText(); |
| 86 | +$text->addText(new TextElement('Column A may be sorted with password sortpw. No sort for any other column.')); |
| 87 | +$comment->setText($text)->setHeight('120pt')->setWidth('120pt'); |
| 88 | + |
| 89 | +// Save |
| 90 | +$helper->write($spreadsheet, __FILE__, ['Xls', 'Xlsx']); |
| 91 | +$spreadsheet->disconnectWorksheets(); |
0 commit comments