No Description

NamedRange.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel
  23. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /**
  28. * PHPExcel_NamedRange
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_NamedRange
  35. {
  36. /**
  37. * Range name
  38. *
  39. * @var string
  40. */
  41. private $_name;
  42. /**
  43. * Worksheet on which the named range can be resolved
  44. *
  45. * @var PHPExcel_Worksheet
  46. */
  47. private $_worksheet;
  48. /**
  49. * Range of the referenced cells
  50. *
  51. * @var string
  52. */
  53. private $_range;
  54. /**
  55. * Is the named range local? (i.e. can only be used on $this->_worksheet)
  56. *
  57. * @var bool
  58. */
  59. private $_localOnly;
  60. /**
  61. * Scope
  62. *
  63. * @var PHPExcel_Worksheet
  64. */
  65. private $_scope;
  66. /**
  67. * Create a new NamedRange
  68. *
  69. * @param string $pName
  70. * @param PHPExcel_Worksheet $pWorksheet
  71. * @param string $pRange
  72. * @param bool $pLocalOnly
  73. * @param PHPExcel_Worksheet|null $pScope Scope. Only applies when $pLocalOnly = true. Null for global scope.
  74. * @throws PHPExcel_Exception
  75. */
  76. public function __construct($pName = null, PHPExcel_Worksheet $pWorksheet, $pRange = 'A1', $pLocalOnly = false, $pScope = null)
  77. {
  78. // Validate data
  79. if (($pName === NULL) || ($pWorksheet === NULL) || ($pRange === NULL)) {
  80. throw new PHPExcel_Exception('Parameters can not be null.');
  81. }
  82. // Set local members
  83. $this->_name = $pName;
  84. $this->_worksheet = $pWorksheet;
  85. $this->_range = $pRange;
  86. $this->_localOnly = $pLocalOnly;
  87. $this->_scope = ($pLocalOnly == true) ?
  88. (($pScope == null) ? $pWorksheet : $pScope) : null;
  89. }
  90. /**
  91. * Get name
  92. *
  93. * @return string
  94. */
  95. public function getName() {
  96. return $this->_name;
  97. }
  98. /**
  99. * Set name
  100. *
  101. * @param string $value
  102. * @return PHPExcel_NamedRange
  103. */
  104. public function setName($value = null) {
  105. if ($value !== NULL) {
  106. // Old title
  107. $oldTitle = $this->_name;
  108. // Re-attach
  109. if ($this->_worksheet !== NULL) {
  110. $this->_worksheet->getParent()->removeNamedRange($this->_name,$this->_worksheet);
  111. }
  112. $this->_name = $value;
  113. if ($this->_worksheet !== NULL) {
  114. $this->_worksheet->getParent()->addNamedRange($this);
  115. }
  116. // New title
  117. $newTitle = $this->_name;
  118. PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->_worksheet->getParent(), $oldTitle, $newTitle);
  119. }
  120. return $this;
  121. }
  122. /**
  123. * Get worksheet
  124. *
  125. * @return PHPExcel_Worksheet
  126. */
  127. public function getWorksheet() {
  128. return $this->_worksheet;
  129. }
  130. /**
  131. * Set worksheet
  132. *
  133. * @param PHPExcel_Worksheet $value
  134. * @return PHPExcel_NamedRange
  135. */
  136. public function setWorksheet(PHPExcel_Worksheet $value = null) {
  137. if ($value !== NULL) {
  138. $this->_worksheet = $value;
  139. }
  140. return $this;
  141. }
  142. /**
  143. * Get range
  144. *
  145. * @return string
  146. */
  147. public function getRange() {
  148. return $this->_range;
  149. }
  150. /**
  151. * Set range
  152. *
  153. * @param string $value
  154. * @return PHPExcel_NamedRange
  155. */
  156. public function setRange($value = null) {
  157. if ($value !== NULL) {
  158. $this->_range = $value;
  159. }
  160. return $this;
  161. }
  162. /**
  163. * Get localOnly
  164. *
  165. * @return bool
  166. */
  167. public function getLocalOnly() {
  168. return $this->_localOnly;
  169. }
  170. /**
  171. * Set localOnly
  172. *
  173. * @param bool $value
  174. * @return PHPExcel_NamedRange
  175. */
  176. public function setLocalOnly($value = false) {
  177. $this->_localOnly = $value;
  178. $this->_scope = $value ? $this->_worksheet : null;
  179. return $this;
  180. }
  181. /**
  182. * Get scope
  183. *
  184. * @return PHPExcel_Worksheet|null
  185. */
  186. public function getScope() {
  187. return $this->_scope;
  188. }
  189. /**
  190. * Set scope
  191. *
  192. * @param PHPExcel_Worksheet|null $value
  193. * @return PHPExcel_NamedRange
  194. */
  195. public function setScope(PHPExcel_Worksheet $value = null) {
  196. $this->_scope = $value;
  197. $this->_localOnly = ($value == null) ? false : true;
  198. return $this;
  199. }
  200. /**
  201. * Resolve a named range to a regular cell range
  202. *
  203. * @param string $pNamedRange Named range
  204. * @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope
  205. * @return PHPExcel_NamedRange
  206. */
  207. public static function resolveRange($pNamedRange = '', PHPExcel_Worksheet $pSheet) {
  208. return $pSheet->getParent()->getNamedRange($pNamedRange, $pSheet);
  209. }
  210. /**
  211. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  212. */
  213. public function __clone() {
  214. $vars = get_object_vars($this);
  215. foreach ($vars as $key => $value) {
  216. if (is_object($value)) {
  217. $this->$key = clone $value;
  218. } else {
  219. $this->$key = $value;
  220. }
  221. }
  222. }
  223. }