NamedRange.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_NamedRange
  8. {
  9. /**
  10. * Range name
  11. *
  12. * @var string
  13. */
  14. private $name;
  15. /**
  16. * Worksheet on which the named range can be resolved
  17. *
  18. * @var PHPExcel_Worksheet
  19. */
  20. private $worksheet;
  21. /**
  22. * Range of the referenced cells
  23. *
  24. * @var string
  25. */
  26. private $range;
  27. /**
  28. * Is the named range local? (i.e. can only be used on $this->worksheet)
  29. *
  30. * @var bool
  31. */
  32. private $localOnly;
  33. /**
  34. * Scope
  35. *
  36. * @var PHPExcel_Worksheet
  37. */
  38. private $scope;
  39. /**
  40. * Create a new NamedRange
  41. *
  42. * @param string $pName
  43. * @param PHPExcel_Worksheet $pWorksheet
  44. * @param string $pRange
  45. * @param bool $pLocalOnly
  46. * @param PHPExcel_Worksheet|null $pScope Scope. Only applies when $pLocalOnly = true. Null for global scope.
  47. * @throws PHPExcel_Exception
  48. */
  49. public function __construct($pName = null, PHPExcel_Worksheet $pWorksheet, $pRange = 'A1', $pLocalOnly = false, $pScope = null)
  50. {
  51. // Validate data
  52. if (($pName === null) || ($pWorksheet === null) || ($pRange === null)) {
  53. throw new PHPExcel_Exception('Parameters can not be null.');
  54. }
  55. // Set local members
  56. $this->name = $pName;
  57. $this->worksheet = $pWorksheet;
  58. $this->range = $pRange;
  59. $this->localOnly = $pLocalOnly;
  60. $this->scope = ($pLocalOnly == true) ? (($pScope == null) ? $pWorksheet : $pScope) : null;
  61. }
  62. /**
  63. * Get name
  64. *
  65. * @return string
  66. */
  67. public function getName()
  68. {
  69. return $this->name;
  70. }
  71. /**
  72. * Set name
  73. *
  74. * @param string $value
  75. * @return PHPExcel_NamedRange
  76. */
  77. public function setName($value = null)
  78. {
  79. if ($value !== null) {
  80. // Old title
  81. $oldTitle = $this->name;
  82. // Re-attach
  83. if ($this->worksheet !== null) {
  84. $this->worksheet->getParent()->removeNamedRange($this->name, $this->worksheet);
  85. }
  86. $this->name = $value;
  87. if ($this->worksheet !== null) {
  88. $this->worksheet->getParent()->addNamedRange($this);
  89. }
  90. // New title
  91. $newTitle = $this->name;
  92. PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->worksheet->getParent(), $oldTitle, $newTitle);
  93. }
  94. return $this;
  95. }
  96. /**
  97. * Get worksheet
  98. *
  99. * @return PHPExcel_Worksheet
  100. */
  101. public function getWorksheet()
  102. {
  103. return $this->worksheet;
  104. }
  105. /**
  106. * Set worksheet
  107. *
  108. * @param PHPExcel_Worksheet $value
  109. * @return PHPExcel_NamedRange
  110. */
  111. public function setWorksheet(PHPExcel_Worksheet $value = null)
  112. {
  113. if ($value !== null) {
  114. $this->worksheet = $value;
  115. }
  116. return $this;
  117. }
  118. /**
  119. * Get range
  120. *
  121. * @return string
  122. */
  123. public function getRange()
  124. {
  125. return $this->range;
  126. }
  127. /**
  128. * Set range
  129. *
  130. * @param string $value
  131. * @return PHPExcel_NamedRange
  132. */
  133. public function setRange($value = null)
  134. {
  135. if ($value !== null) {
  136. $this->range = $value;
  137. }
  138. return $this;
  139. }
  140. /**
  141. * Get localOnly
  142. *
  143. * @return bool
  144. */
  145. public function getLocalOnly()
  146. {
  147. return $this->localOnly;
  148. }
  149. /**
  150. * Set localOnly
  151. *
  152. * @param bool $value
  153. * @return PHPExcel_NamedRange
  154. */
  155. public function setLocalOnly($value = false)
  156. {
  157. $this->localOnly = $value;
  158. $this->scope = $value ? $this->worksheet : null;
  159. return $this;
  160. }
  161. /**
  162. * Get scope
  163. *
  164. * @return PHPExcel_Worksheet|null
  165. */
  166. public function getScope()
  167. {
  168. return $this->scope;
  169. }
  170. /**
  171. * Set scope
  172. *
  173. * @param PHPExcel_Worksheet|null $value
  174. * @return PHPExcel_NamedRange
  175. */
  176. public function setScope(PHPExcel_Worksheet $value = null)
  177. {
  178. $this->scope = $value;
  179. $this->localOnly = ($value == null) ? false : true;
  180. return $this;
  181. }
  182. /**
  183. * Resolve a named range to a regular cell range
  184. *
  185. * @param string $pNamedRange Named range
  186. * @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope
  187. * @return PHPExcel_NamedRange
  188. */
  189. public static function resolveRange($pNamedRange = '', PHPExcel_Worksheet $pSheet)
  190. {
  191. return $pSheet->getParent()->getNamedRange($pNamedRange, $pSheet);
  192. }
  193. /**
  194. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  195. */
  196. public function __clone()
  197. {
  198. $vars = get_object_vars($this);
  199. foreach ($vars as $key => $value) {
  200. if (is_object($value)) {
  201. $this->$key = clone $value;
  202. } else {
  203. $this->$key = $value;
  204. }
  205. }
  206. }
  207. }