Sitelet https://github.com/linuxtechie/PHPExcel/commit/6d21efc173b54e61fadeaaee971bfa219bf806c0
Skip to content

Commit 6d21efc

Browse files
author
Mark Baker
committed
Updated data validation example to show a list derived from data in the worksheet rather than from a comma-separated string
1 parent 9d22445 commit 6d21efc

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

‎Examples/15datavalidation.php‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@
6262
->setCellValue('A3', "Number:")
6363
->setCellValue('B3', "10")
6464
->setCellValue('A5', "List:")
65-
->setCellValue('B5', "Item A");
65+
->setCellValue('B5', "Item A")
66+
->setCellValue('A7', "List #2:")
67+
->setCellValue('B7', "Item #2")
68+
->setCellValue('D2', "Item #1")
69+
->setCellValue('D3', "Item #2")
70+
->setCellValue('D4', "Item #3")
71+
->setCellValue('D5', "Item #4")
72+
->setCellValue('D6', "Item #5")
73+
;
6674

6775

6876
// Set data validation
@@ -91,7 +99,21 @@
9199
$objValidation->setError('Value is not in list.');
92100
$objValidation->setPromptTitle('Pick from list');
93101
$objValidation->setPrompt('Please pick a value from the drop-down list.');
94-
$objValidation->setFormula1('"Item A,Item B,Item C"'); // Make sure to put the list items between " and " !!!
102+
$objValidation->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 !!!
103+
104+
105+
$objValidation = $objPHPExcel->getActiveSheet()->getCell('B7')->getDataValidation();
106+
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_LIST );
107+
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_INFORMATION );
108+
$objValidation->setAllowBlank(false);
109+
$objValidation->setShowInputMessage(true);
110+
$objValidation->setShowErrorMessage(true);
111+
$objValidation->setShowDropDown(true);
112+
$objValidation->setErrorTitle('Input error');
113+
$objValidation->setError('Value is not in list.');
114+
$objValidation->setPromptTitle('Pick from list');
115+
$objValidation->setPrompt('Please pick a value from the drop-down list.');
116+
$objValidation->setFormula1('$D$2:$D$6'); // Make sure NOT to put a range of cells or a formula between " and " !!!
95117

96118

97119
// Set active sheet index to the first sheet, so Excel opens this as the first sheet

0 commit comments

Comments
 (0)