Skip to content

Commit 7d1c140

Browse files
author
MarkBaker
committed
Data validation example limiting the length of text that can be entered in a cell
1 parent 5e89b26 commit 7d1c140

File tree

3 files changed

+78
-36
lines changed

3 files changed

+78
-36
lines changed

Documentation/markdown/Overview/08-Recipes.md

+16
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,22 @@ $objValidation->setFormula1(10);
765765
$objValidation->setFormula2(20);
766766
```
767767

768+
This validation will limit the length of text that can be entered in a cell to 6 characters.
769+
770+
```
771+
$objValidation = $objPHPExcel->getActiveSheet()->getCell('B9')->getDataValidation();
772+
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_TEXTLENGTH );
773+
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
774+
$objValidation->setAllowBlank(true);
775+
$objValidation->setShowInputMessage(true);
776+
$objValidation->setShowErrorMessage(true);
777+
$objValidation->setErrorTitle('Input error');
778+
$objValidation->setError('Text exceeds maximum length');
779+
$objValidation->setPromptTitle('Allowed input');
780+
$objValidation->setPrompt('Maximum text length is 6 characters.');
781+
$objValidation->setFormula1(6);
782+
```
783+
768784
The following piece of code only allows an item picked from a list of data to be entered in cell B3:
769785

770786
```php

Examples/15datavalidation-xls.php

+31-18
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,31 @@
4747
// Set document properties
4848
echo date('H:i:s') , " Set document properties" , EOL;
4949
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
50-
->setLastModifiedBy("Maarten Balliauw")
51-
->setTitle("Office 2007 XLSX Test Document")
52-
->setSubject("Office 2007 XLSX Test Document")
53-
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
54-
->setKeywords("office 2007 openxml php")
55-
->setCategory("Test result file");
50+
->setLastModifiedBy("Maarten Balliauw")
51+
->setTitle("Office 2007 XLSX Test Document")
52+
->setSubject("Office 2007 XLSX Test Document")
53+
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
54+
->setKeywords("office 2007 openxml php")
55+
->setCategory("Test result file");
5656

5757

5858
// Create a first sheet
5959
echo date('H:i:s') , " Add data" , EOL;
6060
$objPHPExcel->setActiveSheetIndex(0);
6161
$objPHPExcel->getActiveSheet()->setCellValue('A1', "Cell B3 and B5 contain data validation...")
62-
->setCellValue('A3', "Number:")
63-
->setCellValue('B3', "10")
64-
->setCellValue('A5', "List:")
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-
;
62+
->setCellValue('A3', "Number:")
63+
->setCellValue('B3', "10")
64+
->setCellValue('A5', "List:")
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+
->setCellValue('A9', 'Text:')
74+
;
7475

7576

7677
// Set data validation
@@ -114,6 +115,18 @@
114115
$objValidation->setPrompt('Please pick a value from the drop-down list.');
115116
$objValidation->setFormula1('$D$2:$D$6'); // Make sure NOT to put a range of cells or a formula between " and " !!!
116117

118+
$objValidation = $objPHPExcel->getActiveSheet()->getCell('B9')->getDataValidation();
119+
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_TEXTLENGTH );
120+
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
121+
$objValidation->setAllowBlank(true);
122+
$objValidation->setShowInputMessage(true);
123+
$objValidation->setShowErrorMessage(true);
124+
$objValidation->setErrorTitle('Input error');
125+
$objValidation->setError('Text exceeds maximum length');
126+
$objValidation->setPromptTitle('Allowed input');
127+
$objValidation->setPrompt('Maximum text length is 6 characters.');
128+
$objValidation->setFormula1(6);
129+
117130

118131
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
119132
$objPHPExcel->setActiveSheetIndex(0);

Examples/15datavalidation.php

+31-18
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,31 @@
4747
// Set document properties
4848
echo date('H:i:s') , " Set document properties" , EOL;
4949
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
50-
->setLastModifiedBy("Maarten Balliauw")
51-
->setTitle("Office 2007 XLSX Test Document")
52-
->setSubject("Office 2007 XLSX Test Document")
53-
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
54-
->setKeywords("office 2007 openxml php")
55-
->setCategory("Test result file");
50+
->setLastModifiedBy("Maarten Balliauw")
51+
->setTitle("Office 2007 XLSX Test Document")
52+
->setSubject("Office 2007 XLSX Test Document")
53+
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
54+
->setKeywords("office 2007 openxml php")
55+
->setCategory("Test result file");
5656

5757

5858
// Create a first sheet
5959
echo date('H:i:s') , " Add data" , EOL;
6060
$objPHPExcel->setActiveSheetIndex(0);
6161
$objPHPExcel->getActiveSheet()->setCellValue('A1', "Cell B3 and B5 contain data validation...")
62-
->setCellValue('A3', "Number:")
63-
->setCellValue('B3', "10")
64-
->setCellValue('A5', "List:")
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-
;
62+
->setCellValue('A3', "Number:")
63+
->setCellValue('B3', "10")
64+
->setCellValue('A5', "List:")
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+
->setCellValue('A9', 'Text:')
74+
;
7475

7576

7677
// Set data validation
@@ -115,6 +116,18 @@
115116
$objValidation->setPrompt('Please pick a value from the drop-down list.');
116117
$objValidation->setFormula1('$D$2:$D$6'); // Make sure NOT to put a range of cells or a formula between " and " !!!
117118

119+
$objValidation = $objPHPExcel->getActiveSheet()->getCell('B9')->getDataValidation();
120+
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_TEXTLENGTH );
121+
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
122+
$objValidation->setAllowBlank(true);
123+
$objValidation->setShowInputMessage(true);
124+
$objValidation->setShowErrorMessage(true);
125+
$objValidation->setErrorTitle('Input error');
126+
$objValidation->setError('Text exceeds maximum length');
127+
$objValidation->setPromptTitle('Allowed input');
128+
$objValidation->setPrompt('Maximum text length is 6 characters.');
129+
$objValidation->setFormula1(6);
130+
118131

119132
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
120133
$objPHPExcel->setActiveSheetIndex(0);

0 commit comments

Comments
 (0)