Alignment.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Style_Alignment extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  8. {
  9. /* Horizontal alignment styles */
  10. const HORIZONTAL_GENERAL = 'general';
  11. const HORIZONTAL_LEFT = 'left';
  12. const HORIZONTAL_RIGHT = 'right';
  13. const HORIZONTAL_CENTER = 'center';
  14. const HORIZONTAL_CENTER_CONTINUOUS = 'centerContinuous';
  15. const HORIZONTAL_JUSTIFY = 'justify';
  16. const HORIZONTAL_FILL = 'fill';
  17. const HORIZONTAL_DISTRIBUTED = 'distributed'; // Excel2007 only
  18. /* Vertical alignment styles */
  19. const VERTICAL_BOTTOM = 'bottom';
  20. const VERTICAL_TOP = 'top';
  21. const VERTICAL_CENTER = 'center';
  22. const VERTICAL_JUSTIFY = 'justify';
  23. const VERTICAL_DISTRIBUTED = 'distributed'; // Excel2007 only
  24. /* Read order */
  25. const READORDER_CONTEXT = 0;
  26. const READORDER_LTR = 1;
  27. const READORDER_RTL = 2;
  28. /**
  29. * Horizontal alignment
  30. *
  31. * @var string
  32. */
  33. protected $horizontal = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  34. /**
  35. * Vertical alignment
  36. *
  37. * @var string
  38. */
  39. protected $vertical = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  40. /**
  41. * Text rotation
  42. *
  43. * @var integer
  44. */
  45. protected $textRotation = 0;
  46. /**
  47. * Wrap text
  48. *
  49. * @var boolean
  50. */
  51. protected $wrapText = false;
  52. /**
  53. * Shrink to fit
  54. *
  55. * @var boolean
  56. */
  57. protected $shrinkToFit = false;
  58. /**
  59. * Indent - only possible with horizontal alignment left and right
  60. *
  61. * @var integer
  62. */
  63. protected $indent = 0;
  64. /**
  65. * Read order
  66. *
  67. * @var integer
  68. */
  69. protected $readorder = 0;
  70. /**
  71. * Create a new PHPExcel_Style_Alignment
  72. *
  73. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  74. * Leave this value at default unless you understand exactly what
  75. * its ramifications are
  76. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  77. * Leave this value at default unless you understand exactly what
  78. * its ramifications are
  79. */
  80. public function __construct($isSupervisor = false, $isConditional = false)
  81. {
  82. // Supervisor?
  83. parent::__construct($isSupervisor);
  84. if ($isConditional) {
  85. $this->horizontal = null;
  86. $this->vertical = null;
  87. $this->textRotation = null;
  88. }
  89. }
  90. /**
  91. * Get the shared style component for the currently active cell in currently active sheet.
  92. * Only used for style supervisor
  93. *
  94. * @return PHPExcel_Style_Alignment
  95. */
  96. public function getSharedComponent()
  97. {
  98. return $this->parent->getSharedComponent()->getAlignment();
  99. }
  100. /**
  101. * Build style array from subcomponents
  102. *
  103. * @param array $array
  104. * @return array
  105. */
  106. public function getStyleArray($array)
  107. {
  108. return array('alignment' => $array);
  109. }
  110. /**
  111. * Apply styles from array
  112. *
  113. * <code>
  114. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getAlignment()->applyFromArray(
  115. * array(
  116. * 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  117. * 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  118. * 'rotation' => 0,
  119. * 'wrap' => TRUE
  120. * )
  121. * );
  122. * </code>
  123. *
  124. * @param array $pStyles Array containing style information
  125. * @throws PHPExcel_Exception
  126. * @return PHPExcel_Style_Alignment
  127. */
  128. public function applyFromArray($pStyles = null)
  129. {
  130. if (is_array($pStyles)) {
  131. if ($this->isSupervisor) {
  132. $this->getActiveSheet()->getStyle($this->getSelectedCells())
  133. ->applyFromArray($this->getStyleArray($pStyles));
  134. } else {
  135. if (isset($pStyles['horizontal'])) {
  136. $this->setHorizontal($pStyles['horizontal']);
  137. }
  138. if (isset($pStyles['vertical'])) {
  139. $this->setVertical($pStyles['vertical']);
  140. }
  141. if (isset($pStyles['rotation'])) {
  142. $this->setTextRotation($pStyles['rotation']);
  143. }
  144. if (isset($pStyles['wrap'])) {
  145. $this->setWrapText($pStyles['wrap']);
  146. }
  147. if (isset($pStyles['shrinkToFit'])) {
  148. $this->setShrinkToFit($pStyles['shrinkToFit']);
  149. }
  150. if (isset($pStyles['indent'])) {
  151. $this->setIndent($pStyles['indent']);
  152. }
  153. if (isset($pStyles['readorder'])) {
  154. $this->setReadorder($pStyles['readorder']);
  155. }
  156. }
  157. } else {
  158. throw new PHPExcel_Exception("Invalid style array passed.");
  159. }
  160. return $this;
  161. }
  162. /**
  163. * Get Horizontal
  164. *
  165. * @return string
  166. */
  167. public function getHorizontal()
  168. {
  169. if ($this->isSupervisor) {
  170. return $this->getSharedComponent()->getHorizontal();
  171. }
  172. return $this->horizontal;
  173. }
  174. /**
  175. * Set Horizontal
  176. *
  177. * @param string $pValue
  178. * @return PHPExcel_Style_Alignment
  179. */
  180. public function setHorizontal($pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL)
  181. {
  182. if ($pValue == '') {
  183. $pValue = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  184. }
  185. if ($this->isSupervisor) {
  186. $styleArray = $this->getStyleArray(array('horizontal' => $pValue));
  187. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  188. } else {
  189. $this->horizontal = $pValue;
  190. }
  191. return $this;
  192. }
  193. /**
  194. * Get Vertical
  195. *
  196. * @return string
  197. */
  198. public function getVertical()
  199. {
  200. if ($this->isSupervisor) {
  201. return $this->getSharedComponent()->getVertical();
  202. }
  203. return $this->vertical;
  204. }
  205. /**
  206. * Set Vertical
  207. *
  208. * @param string $pValue
  209. * @return PHPExcel_Style_Alignment
  210. */
  211. public function setVertical($pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM)
  212. {
  213. if ($pValue == '') {
  214. $pValue = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  215. }
  216. if ($this->isSupervisor) {
  217. $styleArray = $this->getStyleArray(array('vertical' => $pValue));
  218. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  219. } else {
  220. $this->vertical = $pValue;
  221. }
  222. return $this;
  223. }
  224. /**
  225. * Get TextRotation
  226. *
  227. * @return int
  228. */
  229. public function getTextRotation()
  230. {
  231. if ($this->isSupervisor) {
  232. return $this->getSharedComponent()->getTextRotation();
  233. }
  234. return $this->textRotation;
  235. }
  236. /**
  237. * Set TextRotation
  238. *
  239. * @param int $pValue
  240. * @throws PHPExcel_Exception
  241. * @return PHPExcel_Style_Alignment
  242. */
  243. public function setTextRotation($pValue = 0)
  244. {
  245. // Excel2007 value 255 => PHPExcel value -165
  246. if ($pValue == 255) {
  247. $pValue = -165;
  248. }
  249. // Set rotation
  250. if (($pValue >= -90 && $pValue <= 90) || $pValue == -165) {
  251. if ($this->isSupervisor) {
  252. $styleArray = $this->getStyleArray(array('rotation' => $pValue));
  253. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  254. } else {
  255. $this->textRotation = $pValue;
  256. }
  257. } else {
  258. throw new PHPExcel_Exception("Text rotation should be a value between -90 and 90.");
  259. }
  260. return $this;
  261. }
  262. /**
  263. * Get Wrap Text
  264. *
  265. * @return boolean
  266. */
  267. public function getWrapText()
  268. {
  269. if ($this->isSupervisor) {
  270. return $this->getSharedComponent()->getWrapText();
  271. }
  272. return $this->wrapText;
  273. }
  274. /**
  275. * Set Wrap Text
  276. *
  277. * @param boolean $pValue
  278. * @return PHPExcel_Style_Alignment
  279. */
  280. public function setWrapText($pValue = false)
  281. {
  282. if ($pValue == '') {
  283. $pValue = false;
  284. }
  285. if ($this->isSupervisor) {
  286. $styleArray = $this->getStyleArray(array('wrap' => $pValue));
  287. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  288. } else {
  289. $this->wrapText = $pValue;
  290. }
  291. return $this;
  292. }
  293. /**
  294. * Get Shrink to fit
  295. *
  296. * @return boolean
  297. */
  298. public function getShrinkToFit()
  299. {
  300. if ($this->isSupervisor) {
  301. return $this->getSharedComponent()->getShrinkToFit();
  302. }
  303. return $this->shrinkToFit;
  304. }
  305. /**
  306. * Set Shrink to fit
  307. *
  308. * @param boolean $pValue
  309. * @return PHPExcel_Style_Alignment
  310. */
  311. public function setShrinkToFit($pValue = false)
  312. {
  313. if ($pValue == '') {
  314. $pValue = false;
  315. }
  316. if ($this->isSupervisor) {
  317. $styleArray = $this->getStyleArray(array('shrinkToFit' => $pValue));
  318. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  319. } else {
  320. $this->shrinkToFit = $pValue;
  321. }
  322. return $this;
  323. }
  324. /**
  325. * Get indent
  326. *
  327. * @return int
  328. */
  329. public function getIndent()
  330. {
  331. if ($this->isSupervisor) {
  332. return $this->getSharedComponent()->getIndent();
  333. }
  334. return $this->indent;
  335. }
  336. /**
  337. * Set indent
  338. *
  339. * @param int $pValue
  340. * @return PHPExcel_Style_Alignment
  341. */
  342. public function setIndent($pValue = 0)
  343. {
  344. if ($pValue > 0) {
  345. if ($this->getHorizontal() != self::HORIZONTAL_GENERAL &&
  346. $this->getHorizontal() != self::HORIZONTAL_LEFT &&
  347. $this->getHorizontal() != self::HORIZONTAL_RIGHT) {
  348. $pValue = 0; // indent not supported
  349. }
  350. }
  351. if ($this->isSupervisor) {
  352. $styleArray = $this->getStyleArray(array('indent' => $pValue));
  353. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  354. } else {
  355. $this->indent = $pValue;
  356. }
  357. return $this;
  358. }
  359. /**
  360. * Get read order
  361. *
  362. * @return integer
  363. */
  364. public function getReadorder()
  365. {
  366. if ($this->isSupervisor) {
  367. return $this->getSharedComponent()->getReadorder();
  368. }
  369. return $this->readorder;
  370. }
  371. /**
  372. * Set read order
  373. *
  374. * @param int $pValue
  375. * @return PHPExcel_Style_Alignment
  376. */
  377. public function setReadorder($pValue = 0)
  378. {
  379. if ($pValue < 0 || $pValue > 2) {
  380. $pValue = 0;
  381. }
  382. if ($this->isSupervisor) {
  383. $styleArray = $this->getStyleArray(array('readorder' => $pValue));
  384. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  385. } else {
  386. $this->readorder = $pValue;
  387. }
  388. return $this;
  389. }
  390. /**
  391. * Get hash code
  392. *
  393. * @return string Hash code
  394. */
  395. public function getHashCode()
  396. {
  397. if ($this->isSupervisor) {
  398. return $this->getSharedComponent()->getHashCode();
  399. }
  400. return md5(
  401. $this->horizontal .
  402. $this->vertical .
  403. $this->textRotation .
  404. ($this->wrapText ? 't' : 'f') .
  405. ($this->shrinkToFit ? 't' : 'f') .
  406. $this->indent .
  407. $this->readorder .
  408. __CLASS__
  409. );
  410. }
  411. }