Skip to content
This repository has been archived by the owner on Jan 2, 2019. It is now read-only.

Write conditional formatting styles to worksheet instead of copying to e... #172

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions Classes/PHPExcel/Reader/Excel2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,9 @@ public function load($pFilename)
(string)$cfRule["type"] == PHPExcel_Style_Conditional::CONDITION_NONE ||
(string)$cfRule["type"] == PHPExcel_Style_Conditional::CONDITION_CELLIS ||
(string)$cfRule["type"] == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT ||
(string)$cfRule["type"] == PHPExcel_Style_Conditional::CONDITION_EXPRESSION
(string)$cfRule["type"] == PHPExcel_Style_Conditional::CONDITION_EXPRESSION ||
(string)$cfRule["type"] == PHPExcel_Style_Conditional::CONDITION_BLANK ||
(string)$cfRule["type"] == PHPExcel_Style_Conditional::CONDITION_NOTBLANK
) && isset($dxfs[intval($cfRule["dxfId"])])
) {
$conditionals[(string) $conditional["sqref"]][intval($cfRule["priority"])] = $cfRule;
Expand All @@ -917,13 +919,15 @@ public function load($pFilename)
}

foreach ($conditionals as $ref => $cfRules) {
ksort($cfRules);
$conditionalStyles = array();
foreach ($cfRules as $cfRule) {
$objConditional = new PHPExcel_Style_Conditional();
$objConditional->setConditionType((string)$cfRule["type"]);
$objConditional->setOperatorType((string)$cfRule["operator"]);

$objConditional->setPriority((int)$cfRule["priority"]);
$objConditional->setStopIfTrue((string)$cfRule["stopIfTrue"]);

if ((string)$cfRule["text"] != '') {
$objConditional->setText((string)$cfRule["text"]);
}
Expand All @@ -935,15 +939,15 @@ public function load($pFilename)
} else {
$objConditional->addCondition((string)$cfRule->formula);
}
$objConditional->setStyle(clone $dxfs[intval($cfRule["dxfId"])]);

// the rule may unset formatting in which case dxfId will not be present
if(isset($cfRule["dxfId"]])){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parse error: syntax error, unexpected ']' in PHPExcel\Reader\Excel2007.php on line 944

$objConditional->setStyle(clone $dxfs[(int)$cfRule["dxfId"]]);
}
$conditionalStyles[] = $objConditional;
}

// Extract all cell references in $ref
$aReferences = PHPExcel_Cell::extractAllCellReferencesInRange($ref);
foreach ($aReferences as $reference) {
$docSheet->getStyle($reference)->setConditionalStyles($conditionalStyles);
}
$docSheet->setConditionalStyles($ref, $conditionalStyles);
}
}

Expand Down
60 changes: 60 additions & 0 deletions Classes/PHPExcel/Style/Conditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class PHPExcel_Style_Conditional implements PHPExcel_IComparable
const CONDITION_CELLIS = 'cellIs';
const CONDITION_CONTAINSTEXT = 'containsText';
const CONDITION_EXPRESSION = 'expression';
const CONDITION_CONTAINSBLANKS = 'containsBlanks';
const CONDITION_NOTCONTAINSBLANKS = 'notContainsBlanks';

/* Operator types */
const OPERATOR_NONE = '';
Expand All @@ -55,6 +57,8 @@ class PHPExcel_Style_Conditional implements PHPExcel_IComparable
const OPERATOR_NOTCONTAINS = 'notContains';
const OPERATOR_BETWEEN = 'between';

const STOP_IF_TRUE = '1';

/**
* Condition type
*
Expand All @@ -76,6 +80,13 @@ class PHPExcel_Style_Conditional implements PHPExcel_IComparable
*/
private $_text;

/**
* Stop If True
*
* @var string
*/
private $_stopIfTrue = '';

/**
* Condition
*
Expand Down Expand Up @@ -163,6 +174,55 @@ public function setText($value = null) {
return $this;
}

/**
* Get Stop If True
*
* Whether to stop processing conditional formatting rules for a given cell if this rule evaluates to true.
*
* @return string
*/
public function getStopIfTrue() {
return $this->_stopIfTrue;
}

/**
* Set Stop If True
*
* @param string $pValue
* @return PHPExcel_Style_Conditional
*/
public function setStopIfTrue($pValue = '') {
if($pValue === PHPExcel_Style_Conditional::STOP_IF_TRUE){
$this->_stopIfTrue = $pValue;
} else{
// treat all unknown as "unset"
$this->_stopIfTrue = '';
}
return $this;
}

/**
* Get Priority
*
* Priority is the order in which conditional formatting rules will apply to the affected range(s). Lower priority values run first.
*
* @return int
*/
public function getPriority() {
return $this->_priority;
}

/**
* Set Priority
*
* @param int $pValue
* @return PHPExcel_Style_Conditional
*/
public function setStopIfTrue($pValue = 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fatal error: Cannot redeclare PHPExcel_Style_Conditional::setStopIfTrue() in PHPExcel\Style\Conditional.php on line 221

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you mean setPriority() instead of setStopIfTrue(), not?

$this->_priority = $pValue;
return $this;
}

/**
* Get Condition
*
Expand Down
39 changes: 31 additions & 8 deletions Classes/PHPExcel/Writer/Excel2007/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,25 +465,40 @@ private function _writeConditionalFormatting(PHPExcel_Shared_XMLWriter $objWrite

// Loop through styles in the current worksheet
foreach ($pSheet->getConditionalStylesCollection() as $cellCoordinate => $conditionalStyles) {
$cell_ref = '';
if(count($conditionalStyles) > 0){ // write conditionalStyles only if we are going to have some cfRules
$objWriter->startElement('conditionalFormatting');
$objWriter->writeAttribute('sqref', $cellCoordinate);

/* inside certain conditions a single cell reference is required,
* but the conditional style may be applied to an range or a set
* of ranges. Any single one of the cells in this range seems to
* be sufficient and seems to match sheets written by Excel. */
$cell_ref = array_shift(PHPExcel_Cell::extractAllCellReferencesInRange($cellCoordinate));
}

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);

// cfRule
$objWriter->startElement('cfRule');
$objWriter->writeAttribute('type', $conditional->getConditionType());
$objWriter->writeAttribute('dxfId', $this->getParentWriter()->getStylesConditionalHashTable()->getIndexForHashCode( $conditional->getHashCode() ));
$objWriter->writeAttribute('priority', $id++);

if($conditional->getStopIfTrue() !== ''){
$objWriter->writeAttribute('stopIfTrue', $conditional->getStopIfTrue());
}

if (($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CELLIS
||
$conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT)
$conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT)
&& $conditional->getOperatorType() != PHPExcel_Style_Conditional::OPERATOR_NONE) {
$objWriter->writeAttribute('operator', $conditional->getOperatorType());
}
Expand All @@ -496,33 +511,41 @@ private function _writeConditionalFormatting(PHPExcel_Shared_XMLWriter $objWrite
if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
&& $conditional->getOperatorType() == PHPExcel_Style_Conditional::OPERATOR_CONTAINSTEXT
&& !is_null($conditional->getText())) {
$objWriter->writeElement('formula', 'NOT(ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . ')))');
$objWriter->writeElement('formula', 'NOT(ISERROR(SEARCH("' . $conditional->getText() . '",' . $cell_ref . ')))');
} else if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
&& $conditional->getOperatorType() == PHPExcel_Style_Conditional::OPERATOR_BEGINSWITH
&& !is_null($conditional->getText())) {
$objWriter->writeElement('formula', 'LEFT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
$objWriter->writeElement('formula', 'LEFT(' . $cell_ref . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
} else if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
&& $conditional->getOperatorType() == PHPExcel_Style_Conditional::OPERATOR_ENDSWITH
&& !is_null($conditional->getText())) {
$objWriter->writeElement('formula', 'RIGHT(' . $cellCoordinate . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
$objWriter->writeElement('formula', 'RIGHT(' . $cell_ref . ',' . strlen($conditional->getText()) . ')="' . $conditional->getText() . '"');
} else if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
&& $conditional->getOperatorType() == PHPExcel_Style_Conditional::OPERATOR_NOTCONTAINS
&& !is_null($conditional->getText())) {
$objWriter->writeElement('formula', 'ISERROR(SEARCH("' . $conditional->getText() . '",' . $cellCoordinate . '))');
$objWriter->writeElement('formula', 'ISERROR(SEARCH("' . $conditional->getText() . '",' . $cell_ref . '))');
} else if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CELLIS
|| $conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSTEXT
|| $conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_EXPRESSION) {
foreach ($conditional->getConditions() as $formula) {
// Formula
$objWriter->writeElement('formula', $formula);
}
} else if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_CONTAINSBLANKS
&& $cell_ref !== '') {
$objWriter->writeElement('formula', 'LEFT(TRIM(' . $cell_ref . '))=0');
} else if ($conditional->getConditionType() == PHPExcel_Style_Conditional::CONDITION_NOTCONTAINSBLANKS
&& $cell_ref !== '') {
$objWriter->writeElement('formula', 'LEFT(TRIM(' . $cell_ref . '))>0');
}

$objWriter->endElement();

$objWriter->endElement();
}
}
if(count($conditionalStyles) > 0){
$objWriter->endElement();
}
}
}

Expand Down