1

I'm trying to set conditional formatting for duplicate values using PHPExcel.

The class PHPExcel_Style_Conditional exports almost all of the conditions available in Excel like OPERATOR_GREATERTHAN, OPERATOR_LESSTHAN etc. but I can't find anything to format duplicate values like this:

enter image description here

I know, I could check for duplicates myself and format them accordingly, but using Excel's own feature set for this would be much more elegant.

Does anyone know how to do this?

flu
  • 14,307
  • 8
  • 74
  • 71

1 Answers1

2

After digging deeper into PHPExcel's code, I found out that this conditional type isn't supported yet.

So I added it myself and created a pull request for it: PHPExcel - PR: Add support for conditionally formatting duplicate values

I'll update this answer as soon as it is merged.

If you're inpatient, you could temporarily perform the two (really small) changes from the PR on your own. After that you can add conditional formatting of duplicate values to your Excel Sheets like this:

$conditional = new PHPExcel_Style_Conditional();
$conditional->setConditionType(PHPExcel_Style_Conditional::CONDITION_DUPLICATEVALUES);
$conditional->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);

$style = $sheet->getStyle('A1:A10');

$conditionalStyles = $style->getConditionalStyles();

array_push($conditionalStyles, $conditional);
$style->setConditionalStyles($conditionalStyles);
flu
  • 14,307
  • 8
  • 74
  • 71