| 1 |
<?php |
| 2 |
|
| 3 |
use PhpOffice\PhpSpreadsheet\Cell\DataValidation; |
| 4 |
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 5 |
|
| 6 |
require __DIR__ . '/../Header.php'; |
| 7 |
|
| 8 |
// Create new Spreadsheet object |
| 9 |
$helper->log('Create new Spreadsheet object'); |
| 10 |
$spreadsheet = new Spreadsheet(); |
| 11 |
|
| 12 |
// Set document properties |
| 13 |
$helper->log('Set document properties'); |
| 14 |
$spreadsheet->getProperties()->setCreator('Maarten Balliauw') |
| 15 |
->setLastModifiedBy('Maarten Balliauw') |
| 16 |
->setTitle('Office 2007 XLSX Test Document') |
| 17 |
->setSubject('Office 2007 XLSX Test Document') |
| 18 |
->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.') |
| 19 |
->setKeywords('office 2007 openxml php') |
| 20 |
->setCategory('Test result file'); |
| 21 |
|
| 22 |
// Create a first sheet |
| 23 |
$helper->log('Add data'); |
| 24 |
$spreadsheet->setActiveSheetIndex(0); |
| 25 |
$spreadsheet->getActiveSheet()->setCellValue('A1', 'Cell B3 and B5 contain data validation...') |
| 26 |
->setCellValue('A3', 'Number:') |
| 27 |
->setCellValue('B3', '10') |
| 28 |
->setCellValue('A5', 'List:') |
| 29 |
->setCellValue('B5', 'Item A') |
| 30 |
->setCellValue('A7', 'List #2:') |
| 31 |
->setCellValue('B7', 'Item #2') |
| 32 |
->setCellValue('D2', 'Item #1') |
| 33 |
->setCellValue('D3', 'Item #2') |
| 34 |
->setCellValue('D4', 'Item #3') |
| 35 |
->setCellValue('D5', 'Item #4') |
| 36 |
->setCellValue('D6', 'Item #5'); |
| 37 |
|
| 38 |
// Set data validation |
| 39 |
$helper->log('Set data validation'); |
| 40 |
$validation = $spreadsheet->getActiveSheet()->getCell('B3')->getDataValidation(); |
| 41 |
$validation->setType(DataValidation::TYPE_WHOLE); |
| 42 |
$validation->setErrorStyle(DataValidation::STYLE_STOP); |
| 43 |
$validation->setAllowBlank(true); |
| 44 |
$validation->setShowInputMessage(true); |
| 45 |
$validation->setShowErrorMessage(true); |
| 46 |
$validation->setErrorTitle('Input error'); |
| 47 |
$validation->setError('Only numbers between 10 and 20 are allowed!'); |
| 48 |
$validation->setPromptTitle('Allowed input'); |
| 49 |
$validation->setPrompt('Only numbers between 10 and 20 are allowed.'); |
| 50 |
$validation->setFormula1(10); |
| 51 |
$validation->setFormula2(20); |
| 52 |
|
| 53 |
$validation = $spreadsheet->getActiveSheet()->getCell('B5')->getDataValidation(); |
| 54 |
$validation->setType(DataValidation::TYPE_LIST); |
| 55 |
$validation->setErrorStyle(DataValidation::STYLE_INFORMATION); |
| 56 |
$validation->setAllowBlank(false); |
| 57 |
$validation->setShowInputMessage(true); |
| 58 |
$validation->setShowErrorMessage(true); |
| 59 |
$validation->setShowDropDown(true); |
| 60 |
$validation->setErrorTitle('Input error'); |
| 61 |
$validation->setError('Value is not in list.'); |
| 62 |
$validation->setPromptTitle('Pick from list'); |
| 63 |
$validation->setPrompt('Please pick a value from the drop-down list.'); |
| 64 |
$validation->setFormula1('"Item A,Item B,Item C"'); // Make sure to put the list items between " and " if your list is simply a comma-separated list of values !!! |
| 65 |
|
| 66 |
$validation = $spreadsheet->getActiveSheet()->getCell('B7')->getDataValidation(); |
| 67 |
$validation->setType(DataValidation::TYPE_LIST); |
| 68 |
$validation->setErrorStyle(DataValidation::STYLE_INFORMATION); |
| 69 |
$validation->setAllowBlank(false); |
| 70 |
$validation->setShowInputMessage(true); |
| 71 |
$validation->setShowErrorMessage(true); |
| 72 |
$validation->setShowDropDown(true); |
| 73 |
$validation->setErrorTitle('Input error'); |
| 74 |
$validation->setError('Value is not in list.'); |
| 75 |
$validation->setPromptTitle('Pick from list'); |
| 76 |
$validation->setPrompt('Please pick a value from the drop-down list.'); |
| 77 |
$validation->setFormula1('$D$2:$D$6'); // Make sure NOT to put a range of cells or a formula between " and " |
| 78 |
|
| 79 |
// Save |
| 80 |
$helper->write($spreadsheet, __FILE__); |
| 81 |
|