For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
6.0 KiB

  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. }