mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-21 05:00:44 +00:00
ecbd702628
Phpstan hasn't identified any errors in Samples in a long time. But, as it turns out, one particular error is suppressed: ``` Variable $helper might not be defined. ``` This error would be generated by almost all samples, and the errors that it suppresses will become problematic if we move to Level 10. We are not yet committed to doing that, but it is pretty easy to write a script to change the samples so that error no longer happens, and there isn't really any reason to delay doing so. The results of that script constitute this PR.
93 lines
3.6 KiB
PHP
93 lines
3.6 KiB
PHP
<?php
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
|
|
|
|
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
|
use PhpOffice\PhpSpreadsheet\RichText\TextElement;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
$helper->log('First sheet - protected, sorts not allowed');
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setTitle('sorttrue');
|
|
$sheet->getCell('A1')->setValue(10);
|
|
$sheet->getCell('A2')->setValue(5);
|
|
$sheet->getCell('B1')->setValue(15);
|
|
$protection = $sheet->getProtection();
|
|
$protection->setPassword('testpassword');
|
|
$protection->setSheet(true);
|
|
$protection->setInsertRows(true);
|
|
$protection->setFormatCells(true);
|
|
$protection->setObjects(true);
|
|
$protection->setAutoFilter(false);
|
|
$protection->setSort(true);
|
|
$comment = $sheet->getComment('A1');
|
|
$text = new RichText();
|
|
$text->addText(new TextElement('Sort options should be grayed out. Sheet password to remove protections is testpassword for all sheets.'));
|
|
$comment->setText($text)->setHeight('120pt')->setWidth('120pt');
|
|
|
|
$helper->log('Second sheet - protected, sorts allowed, but no permitted range defined');
|
|
$sheet = $spreadsheet->createSheet();
|
|
$sheet->setTitle('sortfalse');
|
|
$sheet->getCell('A1')->setValue(10);
|
|
$sheet->getCell('A2')->setValue(5);
|
|
$sheet->getCell('B1')->setValue(15);
|
|
$protection = $sheet->getProtection();
|
|
$protection->setPassword('testpassword');
|
|
$protection->setSheet(true);
|
|
$protection->setInsertRows(true);
|
|
$protection->setFormatCells(true);
|
|
$protection->setObjects(true);
|
|
$protection->setAutoFilter(false);
|
|
$protection->setSort(false);
|
|
$comment = $sheet->getComment('A1');
|
|
$text = new RichText();
|
|
$text->addText(new TextElement('Sort options not grayed out, but no permissible sort range.'));
|
|
$comment->setText($text)->setHeight('120pt')->setWidth('120pt');
|
|
|
|
$helper->log('Third sheet - protected, sorts allowed, but only on permitted range A:A, no range password needed');
|
|
$sheet = $spreadsheet->createSheet();
|
|
$sheet->setTitle('sortfalsenocolpw');
|
|
$sheet->getCell('A1')->setValue(10);
|
|
$sheet->getCell('A2')->setValue(5);
|
|
$sheet->getCell('C1')->setValue(15);
|
|
$protection = $sheet->getProtection();
|
|
$protection->setPassword('testpassword');
|
|
$protection->setSheet(true);
|
|
$protection->setInsertRows(true);
|
|
$protection->setFormatCells(true);
|
|
$protection->setObjects(true);
|
|
$protection->setAutoFilter(false);
|
|
$protection->setSort(false);
|
|
$sheet->protectCells('A:A');
|
|
$comment = $sheet->getComment('A1');
|
|
$text = new RichText();
|
|
$text->addText(new TextElement('Column A may be sorted without a password. No sort for any other column.'));
|
|
$comment->setText($text)->setHeight('120pt')->setWidth('120pt');
|
|
|
|
$helper->log('Fourth sheet - protected, sorts allowed, but only on permitted range A:A, and range password needed');
|
|
$sheet = $spreadsheet->createSheet();
|
|
$sheet->setTitle('sortfalsecolpw');
|
|
$sheet->getCell('A1')->setValue(10);
|
|
$sheet->getCell('A2')->setValue(5);
|
|
$sheet->getCell('C1')->setValue(15);
|
|
$protection = $sheet->getProtection();
|
|
$protection->setPassword('testpassword');
|
|
$protection->setSheet(true);
|
|
$protection->setInsertRows(true);
|
|
$protection->setFormatCells(true);
|
|
$protection->setObjects(true);
|
|
$protection->setAutoFilter(false);
|
|
$protection->setSort(false);
|
|
$sheet->protectCells('A:A', 'sortpw', false, 'sortrange');
|
|
$comment = $sheet->getComment('A1');
|
|
$text = new RichText();
|
|
$text->addText(new TextElement('Column A may be sorted with password sortpw. No sort for any other column.'));
|
|
$comment->setText($text)->setHeight('120pt')->setWidth('120pt');
|
|
|
|
// Save
|
|
$helper->write($spreadsheet, __FILE__, ['Xls', 'Xlsx']);
|
|
$spreadsheet->disconnectWorksheets();
|