2010-08-26 19:14:53 +00:00
|
|
|
<?php
|
2015-05-11 21:38:52 +00:00
|
|
|
|
2010-08-26 19:14:53 +00:00
|
|
|
/**
|
2015-05-11 21:38:52 +00:00
|
|
|
* PHPExcel_NamedRange
|
2010-08-26 19:14:53 +00:00
|
|
|
*
|
2015-05-02 22:50:37 +00:00
|
|
|
* Copyright (c) 2006 - 2015 PHPExcel
|
2010-08-26 19:14:53 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* @category PHPExcel
|
|
|
|
* @package PHPExcel
|
2015-05-02 22:50:37 +00:00
|
|
|
* @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
|
2015-05-11 21:38:52 +00:00
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
2010-08-26 19:14:53 +00:00
|
|
|
* @version ##VERSION##, ##DATE##
|
|
|
|
*/
|
|
|
|
class PHPExcel_NamedRange
|
|
|
|
{
|
2015-05-11 21:38:52 +00:00
|
|
|
/**
|
|
|
|
* Range name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-05-18 15:39:04 +00:00
|
|
|
private $name;
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Worksheet on which the named range can be resolved
|
|
|
|
*
|
|
|
|
* @var PHPExcel_Worksheet
|
|
|
|
*/
|
2015-05-18 15:39:04 +00:00
|
|
|
private $worksheet;
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Range of the referenced cells
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-05-18 15:39:04 +00:00
|
|
|
private $range;
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
/**
|
2015-05-18 15:39:04 +00:00
|
|
|
* Is the named range local? (i.e. can only be used on $this->worksheet)
|
2015-05-11 21:38:52 +00:00
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2015-05-18 15:39:04 +00:00
|
|
|
private $localOnly;
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope
|
|
|
|
*
|
|
|
|
* @var PHPExcel_Worksheet
|
|
|
|
*/
|
2015-05-18 15:39:04 +00:00
|
|
|
private $scope;
|
2010-08-26 19:14:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new NamedRange
|
|
|
|
*
|
|
|
|
* @param string $pName
|
|
|
|
* @param PHPExcel_Worksheet $pWorksheet
|
|
|
|
* @param string $pRange
|
|
|
|
* @param bool $pLocalOnly
|
2015-05-11 21:38:52 +00:00
|
|
|
* @param PHPExcel_Worksheet|null $pScope Scope. Only applies when $pLocalOnly = true. Null for global scope.
|
2012-11-26 18:01:15 +00:00
|
|
|
* @throws PHPExcel_Exception
|
2010-08-26 19:14:53 +00:00
|
|
|
*/
|
|
|
|
public function __construct($pName = null, PHPExcel_Worksheet $pWorksheet, $pRange = 'A1', $pLocalOnly = false, $pScope = null)
|
|
|
|
{
|
2015-05-11 21:38:52 +00:00
|
|
|
// Validate data
|
2015-05-15 10:55:10 +00:00
|
|
|
if (($pName === null) || ($pWorksheet === null) || ($pRange === null)) {
|
2015-05-11 21:38:52 +00:00
|
|
|
throw new PHPExcel_Exception('Parameters can not be null.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set local members
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->name = $pName;
|
|
|
|
$this->worksheet = $pWorksheet;
|
|
|
|
$this->range = $pRange;
|
|
|
|
$this->localOnly = $pLocalOnly;
|
|
|
|
$this->scope = ($pLocalOnly == true) ? (($pScope == null) ? $pWorksheet : $pScope) : null;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public function getName()
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
return $this->name;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set name
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return PHPExcel_NamedRange
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public function setName($value = null)
|
|
|
|
{
|
|
|
|
if ($value !== null) {
|
2015-05-11 21:38:52 +00:00
|
|
|
// Old title
|
2015-05-18 15:39:04 +00:00
|
|
|
$oldTitle = $this->name;
|
2015-05-11 21:38:52 +00:00
|
|
|
|
|
|
|
// Re-attach
|
2015-05-18 15:39:04 +00:00
|
|
|
if ($this->worksheet !== null) {
|
|
|
|
$this->worksheet->getParent()->removeNamedRange($this->name, $this->worksheet);
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->name = $value;
|
2015-05-11 21:38:52 +00:00
|
|
|
|
2015-05-18 15:39:04 +00:00
|
|
|
if ($this->worksheet !== null) {
|
|
|
|
$this->worksheet->getParent()->addNamedRange($this);
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New title
|
2015-05-18 15:39:04 +00:00
|
|
|
$newTitle = $this->name;
|
|
|
|
PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->worksheet->getParent(), $oldTitle, $newTitle);
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
return $this;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get worksheet
|
|
|
|
*
|
|
|
|
* @return PHPExcel_Worksheet
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public function getWorksheet()
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
return $this->worksheet;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set worksheet
|
|
|
|
*
|
|
|
|
* @param PHPExcel_Worksheet $value
|
|
|
|
* @return PHPExcel_NamedRange
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public function setWorksheet(PHPExcel_Worksheet $value = null)
|
|
|
|
{
|
|
|
|
if ($value !== null) {
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->worksheet = $value;
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
return $this;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get range
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public function getRange()
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
return $this->range;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set range
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return PHPExcel_NamedRange
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public function setRange($value = null)
|
|
|
|
{
|
|
|
|
if ($value !== null) {
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->range = $value;
|
2015-05-11 21:38:52 +00:00
|
|
|
}
|
|
|
|
return $this;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get localOnly
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public function getLocalOnly()
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
return $this->localOnly;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set localOnly
|
|
|
|
*
|
|
|
|
* @param bool $value
|
|
|
|
* @return PHPExcel_NamedRange
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public function setLocalOnly($value = false)
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->localOnly = $value;
|
|
|
|
$this->scope = $value ? $this->worksheet : null;
|
2015-05-11 21:38:52 +00:00
|
|
|
return $this;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get scope
|
|
|
|
*
|
|
|
|
* @return PHPExcel_Worksheet|null
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public function getScope()
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
return $this->scope;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set scope
|
|
|
|
*
|
|
|
|
* @param PHPExcel_Worksheet|null $value
|
|
|
|
* @return PHPExcel_NamedRange
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public function setScope(PHPExcel_Worksheet $value = null)
|
|
|
|
{
|
2015-05-18 15:39:04 +00:00
|
|
|
$this->scope = $value;
|
|
|
|
$this->localOnly = ($value == null) ? false : true;
|
2015-05-11 21:38:52 +00:00
|
|
|
return $this;
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a named range to a regular cell range
|
|
|
|
*
|
|
|
|
* @param string $pNamedRange Named range
|
|
|
|
* @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope
|
|
|
|
* @return PHPExcel_NamedRange
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public static function resolveRange($pNamedRange = '', PHPExcel_Worksheet $pSheet)
|
|
|
|
{
|
2015-05-11 21:38:52 +00:00
|
|
|
return $pSheet->getParent()->getNamedRange($pNamedRange, $pSheet);
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 21:38:52 +00:00
|
|
|
/**
|
|
|
|
* Implement PHP __clone to create a deep clone, not just a shallow copy.
|
|
|
|
*/
|
2015-05-15 10:55:10 +00:00
|
|
|
public function __clone()
|
|
|
|
{
|
2015-05-11 21:38:52 +00:00
|
|
|
$vars = get_object_vars($this);
|
|
|
|
foreach ($vars as $key => $value) {
|
|
|
|
if (is_object($value)) {
|
|
|
|
$this->$key = clone $value;
|
|
|
|
} else {
|
|
|
|
$this->$key = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-26 19:14:53 +00:00
|
|
|
}
|