NamedRange.Class.php 5.0 KB

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