NamedRange.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_NamedRange
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel
  32. * @copyright Copyright (c) 2006 - 2011 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. */
  75. public function __construct($pName = null, PHPExcel_Worksheet $pWorksheet, $pRange = 'A1', $pLocalOnly = false, $pScope = null)
  76. {
  77. // Validate data
  78. if (is_null($pName) || is_null($pWorksheet)|| is_null($pRange)) {
  79. throw new Exception('Parameters can not be null.');
  80. }
  81. // Set local members
  82. $this->_name = $pName;
  83. $this->_worksheet = $pWorksheet;
  84. $this->_range = $pRange;
  85. $this->_localOnly = $pLocalOnly;
  86. $this->_scope = ($pLocalOnly == true) ?
  87. (($pScope == null) ? $pWorksheet : $pScope) : null;
  88. }
  89. /**
  90. * Get name
  91. *
  92. * @return string
  93. */
  94. public function getName() {
  95. return $this->_name;
  96. }
  97. /**
  98. * Set name
  99. *
  100. * @param string $value
  101. * @return PHPExcel_NamedRange
  102. */
  103. public function setName($value = null) {
  104. if (!is_null($value)) {
  105. // Old title
  106. $oldTitle = $this->_name;
  107. // Re-attach
  108. if (!is_null($this->_worksheet)) {
  109. $this->_worksheet->getParent()->removeNamedRange($this->_name,$this->_worksheet);
  110. }
  111. $this->_name = $value;
  112. if (!is_null($this->_worksheet)) {
  113. $this->_worksheet->getParent()->addNamedRange($this);
  114. }
  115. // New title
  116. $newTitle = $this->_name;
  117. PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->_worksheet->getParent(), $oldTitle, $newTitle);
  118. }
  119. return $this;
  120. }
  121. /**
  122. * Get worksheet
  123. *
  124. * @return PHPExcel_Worksheet
  125. */
  126. public function getWorksheet() {
  127. return $this->_worksheet;
  128. }
  129. /**
  130. * Set worksheet
  131. *
  132. * @param PHPExcel_Worksheet $value
  133. * @return PHPExcel_NamedRange
  134. */
  135. public function setWorksheet(PHPExcel_Worksheet $value = null) {
  136. if (!is_null($value)) {
  137. $this->_worksheet = $value;
  138. }
  139. return $this;
  140. }
  141. /**
  142. * Get range
  143. *
  144. * @return string
  145. */
  146. public function getRange() {
  147. return $this->_range;
  148. }
  149. /**
  150. * Set range
  151. *
  152. * @param string $value
  153. * @return PHPExcel_NamedRange
  154. */
  155. public function setRange($value = null) {
  156. if (!is_null($value)) {
  157. $this->_range = $value;
  158. }
  159. return $this;
  160. }
  161. /**
  162. * Get localOnly
  163. *
  164. * @return bool
  165. */
  166. public function getLocalOnly() {
  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. $this->_localOnly = $value;
  177. $this->_scope = $value ? $this->_worksheet : null;
  178. return $this;
  179. }
  180. /**
  181. * Get scope
  182. *
  183. * @return PHPExcel_Worksheet|null
  184. */
  185. public function getScope() {
  186. return $this->_scope;
  187. }
  188. /**
  189. * Set scope
  190. *
  191. * @param PHPExcel_Worksheet|null $value
  192. * @return PHPExcel_NamedRange
  193. */
  194. public function setScope(PHPExcel_Worksheet $value = null) {
  195. $this->_scope = $value;
  196. $this->_localOnly = ($value == null) ? false : true;
  197. return $this;
  198. }
  199. /**
  200. * Resolve a named range to a regular cell range
  201. *
  202. * @param string $pNamedRange Named range
  203. * @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope
  204. * @return PHPExcel_NamedRange
  205. */
  206. public static function resolveRange($pNamedRange = '', PHPExcel_Worksheet $pSheet) {
  207. return $pSheet->getParent()->getNamedRange($pNamedRange, $pSheet);
  208. }
  209. /**
  210. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  211. */
  212. public function __clone() {
  213. $vars = get_object_vars($this);
  214. foreach ($vars as $key => $value) {
  215. if (is_object($value)) {
  216. $this->$key = clone $value;
  217. } else {
  218. $this->$key = $value;
  219. }
  220. }
  221. }
  222. }