Replies: 4 comments 7 replies
-
Sample ConditionalFormatting/02_Text_Comparisons does it as follows: use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\Wizard;
// Set rules for Literal Value Begins With
$cellRange = 'A2:B4';
$conditionalStyles = [];
$wizardFactory = new Wizard($cellRange);
/** @var Wizard\TextValue $textWizard */
$textWizard = $wizardFactory->newRule(Wizard::TEXT_VALUE);
$textWizard->beginsWith('H')
->setStyle($yellowStyle);
$conditionalStyles[] = $textWizard->getConditional();
$spreadsheet->getActiveSheet()
->getStyle($textWizard->getCellRange())
->setConditionalStyles($conditionalStyles); |
Beta Was this translation helpful? Give feedback.
-
You can also get the result you want without the wizard by changing 2 statements in your code: //$fpCondHits->setConditionType(Conditional::CONDITION_CELLIS);
$fpCondHits->setConditionType(Conditional::CONDITION_BEGINSWITH);
...
//$fpCondHits->addCondition("Y");
$fpCondHits->setText("Y"); |
Beta Was this translation helpful? Give feedback.
-
Unable to duplicate your problem. You will need to upload code and/or spreadsheet. The following code works for me exactly as desired - the first condition (contains minus sign) matches, applies no formatting, and stops if true. The second condition again tests for contains minus sign, so is useless except as a test, which is exactly what we're doing; this demonstrates that 'stop if true' was effective (otherwise you'd see a red background). The third condition (yellow fill for items under 50) and the fourth (green fill for items over 150) work as desired. $spreadsheet = new Spreadsheet();
$resultWS = $spreadsheet->getActiveSheet();
$resultWS->fromArray([
[102.54, 103.94, '-', 12],
[115, 150, 47, '-'],
[83, 162, 31, 29],
]);
$fpConditionalStyles = [];
$fpCondHits = new Conditional();
$fpCondHits->setConditionType(Conditional::CONDITION_CONTAINSTEXT);
$fpCondHits->setOperatorType(Conditional::OPERATOR_CONTAINSTEXT);
$fpCondHits->setText('-');
$fpCondHits->setStopIfTrue(true);
$fpConditionalStyles[] = $fpCondHits;
$fpCondHits = new Conditional();
$fpCondHits->setConditionType(Conditional::CONDITION_CONTAINSTEXT);
$fpCondHits->setOperatorType(Conditional::OPERATOR_CONTAINSTEXT);
$fpCondHits->setText('-');
$fpCondHits->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
$fpCondHits->getStyle()->getFill()->getEndColor()->setARGB(Color::COLOR_RED);
$fpConditionalStyles[] = $fpCondHits;
$fpCondHits = new Conditional();
$fpCondHits->setConditionType(Conditional::CONDITION_CELLIS);
$fpCondHits->setOperatorType(Conditional::OPERATOR_LESSTHAN);
$fpCondHits->addCondition(50);
$fpCondHits->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
$fpCondHits->getStyle()->getFill()->getEndColor()->setARGB(Color::COLOR_YELLOW);
$fpConditionalStyles[] = $fpCondHits;
$fpCondHits = new Conditional();
$fpCondHits->setConditionType(Conditional::CONDITION_CELLIS);
$fpCondHits->setOperatorType(Conditional::OPERATOR_GREATERTHAN);
$fpCondHits->addCondition(150);
$fpCondHits->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
$fpCondHits->getStyle()->getFill()->getEndColor()->setARGB(Color::COLOR_GREEN);
$fpConditionalStyles[] = $fpCondHits;
// Apply the conditional formatting
$resultWS->getStyle('A1:D3')->setConditionalStyles($fpConditionalStyles);
$outfile = 'issue.3880a.generated.xlsx';
$writer = new XlsxWriter($spreadsheet);
$writer->save($outfile); |
Beta Was this translation helpful? Give feedback.
-
I have been a fan of this library for many years. Thank you for the time you spend developing. |
Beta Was this translation helpful? Give feedback.
-
What is the appropriate why to configure conditional formatting to test a cell for string comparison. I tried the following:
Getting an error when opening the spredsheet.
I have performed a lot of conditional formatting for integers without a problem.
Beta Was this translation helpful? Give feedback.
All reactions