| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-17 22:02:17 +00:00
										 |  |  | use PhpOffice\PhpSpreadsheet\Cell\DataValidation; | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\NamedRange; | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\Spreadsheet; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-01 08:48:59 +00:00
										 |  |  | require __DIR__ . '/../Header.php'; | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Create new Spreadsheet object
 | 
					
						
							|  |  |  | $helper->log('Create new Spreadsheet object'); | 
					
						
							| 
									
										
										
										
											2017-05-17 22:02:17 +00:00
										 |  |  | $spreadsheet = new Spreadsheet(); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Set document properties
 | 
					
						
							|  |  |  | $helper->log('Set document properties'); | 
					
						
							|  |  |  | $spreadsheet->getProperties() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->setCreator('PHPOffice') | 
					
						
							|  |  |  |     ->setLastModifiedBy('PHPOffice') | 
					
						
							|  |  |  |     ->setTitle('PhpSpreadsheet Test Document') | 
					
						
							|  |  |  |     ->setSubject('PhpSpreadsheet Test Document') | 
					
						
							|  |  |  |     ->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.') | 
					
						
							|  |  |  |     ->setKeywords('Office PhpSpreadsheet php') | 
					
						
							|  |  |  |     ->setCategory('Test result file'); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | function transpose($value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return [$value]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Add some data
 | 
					
						
							|  |  |  | $continentColumn = 'D'; | 
					
						
							|  |  |  | $column = 'F'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Set data for dropdowns
 | 
					
						
							| 
									
										
										
										
											2017-10-01 08:48:59 +00:00
										 |  |  | $continents = glob(__DIR__ . '/data/continents/*'); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | foreach ($continents as $key => $filename) { | 
					
						
							|  |  |  |     $continent = pathinfo($filename, PATHINFO_FILENAME); | 
					
						
							| 
									
										
										
										
											2017-10-01 08:48:59 +00:00
										 |  |  |     $helper->log("Loading $continent"); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  |     $continent = str_replace(' ', '_', $continent); | 
					
						
							|  |  |  |     $countries = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | 
					
						
							|  |  |  |     $countryCount = count($countries); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Transpose $countries from a row to a column array
 | 
					
						
							|  |  |  |     $countries = array_map('transpose', $countries); | 
					
						
							|  |  |  |     $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |         ->fromArray($countries, null, $column . '1'); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  |     $spreadsheet->addNamedRange( | 
					
						
							| 
									
										
										
										
											2017-05-17 22:02:17 +00:00
										 |  |  |         new NamedRange( | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  |             $continent, | 
					
						
							|  |  |  |             $spreadsheet->getActiveSheet(), | 
					
						
							|  |  |  |             $column . '1:' . $column . $countryCount | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |         ->getColumnDimension($column) | 
					
						
							|  |  |  |         ->setVisible(false); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |         ->setCellValue($continentColumn . ($key + 1), $continent); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ++$column; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Hide the dropdown data
 | 
					
						
							|  |  |  | $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->getColumnDimension($continentColumn) | 
					
						
							|  |  |  |     ->setVisible(false); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | $spreadsheet->addNamedRange( | 
					
						
							| 
									
										
										
										
											2017-05-17 22:02:17 +00:00
										 |  |  |     new NamedRange( | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  |         'Continents', | 
					
						
							|  |  |  |         $spreadsheet->getActiveSheet(), | 
					
						
							|  |  |  |         $continentColumn . '1:' . $continentColumn . count($continents) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Set selection cells
 | 
					
						
							|  |  |  | $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->setCellValue('A1', 'Continent:'); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->setCellValue('B1', 'Select continent'); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->setCellValue('B3', '=' . $column . 1); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->setCellValue('B3', 'Select country'); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->getStyle('A1:A3') | 
					
						
							|  |  |  |     ->getFont()->setBold(true); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Set linked validators
 | 
					
						
							|  |  |  | $validation = $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->getCell('B1') | 
					
						
							|  |  |  |     ->getDataValidation(); | 
					
						
							| 
									
										
										
										
											2017-05-17 22:02:17 +00:00
										 |  |  | $validation->setType(DataValidation::TYPE_LIST) | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->setErrorStyle(DataValidation::STYLE_INFORMATION) | 
					
						
							|  |  |  |     ->setAllowBlank(false) | 
					
						
							|  |  |  |     ->setShowInputMessage(true) | 
					
						
							|  |  |  |     ->setShowErrorMessage(true) | 
					
						
							|  |  |  |     ->setShowDropDown(true) | 
					
						
							|  |  |  |     ->setErrorTitle('Input error') | 
					
						
							|  |  |  |     ->setError('Continent is not in the list.') | 
					
						
							|  |  |  |     ->setPromptTitle('Pick from the list') | 
					
						
							|  |  |  |     ->setPrompt('Please pick a continent from the drop-down list.') | 
					
						
							|  |  |  |     ->setFormula1('=Continents'); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->setCellValue('A3', 'Country:'); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->getStyle('A3') | 
					
						
							|  |  |  |     ->getFont()->setBold(true); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | $validation = $spreadsheet->getActiveSheet() | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->getCell('B3') | 
					
						
							|  |  |  |     ->getDataValidation(); | 
					
						
							| 
									
										
										
										
											2017-05-17 22:02:17 +00:00
										 |  |  | $validation->setType(DataValidation::TYPE_LIST) | 
					
						
							| 
									
										
										
										
											2018-01-28 06:59:38 +00:00
										 |  |  |     ->setErrorStyle(DataValidation::STYLE_INFORMATION) | 
					
						
							|  |  |  |     ->setAllowBlank(false) | 
					
						
							|  |  |  |     ->setShowInputMessage(true) | 
					
						
							|  |  |  |     ->setShowErrorMessage(true) | 
					
						
							|  |  |  |     ->setShowDropDown(true) | 
					
						
							|  |  |  |     ->setErrorTitle('Input error') | 
					
						
							|  |  |  |     ->setError('Country is not in the list.') | 
					
						
							|  |  |  |     ->setPromptTitle('Pick from the list') | 
					
						
							|  |  |  |     ->setPrompt('Please pick a country from the drop-down list.') | 
					
						
							|  |  |  |     ->setFormula1('=INDIRECT($B$1)'); | 
					
						
							| 
									
										
										
										
											2016-08-31 16:15:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | $spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(12); | 
					
						
							|  |  |  | $spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(30); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Save
 | 
					
						
							|  |  |  | $helper->write($spreadsheet, __FILE__); |