Data validation example limiting the length of text that can be entered in a cell

This commit is contained in:
MarkBaker 2016-03-15 11:48:49 +00:00
parent 5e89b26e9d
commit 7d1c140974
3 changed files with 78 additions and 36 deletions

View File

@ -765,6 +765,22 @@ $objValidation->setFormula1(10);
$objValidation->setFormula2(20); $objValidation->setFormula2(20);
``` ```
This validation will limit the length of text that can be entered in a cell to 6 characters.
```
$objValidation = $objPHPExcel->getActiveSheet()->getCell('B9')->getDataValidation();
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_TEXTLENGTH );
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
$objValidation->setAllowBlank(true);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setErrorTitle('Input error');
$objValidation->setError('Text exceeds maximum length');
$objValidation->setPromptTitle('Allowed input');
$objValidation->setPrompt('Maximum text length is 6 characters.');
$objValidation->setFormula1(6);
```
The following piece of code only allows an item picked from a list of data to be entered in cell B3: The following piece of code only allows an item picked from a list of data to be entered in cell B3:
```php ```php

View File

@ -70,6 +70,7 @@ $objPHPExcel->getActiveSheet()->setCellValue('A1', "Cell B3 and B5 contain data
->setCellValue('D4', "Item #3") ->setCellValue('D4', "Item #3")
->setCellValue('D5', "Item #4") ->setCellValue('D5', "Item #4")
->setCellValue('D6', "Item #5") ->setCellValue('D6', "Item #5")
->setCellValue('A9', 'Text:')
; ;
@ -114,6 +115,18 @@ $objValidation->setPromptTitle('Pick from list');
$objValidation->setPrompt('Please pick a value from the drop-down list.'); $objValidation->setPrompt('Please pick a value from the drop-down list.');
$objValidation->setFormula1('$D$2:$D$6'); // Make sure NOT to put a range of cells or a formula between " and " !!! $objValidation->setFormula1('$D$2:$D$6'); // Make sure NOT to put a range of cells or a formula between " and " !!!
$objValidation = $objPHPExcel->getActiveSheet()->getCell('B9')->getDataValidation();
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_TEXTLENGTH );
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
$objValidation->setAllowBlank(true);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setErrorTitle('Input error');
$objValidation->setError('Text exceeds maximum length');
$objValidation->setPromptTitle('Allowed input');
$objValidation->setPrompt('Maximum text length is 6 characters.');
$objValidation->setFormula1(6);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet // Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->setActiveSheetIndex(0);

View File

@ -70,6 +70,7 @@ $objPHPExcel->getActiveSheet()->setCellValue('A1', "Cell B3 and B5 contain data
->setCellValue('D4', "Item #3") ->setCellValue('D4', "Item #3")
->setCellValue('D5', "Item #4") ->setCellValue('D5', "Item #4")
->setCellValue('D6', "Item #5") ->setCellValue('D6', "Item #5")
->setCellValue('A9', 'Text:')
; ;
@ -115,6 +116,18 @@ $objValidation->setPromptTitle('Pick from list');
$objValidation->setPrompt('Please pick a value from the drop-down list.'); $objValidation->setPrompt('Please pick a value from the drop-down list.');
$objValidation->setFormula1('$D$2:$D$6'); // Make sure NOT to put a range of cells or a formula between " and " !!! $objValidation->setFormula1('$D$2:$D$6'); // Make sure NOT to put a range of cells or a formula between " and " !!!
$objValidation = $objPHPExcel->getActiveSheet()->getCell('B9')->getDataValidation();
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_TEXTLENGTH );
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
$objValidation->setAllowBlank(true);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setErrorTitle('Input error');
$objValidation->setError('Text exceeds maximum length');
$objValidation->setPromptTitle('Allowed input');
$objValidation->setPrompt('Maximum text length is 6 characters.');
$objValidation->setFormula1(6);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet // Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->setActiveSheetIndex(0);