39_Dropdown.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
  3. use PhpOffice\PhpSpreadsheet\NamedRange;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. require __DIR__ . '/../Header.php';
  6. // Create new Spreadsheet object
  7. $helper->log('Create new Spreadsheet object');
  8. $spreadsheet = new Spreadsheet();
  9. // Set document properties
  10. $helper->log('Set document properties');
  11. $spreadsheet->getProperties()
  12. ->setCreator('PHPOffice')
  13. ->setLastModifiedBy('PHPOffice')
  14. ->setTitle('PhpSpreadsheet Test Document')
  15. ->setSubject('PhpSpreadsheet Test Document')
  16. ->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.')
  17. ->setKeywords('Office PhpSpreadsheet php')
  18. ->setCategory('Test result file');
  19. function transpose($value)
  20. {
  21. return [$value];
  22. }
  23. // Add some data
  24. $continentColumn = 'D';
  25. $column = 'F';
  26. // Set data for dropdowns
  27. $continents = glob(__DIR__ . '/data/continents/*');
  28. foreach ($continents as $key => $filename) {
  29. $continent = pathinfo($filename, PATHINFO_FILENAME);
  30. $helper->log("Loading $continent");
  31. $continent = str_replace(' ', '_', $continent);
  32. $countries = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  33. $countryCount = count($countries);
  34. // Transpose $countries from a row to a column array
  35. $countries = array_map('transpose', $countries);
  36. $spreadsheet->getActiveSheet()
  37. ->fromArray($countries, null, $column . '1');
  38. $spreadsheet->addNamedRange(
  39. new NamedRange(
  40. $continent,
  41. $spreadsheet->getActiveSheet(),
  42. $column . '1:' . $column . $countryCount
  43. )
  44. );
  45. $spreadsheet->getActiveSheet()
  46. ->getColumnDimension($column)
  47. ->setVisible(false);
  48. $spreadsheet->getActiveSheet()
  49. ->setCellValue($continentColumn . ($key + 1), $continent);
  50. ++$column;
  51. }
  52. // Hide the dropdown data
  53. $spreadsheet->getActiveSheet()
  54. ->getColumnDimension($continentColumn)
  55. ->setVisible(false);
  56. $spreadsheet->addNamedRange(
  57. new NamedRange(
  58. 'Continents',
  59. $spreadsheet->getActiveSheet(),
  60. $continentColumn . '1:' . $continentColumn . count($continents)
  61. )
  62. );
  63. // Set selection cells
  64. $spreadsheet->getActiveSheet()
  65. ->setCellValue('A1', 'Continent:');
  66. $spreadsheet->getActiveSheet()
  67. ->setCellValue('B1', 'Select continent');
  68. $spreadsheet->getActiveSheet()
  69. ->setCellValue('B3', '=' . $column . 1);
  70. $spreadsheet->getActiveSheet()
  71. ->setCellValue('B3', 'Select country');
  72. $spreadsheet->getActiveSheet()
  73. ->getStyle('A1:A3')
  74. ->getFont()->setBold(true);
  75. // Set linked validators
  76. $validation = $spreadsheet->getActiveSheet()
  77. ->getCell('B1')
  78. ->getDataValidation();
  79. $validation->setType(DataValidation::TYPE_LIST)
  80. ->setErrorStyle(DataValidation::STYLE_INFORMATION)
  81. ->setAllowBlank(false)
  82. ->setShowInputMessage(true)
  83. ->setShowErrorMessage(true)
  84. ->setShowDropDown(true)
  85. ->setErrorTitle('Input error')
  86. ->setError('Continent is not in the list.')
  87. ->setPromptTitle('Pick from the list')
  88. ->setPrompt('Please pick a continent from the drop-down list.')
  89. ->setFormula1('=Continents');
  90. $spreadsheet->getActiveSheet()
  91. ->setCellValue('A3', 'Country:');
  92. $spreadsheet->getActiveSheet()
  93. ->getStyle('A3')
  94. ->getFont()->setBold(true);
  95. $validation = $spreadsheet->getActiveSheet()
  96. ->getCell('B3')
  97. ->getDataValidation();
  98. $validation->setType(DataValidation::TYPE_LIST)
  99. ->setErrorStyle(DataValidation::STYLE_INFORMATION)
  100. ->setAllowBlank(false)
  101. ->setShowInputMessage(true)
  102. ->setShowErrorMessage(true)
  103. ->setShowDropDown(true)
  104. ->setErrorTitle('Input error')
  105. ->setError('Country is not in the list.')
  106. ->setPromptTitle('Pick from the list')
  107. ->setPrompt('Please pick a country from the drop-down list.')
  108. ->setFormula1('=INDIRECT($B$1)');
  109. $spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(12);
  110. $spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(30);
  111. // Save
  112. $helper->write($spreadsheet, __FILE__);