简体   繁体   中英

PHPExcel mixed Column Validation

I am using PHPExcel and I'd like to validate the values of the cells. Well this works pretty fine by this code:

$objValidation = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($tdCount-1,$ii)->getDataValidation();
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_WHOLE );
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
$objValidation->setAllowBlank(true);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setErrorTitle('Input error');
$objValidation->setError('Only integers between '.$min.' and '.$max.' allowed');
$objValidation->setPromptTitle('Permitted Input:');
$objValidation->setPrompt('Only integers between '.$min.' and '.$max.' allowed');
$objValidation->setFormula1(intval($min));
$objValidation->setFormula2(intval($max));

But now I'd linke to add two default values - strings - which should also be permitted, pe "cancled" or "missed". Is this possible? I did not find anything in this direction? Another idea was to create a hidden sheet but I have no idea what a corresponding value list could be.

Thank you!

UPDATE:

Thanks to Tim Williams i tried:

$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_CUSTOM);
$objValidation->setFormula1('=OR(AND('.$currCell.$ii.'>='.$min.','.$currCell.$ii.'<='.$max.'),'.$currCell.$ii.'="canceled",'.$currCell.$ii.'="missed")');

The weird thing is, that the first AND-expression is validated and i am not able to enter another number outside min / max. But i am still not able to enter "canceled" or "missed". By inserting this formula in OpenOffice i got the right booleans.

Is it the right way to use custom formulas in PHPExcel for validation?

Likely you could use a formula-based validation: for example this worked for me assuming you wanted to allow only whole numbers between 10 and 100 or the values "canceled", "missed" -

     =OR(IFERROR(AND(ROUND(H13,0)=H13,H13>=10,H13<=100),FALSE),
         OR(H13="canceled",H13="missed"))

I don't know how that translates to PHPExcel though so that's only half an answer

  • Try evaluating the Excel Formula (FORMULAS => Evaluate Formula), it might give you some clue.

  • In a similar situation that I faced, where a range of decimals or specific strings were to be allowed, TYPE checked helped solve the issue.

Something like:

=IF(TYPE(A1)=1,AND(A1>=0,A1<=10),A1="allowme")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM