Conditional Formatting: Duplicate Entries for Cell

Topics: Developer Forum
Aug 11, 2011 at 2:05 PM

Hello,

I was looking at the following section of code from http://phpexcel.codeplex.com/SourceControl/changeset/view/77805#1604444 for conditional formatting for Excel 2007:

 

private function _writeConditionalFormatting(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null)
	{
		// Conditional id
		$id = 1;

		// Loop through styles in the current worksheet
		foreach ($pSheet->getConditionalStylesCollection() as $cellCoordinate => $conditionalStyles) {
			foreach ($conditionalStyles as $conditional) {
				// WHY was this again?
				// if ($this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode( $conditional->getHashCode() ) == '') {
				//	continue;
				// }

				if ($conditional->getConditionType() != PHPExcel_Style_Conditional::CONDITION_NONE) {
					// conditionalFormatting
					$objWriter->startElement('conditionalFormatting');
					$objWriter->writeAttribute('sqref',	$cellCoordinate);

 

 

I believe that the 

$objWriter->startElement('conditionalFormatting');
$objWriter->writeAttribute('sqref', $cellCoordinate);

Should actually be before the foreach ($conditionalStyles as $conditional) { statement (and of course close the element correctly as well). Otherwise we take in XML code such as:

 

<conditionalFormatting sqref="A2">
<cfRule type="cellIs" dxfId="6" priority="4" operator="greaterThan">
<formula>1</formula>
</cfRule>
<cfRule type="cellIs" dxfId="5" priority="3" operator="lessThan">
<formula>2</formula>
</cfRule>
</conditionalFormatting>

 

 

And output code as such:

<conditionalFormatting sqref="A2">
<cfRule type="cellIs" dxfId="5" priority="3" operator="lessThan">
<formula>2</formula>
</cfRule>
</conditionalFormatting>
<conditionalFormatting sqref="A2">
<cfRule type="cellIs" dxfId="6" priority="4" operator="greaterThan">
<formula>1</formula>
</conditionalFormatting>

This does perhaps add empty conditional formatting without any rules, but overall it is likely to decrease the amount of XML.

Aug 11, 2011 at 5:16 PM

I tried this out, and found out why it is not done this way.  It is because an empty conditional formatting entry does not sit well with Excel!