15_Datavalidation.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  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('Maarten Balliauw')
  11. ->setLastModifiedBy('Maarten Balliauw')
  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. // Create a first sheet
  18. $helper->log('Add data');
  19. $spreadsheet->setActiveSheetIndex(0);
  20. $spreadsheet->getActiveSheet()->setCellValue('A1', 'Cell B3 and B5 contain data validation...')
  21. ->setCellValue('A3', 'Number:')
  22. ->setCellValue('B3', '10')
  23. ->setCellValue('A5', 'List:')
  24. ->setCellValue('B5', 'Item A')
  25. ->setCellValue('A7', 'List #2:')
  26. ->setCellValue('B7', 'Item #2')
  27. ->setCellValue('D2', 'Item #1')
  28. ->setCellValue('D3', 'Item #2')
  29. ->setCellValue('D4', 'Item #3')
  30. ->setCellValue('D5', 'Item #4')
  31. ->setCellValue('D6', 'Item #5');
  32. // Set data validation
  33. $helper->log('Set data validation');
  34. $validation = $spreadsheet->getActiveSheet()->getCell('B3')->getDataValidation();
  35. $validation->setType(DataValidation::TYPE_WHOLE);
  36. $validation->setErrorStyle(DataValidation::STYLE_STOP);
  37. $validation->setAllowBlank(true);
  38. $validation->setShowInputMessage(true);
  39. $validation->setShowErrorMessage(true);
  40. $validation->setErrorTitle('Input error');
  41. $validation->setError('Only numbers between 10 and 20 are allowed!');
  42. $validation->setPromptTitle('Allowed input');
  43. $validation->setPrompt('Only numbers between 10 and 20 are allowed.');
  44. $validation->setFormula1(10);
  45. $validation->setFormula2(20);
  46. $validation = $spreadsheet->getActiveSheet()->getCell('B5')->getDataValidation();
  47. $validation->setType(DataValidation::TYPE_LIST);
  48. $validation->setErrorStyle(DataValidation::STYLE_INFORMATION);
  49. $validation->setAllowBlank(false);
  50. $validation->setShowInputMessage(true);
  51. $validation->setShowErrorMessage(true);
  52. $validation->setShowDropDown(true);
  53. $validation->setErrorTitle('Input error');
  54. $validation->setError('Value is not in list.');
  55. $validation->setPromptTitle('Pick from list');
  56. $validation->setPrompt('Please pick a value from the drop-down list.');
  57. $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 !!!
  58. $validation = $spreadsheet->getActiveSheet()->getCell('B7')->getDataValidation();
  59. $validation->setType(DataValidation::TYPE_LIST);
  60. $validation->setErrorStyle(DataValidation::STYLE_INFORMATION);
  61. $validation->setAllowBlank(false);
  62. $validation->setShowInputMessage(true);
  63. $validation->setShowErrorMessage(true);
  64. $validation->setShowDropDown(true);
  65. $validation->setErrorTitle('Input error');
  66. $validation->setError('Value is not in list.');
  67. $validation->setPromptTitle('Pick from list');
  68. $validation->setPrompt('Please pick a value from the drop-down list.');
  69. $validation->setFormula1('$D$2:$D$6'); // Make sure NOT to put a range of cells or a formula between " and "
  70. // Save
  71. $helper->write($spreadsheet, __FILE__);