NamedRange.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * PHPExcel_NamedRange
  4. *
  5. * Copyright (c) 2006 - 2015 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 - 2015 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. class PHPExcel_NamedRange
  28. {
  29. /**
  30. * Range name
  31. *
  32. * @var string
  33. */
  34. private $name;
  35. /**
  36. * Worksheet on which the named range can be resolved
  37. *
  38. * @var PHPExcel_Worksheet
  39. */
  40. private $worksheet;
  41. /**
  42. * Range of the referenced cells
  43. *
  44. * @var string
  45. */
  46. private $range;
  47. /**
  48. * Is the named range local? (i.e. can only be used on $this->worksheet)
  49. *
  50. * @var bool
  51. */
  52. private $localOnly;
  53. /**
  54. * Scope
  55. *
  56. * @var PHPExcel_Worksheet
  57. */
  58. private $scope;
  59. /**
  60. * Create a new NamedRange
  61. *
  62. * @param string $pName
  63. * @param PHPExcel_Worksheet $pWorksheet
  64. * @param string $pRange
  65. * @param bool $pLocalOnly
  66. * @param PHPExcel_Worksheet|null $pScope Scope. Only applies when $pLocalOnly = true. Null for global scope.
  67. * @throws PHPExcel_Exception
  68. */
  69. public function __construct($pName = null, PHPExcel_Worksheet $pWorksheet, $pRange = 'A1', $pLocalOnly = false, $pScope = null)
  70. {
  71. // Validate data
  72. if (($pName === null) || ($pWorksheet === null) || ($pRange === null)) {
  73. throw new PHPExcel_Exception('Parameters can not be null.');
  74. }
  75. // Set local members
  76. $this->name = $pName;
  77. $this->worksheet = $pWorksheet;
  78. $this->range = $pRange;
  79. $this->localOnly = $pLocalOnly;
  80. $this->scope = ($pLocalOnly == true) ? (($pScope == null) ? $pWorksheet : $pScope) : null;
  81. }
  82. /**
  83. * Get name
  84. *
  85. * @return string
  86. */
  87. public function getName()
  88. {
  89. return $this->name;
  90. }
  91. /**
  92. * Set name
  93. *
  94. * @param string $value
  95. * @return PHPExcel_NamedRange
  96. */
  97. public function setName($value = null)
  98. {
  99. if ($value !== null) {
  100. // Old title
  101. $oldTitle = $this->name;
  102. // Re-attach
  103. if ($this->worksheet !== null) {
  104. $this->worksheet->getParent()->removeNamedRange($this->name, $this->worksheet);
  105. }
  106. $this->name = $value;
  107. if ($this->worksheet !== null) {
  108. $this->worksheet->getParent()->addNamedRange($this);
  109. }
  110. // New title
  111. $newTitle = $this->name;
  112. PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->worksheet->getParent(), $oldTitle, $newTitle);
  113. }
  114. return $this;
  115. }
  116. /**
  117. * Get worksheet
  118. *
  119. * @return PHPExcel_Worksheet
  120. */
  121. public function getWorksheet()
  122. {
  123. return $this->worksheet;
  124. }
  125. /**
  126. * Set worksheet
  127. *
  128. * @param PHPExcel_Worksheet $value
  129. * @return PHPExcel_NamedRange
  130. */
  131. public function setWorksheet(PHPExcel_Worksheet $value = null)
  132. {
  133. if ($value !== null) {
  134. $this->worksheet = $value;
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Get range
  140. *
  141. * @return string
  142. */
  143. public function getRange()
  144. {
  145. return $this->range;
  146. }
  147. /**
  148. * Set range
  149. *
  150. * @param string $value
  151. * @return PHPExcel_NamedRange
  152. */
  153. public function setRange($value = null)
  154. {
  155. if ($value !== null) {
  156. $this->range = $value;
  157. }
  158. return $this;
  159. }
  160. /**
  161. * Get localOnly
  162. *
  163. * @return bool
  164. */
  165. public function getLocalOnly()
  166. {
  167. return $this->localOnly;
  168. }
  169. /**
  170. * Set localOnly
  171. *
  172. * @param bool $value
  173. * @return PHPExcel_NamedRange
  174. */
  175. public function setLocalOnly($value = false)
  176. {
  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. {
  188. return $this->scope;
  189. }
  190. /**
  191. * Set scope
  192. *
  193. * @param PHPExcel_Worksheet|null $value
  194. * @return PHPExcel_NamedRange
  195. */
  196. public function setScope(PHPExcel_Worksheet $value = null)
  197. {
  198. $this->scope = $value;
  199. $this->localOnly = ($value == null) ? false : true;
  200. return $this;
  201. }
  202. /**
  203. * Resolve a named range to a regular cell range
  204. *
  205. * @param string $pNamedRange Named range
  206. * @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope
  207. * @return PHPExcel_NamedRange
  208. */
  209. public static function resolveRange($pNamedRange = '', PHPExcel_Worksheet $pSheet)
  210. {
  211. return $pSheet->getParent()->getNamedRange($pNamedRange, $pSheet);
  212. }
  213. /**
  214. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  215. */
  216. public function __clone()
  217. {
  218. $vars = get_object_vars($this);
  219. foreach ($vars as $key => $value) {
  220. if (is_object($value)) {
  221. $this->$key = clone $value;
  222. } else {
  223. $this->$key = $value;
  224. }
  225. }
  226. }
  227. }