From 6f91dd9f59a01eec3df6ab441ca762bbee1dd88d Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sun, 12 Feb 2012 18:04:22 +0000 Subject: [PATCH] Bugfix: Work item 17403 - duplicateStyle() method doesn't duplicate conditional formats Added an equivalent duplicateConditionalStyle() method for duplicating conditional styles git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@86383 2327b42d-5241-43d6-9e2a-de5ac946f064 --- Classes/PHPExcel/Worksheet.php | 142 ++++++++++++++++++++++++++++++++- changelog.txt | 2 + 2 files changed, 143 insertions(+), 1 deletion(-) diff --git a/Classes/PHPExcel/Worksheet.php b/Classes/PHPExcel/Worksheet.php index d805626c..7b31385e 100644 --- a/Classes/PHPExcel/Worksheet.php +++ b/Classes/PHPExcel/Worksheet.php @@ -101,6 +101,13 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable */ private $_drawingCollection = null; + /** + * Collection of Chart objects + * + * @var PHPExcel_Chart[] + */ + private $_chartCollection = array(); + /** * Worksheet title * @@ -348,6 +355,9 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable // Drawing collection $this->_drawingCollection = new ArrayObject(); + // Chart collection + $this->_chartCollection = new ArrayObject(); + // Protection $this->_protection = new PHPExcel_Worksheet_Protection(); @@ -490,6 +500,80 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable return $this->_drawingCollection; } + /** + * Get collection of charts + * + * @return PHPExcel_Chart[] + */ + public function getChartCollection() + { + return $this->_chartCollection; + } + + /** + * Add chart + * + * @param PHPExcel_Chart $pChart + * @param int|null $iChartIndex Index where chart should go (0,1,..., or null for last) + * @return PHPExcel_Chart + * @throws Exception + */ + public function addChart(PHPExcel_Chart $pChart = null, $iChartIndex = null) + { + if (is_null($iChartIndex)) { + $this->_chartCollection[] = $pChart; + } else { + // Insert the chart at the requested index + array_splice($this->_chartCollection, $iChartIndex, 0, array($pChart)); + } + + return $pChart; + } + + public function getChartCount() + { + return count($this->_chartCollection); + } + + public function getChartByIndex($index = null) + { + $chartCount = count($this->_chartCollection); + if ($chartCount == 0) { + return false; + } + if (is_null($index)) { + $index = --$chartCount; + } + if (!isset($this->_chartCollection[$index])) { + return false; + } + + return $this->_chartCollection[$index]; + } + + public function getChartNames() + { + $chartNames = array(); + foreach($this->_chartCollection as $chart) { + $chartNames[] = $chart->getName(); + } + return $chartNames; + } + + public function getChartByName($chartName = '') + { + $chartCount = count($this->_chartCollection); + if ($chartCount == 0) { + return false; + } + foreach($this->_chartCollection as $index => $chart) { + if ($chart->getName() == $chartName) { + return $this->_chartCollection[$index]; + } + } + return false; + } + /** * Refresh column dimensions * @@ -1314,7 +1398,7 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable * @throws Exception * @return PHPExcel_Worksheet */ - public function setSharedStyle(PHPExcel_Style $pSharedCellStyle = null, $pRange = '') + public function setSharedStyle(PHPExcel_Style $pSharedCellStyle = null, $pRange = '') { $this->duplicateStyle($pSharedCellStyle, $pRange); return $this; @@ -1384,6 +1468,62 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable return $this; } + /** + * Duplicate conditional style to a range of cells + * + * Please note that this will overwrite existing cell styles for cells in range! + * + * @param array of PHPExcel_Style_Conditional $pCellStyle Cell style to duplicate + * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1") + * @throws Exception + * @return PHPExcel_Worksheet + */ + public function duplicateConditionalStyle(array $pCellStyle = null, $pRange = '') + { + foreach($pCellStyle as $cellStyle) { + if (!is_a($cellStyle,'PHPExcel_Style_Conditional')) { + throw new Exception('Style is not a conditional style'); + } + } + + // Uppercase coordinate + $pRange = strtoupper($pRange); + + // Is it a cell range or a single cell? + $rangeA = ''; + $rangeB = ''; + if (strpos($pRange, ':') === false) { + $rangeA = $pRange; + $rangeB = $pRange; + } else { + list($rangeA, $rangeB) = explode(':', $pRange); + } + + // Calculate range outer borders + $rangeStart = PHPExcel_Cell::coordinateFromString($rangeA); + $rangeEnd = PHPExcel_Cell::coordinateFromString($rangeB); + + // Translate column into index + $rangeStart[0] = PHPExcel_Cell::columnIndexFromString($rangeStart[0]) - 1; + $rangeEnd[0] = PHPExcel_Cell::columnIndexFromString($rangeEnd[0]) - 1; + + // Make sure we can loop upwards on rows and columns + if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) { + $tmp = $rangeStart; + $rangeStart = $rangeEnd; + $rangeEnd = $tmp; + } + + // Loop through cells and apply styles + for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) { + for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) { + $this->setConditionalStyles(PHPExcel_Cell::stringFromColumnIndex($col) . $row, $pCellStyle); + } + } + + return $this; + } + /** * Duplicate cell style array to a range of cells * diff --git a/changelog.txt b/changelog.txt index 4358a1c0..dca07f65 100644 --- a/changelog.txt +++ b/changelog.txt @@ -54,6 +54,8 @@ Fixed in SVN: - Bugfix: (MBaker) Work item 17166 - Indexed colors do not refer to Excel's indexed colors? - Bugfix: (MBaker) Work item 17199 - Indexed colors should be consistent with Excel and start from 1 (current index starts at 0) - Bugfix: (MBaker) Work item 17262 - Named Range definition in .xls when sheet reeference is quote wrapped +- Bugfix: (MBaker) Work item 17403 - duplicateStyle() method doesn't duplicate conditional formats + Added an equivalent duplicateConditionalStyle() method for duplicating conditional styles - General: (MBaker) Work item 15405 - Two easy to fix Issues concerning PHPExcel_Token_Stack (l10n/UC) - General: (MBaker) Work item 15461 - Locale file paths not fit for windows - General: (MBaker) Work item 16643 - Add file directory as a cache option for cache_to_discISAM