From b235c531a75270b3563ef8f49a09e3f01f5b250e Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Fri, 16 Dec 2011 16:01:21 +0000 Subject: [PATCH] Experimental - Added getHighestDataColumn(), getHighestDataRow(), getHighestRowAndColumn() and calculateWorksheetDataDimension() methods for the worksheet that return the highest row and column that have cell records git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@83932 2327b42d-5241-43d6-9e2a-de5ac946f064 --- .../CachedObjectStorage/CacheBase.php | 50 ++++++++++++++++ Classes/PHPExcel/Worksheet.php | 59 ++++++++++++++----- changelog.txt | 2 + 3 files changed, 96 insertions(+), 15 deletions(-) diff --git a/Classes/PHPExcel/CachedObjectStorage/CacheBase.php b/Classes/PHPExcel/CachedObjectStorage/CacheBase.php index 43412a3e..ccfc0c1d 100644 --- a/Classes/PHPExcel/CachedObjectStorage/CacheBase.php +++ b/Classes/PHPExcel/CachedObjectStorage/CacheBase.php @@ -155,6 +155,56 @@ class PHPExcel_CachedObjectStorage_CacheBase { } // function sortCellList() + + /** + * Get highest worksheet column and highest row that have cell records + * + * @return array Highest column name and highest row number + */ + public function getHighestRowAndColumn() + { + // Lookup highest column and highest row + $col = array('A' => '1A'); + $row = array(1); + foreach ($this->getCellList() as $coord) { + list($c,$r) = sscanf($coord,'%[A-Z]%d'); + $row[$r] = $r; + $col[$c] = strlen($c).$c; + } + if (count($row) > 0) { + // Determine highest column and row + $highestRow = max($row); + $highestColumn = substr(max($col),1); + } + return array( 'row' => $highestRow, + 'column' => $highestColumn + ); + } + + + /** + * Get highest worksheet column + * + * @return string Highest column name + */ + public function getHighestColumn() + { + $colRow = $this->getHighestRowAndColumn(); + return $colRow['column']; + } + + /** + * Get highest worksheet row + * + * @return int Highest row number + */ + public function getHighestRow() + { + $colRow = $this->getHighestRowAndColumn(); + return $colRow['row']; + } + + protected function _getUniqueID() { if (function_exists('posix_getpid')) { $baseUnique = posix_getpid(); diff --git a/Classes/PHPExcel/Worksheet.php b/Classes/PHPExcel/Worksheet.php index 2e0b5905..f91249b7 100644 --- a/Classes/PHPExcel/Worksheet.php +++ b/Classes/PHPExcel/Worksheet.php @@ -539,6 +539,17 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable return 'A1' . ':' . $this->getHighestColumn() . $this->getHighestRow(); } + /** + * Calculate worksheet data dimension + * + * @return string String containing the dimension of this worksheet that actually contain data + */ + public function calculateWorksheetDataDimension() + { + // Return + return 'A1' . ':' . $this->getHighestDataColumn() . $this->getHighestDataRow(); + } + /** * Calculate widths for auto-size columns * @@ -843,6 +854,16 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable return $this->_cachedHighestColumn; } + /** + * Get highest worksheet column that contains data + * + * @return string Highest column name that contains data + */ + public function getHighestDataColumn() + { + return $this->_cellCollection->getHighestColumn(); + } + /** * Get highest worksheet row * @@ -853,6 +874,26 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable return $this->_cachedHighestRow; } + /** + * Get highest worksheet row that contains data + * + * @return string Highest row number that contains data + */ + public function getHighestDataRow() + { + return $this->_cellCollection->getHighestRow(); + } + + /** + * Get highest worksheet column and highest row that have cell records + * + * @return array Highest column name and highest row number + */ + public function getHighestRowAndColumn() + { + return $this->_cellCollection->getHighestRowAndColumn(); + } + /** * Set a cell value * @@ -2297,21 +2338,9 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable // } // // Lookup highest column and highest row if cells are cleaned - $highestColumn = -1; - $highestRow = 1; - - // Find cells that can be cleaned - $col = $row = array(); - foreach ($this->_cellCollection->getCellList() as $coord) { - list($c,$r) = sscanf($coord,'%[A-Z]%d'); - $row[$r] = $r; - $col[$c] = strlen($c).$c; - } - if (count($row) > 0) { - // Determine highest column and row - $highestRow = max($row); - $highestColumn = PHPExcel_Cell::columnIndexFromString(substr(max($col),1)); - } + $colRow = $this->_cellCollection->getHighestRowAndColumn(); + $highestRow = $colRow['row']; + $highestColumn = PHPExcel_Cell::columnIndexFromString($colRow['column']); // Loop through column dimensions foreach ($this->_columnDimensions as $dimension) { diff --git a/changelog.txt b/changelog.txt index ba62c8ba..3928c420 100644 --- a/changelog.txt +++ b/changelog.txt @@ -29,6 +29,8 @@ Fixed in SVN: - General: (MBaker) Reduce cell caching overhead using dirty flag to ensure that cells are only rewritten to the cache if they have actually been changed - General: (MBaker) Improved memory usage in CSV Writer - General: (MBaker) Improved speed and memory usage in Excel5 Writer +- General: (MBaker) Experimental - + Added getHighestDataColumn(), getHighestDataRow(), getHighestRowAndColumn() and calculateWorksheetDataDimension() methods for the worksheet that return the highest row and column that have cell records - Bugfix: (MBaker) Work item 15459 - Invalid cell coordinate in Autofilter for Excel2007 Writer - Bugfix: (MBaker) Work item 15518 - PCLZip library issue - Bugfix: (MBaker) Work item 15537 - Excel2007 Reader canRead function bug