PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/add-ons/PHPExcel/PHPExcel/Worksheet.php

https://github.com/jcplat/console-seolan
PHP | 2518 lines | 1056 code | 315 blank | 1147 comment | 152 complexity | 7fbe5a7a5429a02369a89ce315e15d81 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2009 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_Worksheet
  23. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.1, 2009-11-02
  26. */
  27. /** PHPExcel root */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
  33. }
  34. /** PHPExcel */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel.php';
  36. /** PHPExcel_Cell */
  37. require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
  38. /** PHPExcel_Cell_DataType */
  39. require_once PHPEXCEL_ROOT . 'PHPExcel/Cell/DataType.php';
  40. /** PHPExcel_Worksheet_RowDimension */
  41. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/RowDimension.php';
  42. /** PHPExcel_Worksheet_ColumnDimension */
  43. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/ColumnDimension.php';
  44. /** PHPExcel_Worksheet_PageSetup */
  45. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/PageSetup.php';
  46. /** PHPExcel_Worksheet_PageMargins */
  47. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/PageMargins.php';
  48. /** PHPExcel_Worksheet_HeaderFooter */
  49. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/HeaderFooter.php';
  50. /** PHPExcel_Worksheet_BaseDrawing */
  51. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/BaseDrawing.php';
  52. /** PHPExcel_Worksheet_Drawing */
  53. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/Drawing.php';
  54. /** PHPExcel_Worksheet_MemoryDrawing */
  55. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/MemoryDrawing.php';
  56. /** PHPExcel_Worksheet_HeaderFooterDrawing */
  57. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/HeaderFooterDrawing.php';
  58. /** PHPExcel_Worksheet_SheetView */
  59. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/SheetView.php';
  60. /** PHPExcel_Worksheet_Protection */
  61. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/Protection.php';
  62. /** PHPExcel_Worksheet_RowIterator */
  63. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet/RowIterator.php';
  64. /** PHPExcel_Comment */
  65. require_once PHPEXCEL_ROOT . 'PHPExcel/Comment.php';
  66. /** PHPExcel_Style */
  67. require_once PHPEXCEL_ROOT . 'PHPExcel/Style.php';
  68. /** PHPExcel_Style_Fill */
  69. require_once PHPEXCEL_ROOT . 'PHPExcel/Style/Fill.php';
  70. /** PHPExcel_Style_Color */
  71. require_once PHPEXCEL_ROOT . 'PHPExcel/Style/Color.php';
  72. /** PHPExcel_Style_NumberFormat */
  73. require_once PHPEXCEL_ROOT . 'PHPExcel/Style/NumberFormat.php';
  74. /** PHPExcel_IComparable */
  75. require_once PHPEXCEL_ROOT . 'PHPExcel/IComparable.php';
  76. /** PHPExcel_Shared_Font */
  77. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/Font.php';
  78. /** PHPExcel_Shared_String */
  79. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/String.php';
  80. /** PHPExcel_Shared_PasswordHasher */
  81. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/PasswordHasher.php';
  82. /** PHPExcel_ReferenceHelper */
  83. require_once PHPEXCEL_ROOT . 'PHPExcel/ReferenceHelper.php';
  84. /**
  85. * PHPExcel_Worksheet
  86. *
  87. * @category PHPExcel
  88. * @package PHPExcel_Worksheet
  89. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  90. */
  91. class PHPExcel_Worksheet implements PHPExcel_IComparable
  92. {
  93. /* Break types */
  94. const BREAK_NONE = 0;
  95. const BREAK_ROW = 1;
  96. const BREAK_COLUMN = 2;
  97. /* Sheet state */
  98. const SHEETSTATE_VISIBLE = 'visible';
  99. const SHEETSTATE_HIDDEN = 'hidden';
  100. const SHEETSTATE_VERYHIDDEN = 'veryHidden';
  101. /**
  102. * Parent spreadsheet
  103. *
  104. * @var PHPExcel
  105. */
  106. private $_parent;
  107. /**
  108. * Collection of cells
  109. *
  110. * @var PHPExcel_Cell[]
  111. */
  112. private $_cellCollection = array();
  113. /**
  114. * Collection of row dimensions
  115. *
  116. * @var PHPExcel_Worksheet_RowDimension[]
  117. */
  118. private $_rowDimensions = array();
  119. /**
  120. * Default row dimension
  121. *
  122. * @var PHPExcel_Worksheet_RowDimension
  123. */
  124. private $_defaultRowDimension = null;
  125. /**
  126. * Collection of column dimensions
  127. *
  128. * @var PHPExcel_Worksheet_ColumnDimension[]
  129. */
  130. private $_columnDimensions = array();
  131. /**
  132. * Default column dimension
  133. *
  134. * @var PHPExcel_Worksheet_ColumnDimension
  135. */
  136. private $_defaultColumnDimension = null;
  137. /**
  138. * Collection of drawings
  139. *
  140. * @var PHPExcel_Worksheet_BaseDrawing[]
  141. */
  142. private $_drawingCollection = null;
  143. /**
  144. * Worksheet title
  145. *
  146. * @var string
  147. */
  148. private $_title;
  149. /**
  150. * Sheet state
  151. *
  152. * @var string
  153. */
  154. private $_sheetState;
  155. /**
  156. * Page setup
  157. *
  158. * @var PHPExcel_Worksheet_PageSetup
  159. */
  160. private $_pageSetup;
  161. /**
  162. * Page margins
  163. *
  164. * @var PHPExcel_Worksheet_PageMargins
  165. */
  166. private $_pageMargins;
  167. /**
  168. * Page header/footer
  169. *
  170. * @var PHPExcel_Worksheet_HeaderFooter
  171. */
  172. private $_headerFooter;
  173. /**
  174. * Sheet view
  175. *
  176. * @var PHPExcel_Worksheet_SheetView
  177. */
  178. private $_sheetView;
  179. /**
  180. * Protection
  181. *
  182. * @var PHPExcel_Worksheet_Protection
  183. */
  184. private $_protection;
  185. /**
  186. * Collection of styles
  187. *
  188. * @var PHPExcel_Style[]
  189. */
  190. private $_styles = array();
  191. /**
  192. * Conditional styles. Indexed by cell coordinate, e.g. 'A1'
  193. *
  194. * @var array
  195. */
  196. private $_conditionalStylesCollection = array();
  197. /**
  198. * Is the current cell collection sorted already?
  199. *
  200. * @var boolean
  201. */
  202. private $_cellCollectionIsSorted = false;
  203. /**
  204. * Collection of breaks
  205. *
  206. * @var array
  207. */
  208. private $_breaks = array();
  209. /**
  210. * Collection of merged cell ranges
  211. *
  212. * @var array
  213. */
  214. private $_mergeCells = array();
  215. /**
  216. * Collection of protected cell ranges
  217. *
  218. * @var array
  219. */
  220. private $_protectedCells = array();
  221. /**
  222. * Autofilter Range
  223. *
  224. * @var string
  225. */
  226. private $_autoFilter = '';
  227. /**
  228. * Freeze pane
  229. *
  230. * @var string
  231. */
  232. private $_freezePane = '';
  233. /**
  234. * Show gridlines?
  235. *
  236. * @var boolean
  237. */
  238. private $_showGridlines = true;
  239. /**
  240. * Print gridlines?
  241. *
  242. * @var boolean
  243. */
  244. private $_printGridlines = false;
  245. /**
  246. * Show summary below? (Row/Column outline)
  247. *
  248. * @var boolean
  249. */
  250. private $_showSummaryBelow = true;
  251. /**
  252. * Show summary right? (Row/Column outline)
  253. *
  254. * @var boolean
  255. */
  256. private $_showSummaryRight = true;
  257. /**
  258. * Collection of comments
  259. *
  260. * @var PHPExcel_Comment[]
  261. */
  262. private $_comments = array();
  263. /**
  264. * Selected cell
  265. *
  266. * @var string
  267. */
  268. private $_selectedCell = 'A1';
  269. /**
  270. * Temporary property used by style supervisor. Will be removed
  271. *
  272. * @var string
  273. */
  274. private $_xActiveCell = 'A1';
  275. /**
  276. * Temporary property used by style supervisor. Will be removed
  277. *
  278. * @var string
  279. */
  280. private $_xSelectedCells = 'A1:A1';
  281. /**
  282. * Cached highest column
  283. *
  284. * @var string
  285. */
  286. private $_cachedHighestColumn = 'A';
  287. /**
  288. * Cached highest row
  289. *
  290. * @var int
  291. */
  292. private $_cachedHighestRow = 1;
  293. /**
  294. * Right-to-left?
  295. *
  296. * @var boolean
  297. */
  298. private $_rightToLeft = false;
  299. /**
  300. * Hyperlinks. Indexed by cell coordinate, e.g. 'A1'
  301. *
  302. * @var array
  303. */
  304. private $_hyperlinkCollection = array();
  305. /**
  306. * Data validation objects. Indexed by cell coordinate, e.g. 'A1'
  307. *
  308. * @var array
  309. */
  310. private $_dataValidationCollection = array();
  311. /**
  312. * Tab color
  313. *
  314. * @var PHPExcel_Style_Color
  315. */
  316. private $_tabColor;
  317. /**
  318. * Create a new worksheet
  319. *
  320. * @param PHPExcel $pParent
  321. * @param string $pTitle
  322. */
  323. public function __construct(PHPExcel $pParent = null, $pTitle = 'Worksheet')
  324. {
  325. // Set parent and title
  326. $this->_parent = $pParent;
  327. $this->setTitle($pTitle);
  328. $this->setSheetState(PHPExcel_Worksheet::SHEETSTATE_VISIBLE);
  329. // Set page setup
  330. $this->_pageSetup = new PHPExcel_Worksheet_PageSetup();
  331. // Set page margins
  332. $this->_pageMargins = new PHPExcel_Worksheet_PageMargins();
  333. // Set page header/footer
  334. $this->_headerFooter = new PHPExcel_Worksheet_HeaderFooter();
  335. // Set sheet view
  336. $this->_sheetView = new PHPExcel_Worksheet_SheetView();
  337. // Drawing collection
  338. $this->_drawingCollection = new ArrayObject();
  339. // Protection
  340. $this->_protection = new PHPExcel_Worksheet_Protection();
  341. // Gridlines
  342. $this->_showGridlines = true;
  343. $this->_printGridlines = false;
  344. // Outline summary
  345. $this->_showSummaryBelow = true;
  346. $this->_showSummaryRight = true;
  347. // Default row dimension
  348. $this->_defaultRowDimension = new PHPExcel_Worksheet_RowDimension(null);
  349. // Default column dimension
  350. $this->_defaultColumnDimension = new PHPExcel_Worksheet_ColumnDimension(null);
  351. }
  352. /**
  353. * Check sheet title for valid Excel syntax
  354. *
  355. * @param string $pValue The string to check
  356. * @return string The valid string
  357. * @throws Exception
  358. */
  359. private static function _checkSheetTitle($pValue)
  360. {
  361. // Some of the printable ASCII characters are invalid: * : / \ ? [ ]
  362. if (preg_match('/(\\*|\\:|\\/|\\\\|\\?|\\[|\\])/', $pValue)) {
  363. throw new Exception('Invalid character found in sheet title');
  364. }
  365. // Maximum 31 characters allowed for sheet title
  366. if (PHPExcel_Shared_String::CountCharacters($pValue) > 31) {
  367. throw new Exception('Maximum 31 characters allowed in sheet title.');
  368. }
  369. return $pValue;
  370. }
  371. /**
  372. * Get collection of cells
  373. *
  374. * @param boolean $pSorted Also sort the cell collection?
  375. * @return PHPExcel_Cell[]
  376. */
  377. public function getCellCollection($pSorted = true)
  378. {
  379. if ($pSorted) {
  380. // Re-order cell collection
  381. $this->sortCellCollection();
  382. }
  383. return $this->_cellCollection;
  384. }
  385. /**
  386. * Sort collection of cells
  387. *
  388. * @return PHPExcel_Worksheet
  389. */
  390. public function sortCellCollection()
  391. {
  392. if (!$this->_cellCollectionIsSorted) {
  393. // Re-order cell collection
  394. // uasort($this->_cellCollection, array('PHPExcel_Cell', 'compareCells')); <-- slow
  395. $indexed = array();
  396. foreach (array_keys($this->_cellCollection) as $index) {
  397. $rowNum = $this->_cellCollection[$index]->getRow();
  398. $colNum = PHPExcel_Cell::columnIndexFromString($this->_cellCollection[$index]->getColumn());
  399. // Columns are limited to ZZZ (18278), so 20000 is plenty to assure no conflicts
  400. $key = $rowNum * 20000 + $colNum;
  401. $indexed[$key] = $index; // &$this->_cellCollection[$index];
  402. }
  403. ksort($indexed);
  404. // Rebuild cellCollection from the sorted index
  405. $newCellCollection = array();
  406. foreach ($indexed as $index) {
  407. $newCellCollection[$index] = $this->_cellCollection[$index];
  408. }
  409. $this->_cellCollection = $newCellCollection;
  410. $this->_cellCollectionIsSorted = true;
  411. }
  412. return $this;
  413. }
  414. /**
  415. * Get collection of row dimensions
  416. *
  417. * @return PHPExcel_Worksheet_RowDimension[]
  418. */
  419. public function getRowDimensions()
  420. {
  421. return $this->_rowDimensions;
  422. }
  423. /**
  424. * Get default row dimension
  425. *
  426. * @return PHPExcel_Worksheet_RowDimension
  427. */
  428. public function getDefaultRowDimension()
  429. {
  430. return $this->_defaultRowDimension;
  431. }
  432. /**
  433. * Get collection of column dimensions
  434. *
  435. * @return PHPExcel_Worksheet_ColumnDimension[]
  436. */
  437. public function getColumnDimensions()
  438. {
  439. return $this->_columnDimensions;
  440. }
  441. /**
  442. * Get default column dimension
  443. *
  444. * @return PHPExcel_Worksheet_ColumnDimension
  445. */
  446. public function getDefaultColumnDimension()
  447. {
  448. return $this->_defaultColumnDimension;
  449. }
  450. /**
  451. * Get collection of drawings
  452. *
  453. * @return PHPExcel_Worksheet_BaseDrawing[]
  454. */
  455. public function getDrawingCollection()
  456. {
  457. return $this->_drawingCollection;
  458. }
  459. /**
  460. * Refresh column dimensions
  461. *
  462. * @return PHPExcel_Worksheet
  463. */
  464. public function refreshColumnDimensions()
  465. {
  466. $currentColumnDimensions = $this->getColumnDimensions();
  467. $newColumnDimensions = array();
  468. foreach ($currentColumnDimensions as $objColumnDimension) {
  469. $newColumnDimensions[$objColumnDimension->getColumnIndex()] = $objColumnDimension;
  470. }
  471. $this->_columnDimensions = $newColumnDimensions;
  472. return $this;
  473. }
  474. /**
  475. * Refresh row dimensions
  476. *
  477. * @return PHPExcel_Worksheet
  478. */
  479. public function refreshRowDimensions()
  480. {
  481. $currentRowDimensions = $this->getRowDimensions();
  482. $newRowDimensions = array();
  483. foreach ($currentRowDimensions as $objRowDimension) {
  484. $newRowDimensions[$objRowDimension->getRowIndex()] = $objRowDimension;
  485. }
  486. $this->_rowDimensions = $newRowDimensions;
  487. return $this;
  488. }
  489. /**
  490. * Calculate worksheet dimension
  491. *
  492. * @return string String containing the dimension of this worksheet
  493. */
  494. public function calculateWorksheetDimension()
  495. {
  496. // Return
  497. return 'A1' . ':' . $this->getHighestColumn() . $this->getHighestRow();
  498. }
  499. /**
  500. * Calculate widths for auto-size columns
  501. *
  502. * @param boolean $calculateMergeCells Calculate merge cell width
  503. * @return PHPExcel_Worksheet;
  504. */
  505. public function calculateColumnWidths($calculateMergeCells = false)
  506. {
  507. $autoSizes = array();
  508. foreach ($this->getColumnDimensions() as $colDimension) {
  509. if ($colDimension->getAutoSize()) {
  510. $autoSizes[$colDimension->getColumnIndex()] = -1;
  511. }
  512. }
  513. foreach ($this->getCellCollection() as $cell) {
  514. if (isset($autoSizes[$cell->getColumn()])) {
  515. // Calculated value
  516. $cellValue = $cell->getCalculatedValue();
  517. // To formatted string
  518. $cellValue = PHPExcel_Style_NumberFormat::toFormattedString($cellValue, $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode());
  519. foreach ($this->getMergeCells() as $cells) {
  520. if ($cell->isInRange($cells) && !$calculateMergeCells) {
  521. $cellValue = ''; // do not calculate merge cells
  522. }
  523. }
  524. $autoSizes[$cell->getColumn()] = max(
  525. (float)$autoSizes[$cell->getColumn()],
  526. (float)PHPExcel_Shared_Font::calculateColumnWidth(
  527. $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getFont(),
  528. $cellValue,
  529. $this->getParent()->getCellXfByIndex($cell->getXfIndex())->getAlignment()->getTextRotation(),
  530. $this->getDefaultStyle()->getFont()
  531. )
  532. );
  533. }
  534. }
  535. foreach ($autoSizes as $columnIndex => $width) {
  536. if ($width == -1) $width = $this->getDefaultColumnDimension()->getWidth();
  537. $this->getColumnDimension($columnIndex)->setWidth($width);
  538. }
  539. return $this;
  540. }
  541. /**
  542. * Get parent
  543. *
  544. * @return PHPExcel
  545. */
  546. public function getParent() {
  547. return $this->_parent;
  548. }
  549. /**
  550. * Re-bind parent
  551. *
  552. * @param PHPExcel $parent
  553. * @return PHPExcel_Worksheet
  554. */
  555. public function rebindParent(PHPExcel $parent) {
  556. $namedRanges = $this->_parent->getNamedRanges();
  557. foreach ($namedRanges as $namedRange) {
  558. $parent->addNamedRange($namedRange);
  559. }
  560. $this->_parent->removeSheetByIndex(
  561. $this->_parent->getIndex($this)
  562. );
  563. $this->_parent = $parent;
  564. return $this;
  565. }
  566. /**
  567. * Get title
  568. *
  569. * @return string
  570. */
  571. public function getTitle()
  572. {
  573. return $this->_title;
  574. }
  575. /**
  576. * Set title
  577. *
  578. * @param string $pValue String containing the dimension of this worksheet
  579. * @return PHPExcel_Worksheet
  580. */
  581. public function setTitle($pValue = 'Worksheet')
  582. {
  583. // Is this a 'rename' or not?
  584. if ($this->getTitle() == $pValue) {
  585. return;
  586. }
  587. // Syntax check
  588. self::_checkSheetTitle($pValue);
  589. // Old title
  590. $oldTitle = $this->getTitle();
  591. // Is there already such sheet name?
  592. if ($this->getParent()->getSheetByName($pValue)) {
  593. // Use name, but append with lowest possible integer
  594. $i = 1;
  595. while ($this->getParent()->getSheetByName($pValue . ' ' . $i)) {
  596. ++$i;
  597. }
  598. $altTitle = $pValue . ' ' . $i;
  599. $this->setTitle($altTitle);
  600. return;
  601. }
  602. // Set title
  603. $this->_title = $pValue;
  604. // New title
  605. $newTitle = $this->getTitle();
  606. PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->getParent(), $oldTitle, $newTitle);
  607. return $this;
  608. }
  609. /**
  610. * Get sheet state
  611. *
  612. * @return string Sheet state (visible, hidden, veryHidden)
  613. */
  614. public function getSheetState() {
  615. return $this->_sheetState;
  616. }
  617. /**
  618. * Set sheet state
  619. *
  620. * @param string $value Sheet state (visible, hidden, veryHidden)
  621. * @return PHPExcel_Worksheet
  622. */
  623. public function setSheetState($value = PHPExcel_Worksheet::SHEETSTATE_VISIBLE) {
  624. $this->_sheetState = $value;
  625. return $this;
  626. }
  627. /**
  628. * Get page setup
  629. *
  630. * @return PHPExcel_Worksheet_PageSetup
  631. */
  632. public function getPageSetup()
  633. {
  634. return $this->_pageSetup;
  635. }
  636. /**
  637. * Set page setup
  638. *
  639. * @param PHPExcel_Worksheet_PageSetup $pValue
  640. * @return PHPExcel_Worksheet
  641. */
  642. public function setPageSetup(PHPExcel_Worksheet_PageSetup $pValue)
  643. {
  644. $this->_pageSetup = $pValue;
  645. return $this;
  646. }
  647. /**
  648. * Get page margins
  649. *
  650. * @return PHPExcel_Worksheet_PageMargins
  651. */
  652. public function getPageMargins()
  653. {
  654. return $this->_pageMargins;
  655. }
  656. /**
  657. * Set page margins
  658. *
  659. * @param PHPExcel_Worksheet_PageMargins $pValue
  660. * @return PHPExcel_Worksheet
  661. */
  662. public function setPageMargins(PHPExcel_Worksheet_PageMargins $pValue)
  663. {
  664. $this->_pageMargins = $pValue;
  665. return $this;
  666. }
  667. /**
  668. * Get page header/footer
  669. *
  670. * @return PHPExcel_Worksheet_HeaderFooter
  671. */
  672. public function getHeaderFooter()
  673. {
  674. return $this->_headerFooter;
  675. }
  676. /**
  677. * Set page header/footer
  678. *
  679. * @param PHPExcel_Worksheet_HeaderFooter $pValue
  680. * @return PHPExcel_Worksheet
  681. */
  682. public function setHeaderFooter(PHPExcel_Worksheet_HeaderFooter $pValue)
  683. {
  684. $this->_headerFooter = $pValue;
  685. return $this;
  686. }
  687. /**
  688. * Get sheet view
  689. *
  690. * @return PHPExcel_Worksheet_HeaderFooter
  691. */
  692. public function getSheetView()
  693. {
  694. return $this->_sheetView;
  695. }
  696. /**
  697. * Set sheet view
  698. *
  699. * @param PHPExcel_Worksheet_SheetView $pValue
  700. * @return PHPExcel_Worksheet
  701. */
  702. public function setSheetView(PHPExcel_Worksheet_SheetView $pValue)
  703. {
  704. $this->_sheetView = $pValue;
  705. return $this;
  706. }
  707. /**
  708. * Get Protection
  709. *
  710. * @return PHPExcel_Worksheet_Protection
  711. */
  712. public function getProtection()
  713. {
  714. return $this->_protection;
  715. }
  716. /**
  717. * Set Protection
  718. *
  719. * @param PHPExcel_Worksheet_Protection $pValue
  720. * @return PHPExcel_Worksheet
  721. */
  722. public function setProtection(PHPExcel_Worksheet_Protection $pValue)
  723. {
  724. $this->_protection = $pValue;
  725. return $this;
  726. }
  727. /**
  728. * Get highest worksheet column
  729. *
  730. * @return string Highest column name
  731. */
  732. public function getHighestColumn()
  733. {
  734. return $this->_cachedHighestColumn;
  735. }
  736. /**
  737. * Get highest worksheet row
  738. *
  739. * @return int Highest row number
  740. */
  741. public function getHighestRow()
  742. {
  743. return $this->_cachedHighestRow;
  744. }
  745. /**
  746. * Set a cell value
  747. *
  748. * @param string $pCoordinate Coordinate of the cell
  749. * @param mixed $pValue Value of the cell
  750. * @return PHPExcel_Worksheet
  751. */
  752. public function setCellValue($pCoordinate = 'A1', $pValue = null)
  753. {
  754. // Set value
  755. $this->getCell($pCoordinate)->setValue($pValue);
  756. return $this;
  757. }
  758. /**
  759. * Set a cell value by using numeric cell coordinates
  760. *
  761. * @param string $pColumn Numeric column coordinate of the cell
  762. * @param string $pRow Numeric row coordinate of the cell
  763. * @param mixed $pValue Value of the cell
  764. * @return PHPExcel_Worksheet
  765. */
  766. public function setCellValueByColumnAndRow($pColumn = 0, $pRow = 0, $pValue = null)
  767. {
  768. return $this->setCellValue(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow, $pValue);
  769. }
  770. /**
  771. * Set a cell value
  772. *
  773. * @param string $pCoordinate Coordinate of the cell
  774. * @param mixed $pValue Value of the cell
  775. * @param string $pDataType Explicit data type
  776. * @return PHPExcel_Worksheet
  777. */
  778. public function setCellValueExplicit($pCoordinate = 'A1', $pValue = null, $pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
  779. {
  780. // Set value
  781. $this->getCell($pCoordinate)->setValueExplicit($pValue, $pDataType);
  782. return $this;
  783. }
  784. /**
  785. * Set a cell value by using numeric cell coordinates
  786. *
  787. * @param string $pColumn Numeric column coordinate of the cell
  788. * @param string $pRow Numeric row coordinate of the cell
  789. * @param mixed $pValue Value of the cell
  790. * @param string $pDataType Explicit data type
  791. * @return PHPExcel_Worksheet
  792. */
  793. public function setCellValueExplicitByColumnAndRow($pColumn = 0, $pRow = 0, $pValue = null, $pDataType = PHPExcel_Cell_DataType::TYPE_STRING)
  794. {
  795. return $this->setCellValueExplicit(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow, $pValue, $pDataType);
  796. }
  797. /**
  798. * Get cell at a specific coordinate
  799. *
  800. * @param string $pCoordinate Coordinate of the cell
  801. * @throws Exception
  802. * @return PHPExcel_Cell Cell that was found
  803. */
  804. public function getCell($pCoordinate = 'A1')
  805. {
  806. // Check cell collection
  807. if (isset($this->_cellCollection[$pCoordinate])) {
  808. return $this->_cellCollection[$pCoordinate];
  809. }
  810. // Worksheet reference?
  811. if (strpos($pCoordinate, '!') !== false) {
  812. $worksheetReference = PHPExcel_Worksheet::extractSheetTitle($pCoordinate, true);
  813. return $this->getParent()->getSheetByName($worksheetReference[0])->getCell($worksheetReference[1]);
  814. }
  815. // Named range?
  816. if ((!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i', $pCoordinate, $matches)) &&
  817. (preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NAMEDRANGE.'$/i', $pCoordinate, $matches))) {
  818. $namedRange = PHPExcel_NamedRange::resolveRange($pCoordinate, $this);
  819. if (!is_null($namedRange)) {
  820. $pCoordinate = $namedRange->getRange();
  821. if ($this->getHashCode() != $namedRange->getWorksheet()->getHashCode()) {
  822. if (!$namedRange->getLocalOnly()) {
  823. return $namedRange->getWorksheet()->getCell($pCoordinate);
  824. } else {
  825. throw new Exception('Named range ' . $namedRange->getName() . ' is not accessible from within sheet ' . $this->getTitle());
  826. }
  827. } else {
  828. //Allow named ranges within the same sheet.
  829. return $this->getCell($pCoordinate);
  830. }
  831. }
  832. }
  833. // Uppercase coordinate
  834. $pCoordinate = strtoupper($pCoordinate);
  835. if (strpos($pCoordinate,':') !== false || strpos($pCoordinate,',') !== false) {
  836. throw new Exception('Cell coordinate can not be a range of cells.');
  837. } elseif (strpos($pCoordinate,'$') !== false) {
  838. throw new Exception('Cell coordinate must not be absolute.');
  839. } else {
  840. // Create new cell object
  841. // Coordinates
  842. $aCoordinates = PHPExcel_Cell::coordinateFromString($pCoordinate);
  843. $this->_cellCollection[$pCoordinate] = new PHPExcel_Cell($aCoordinates[0], $aCoordinates[1], null, PHPExcel_Cell_DataType::TYPE_NULL, $this);
  844. $this->_cellCollectionIsSorted = false;
  845. if (PHPExcel_Cell::columnIndexFromString($this->_cachedHighestColumn) < PHPExcel_Cell::columnIndexFromString($aCoordinates[0]))
  846. $this->_cachedHighestColumn = $aCoordinates[0];
  847. if ($this->_cachedHighestRow < $aCoordinates[1])
  848. $this->_cachedHighestRow = $aCoordinates[1];
  849. // Cell needs appropriate xfIndex
  850. $rowDimensions = $this->getRowDimensions();
  851. $columnDimensions = $this->getColumnDimensions();
  852. if ( isset($rowDimensions[$aCoordinates[1]]) && $rowDimensions[$aCoordinates[1]]->getXfIndex() !== null ) {
  853. // then there is a row dimension with explicit style, assign it to the cell
  854. $this->_cellCollection[$pCoordinate]->setXfIndex($rowDimensions[$aCoordinates[1]]->getXfIndex());
  855. } else if ( isset($columnDimensions[$aCoordinates[0]]) ) {
  856. // then there is a column dimension, assign it to the cell
  857. $this->_cellCollection[$pCoordinate]->setXfIndex($columnDimensions[$aCoordinates[0]]->getXfIndex());
  858. } else {
  859. // set to default index
  860. $this->_cellCollection[$pCoordinate]->setXfIndex(0);
  861. }
  862. return $this->_cellCollection[$pCoordinate];
  863. }
  864. }
  865. /**
  866. * Get cell at a specific coordinate by using numeric cell coordinates
  867. *
  868. * @param string $pColumn Numeric column coordinate of the cell
  869. * @param string $pRow Numeric row coordinate of the cell
  870. * @return PHPExcel_Cell Cell that was found
  871. */
  872. public function getCellByColumnAndRow($pColumn = 0, $pRow = 0)
  873. {
  874. $coordinate = PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow;
  875. if (!isset($this->_cellCollection[$coordinate])) {
  876. $columnLetter = PHPExcel_Cell::stringFromColumnIndex($pColumn);
  877. $this->_cellCollection[$coordinate] = new PHPExcel_Cell($columnLetter, $pRow, null, PHPExcel_Cell_DataType::TYPE_NULL, $this);
  878. $this->_cellCollectionIsSorted = false;
  879. if (PHPExcel_Cell::columnIndexFromString($this->_cachedHighestColumn) < $pColumn)
  880. $this->_cachedHighestColumn = $pColumn;
  881. if ($this->_cachedHighestRow < $pRow)
  882. $this->_cachedHighestRow = $pRow;
  883. }
  884. return $this->_cellCollection[$coordinate];
  885. }
  886. /**
  887. * Cell at a specific coordinate exists?
  888. *
  889. * @param string $pCoordinate Coordinate of the cell
  890. * @throws Exception
  891. * @return boolean
  892. */
  893. public function cellExists($pCoordinate = 'A1')
  894. {
  895. // Worksheet reference?
  896. if (strpos($pCoordinate, '!') !== false) {
  897. $worksheetReference = PHPExcel_Worksheet::extractSheetTitle($pCoordinate, true);
  898. return $this->getParent()->getSheetByName($worksheetReference[0])->cellExists($worksheetReference[1]);
  899. }
  900. // Named range?
  901. if ((!preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_CELLREF.'$/i', $pCoordinate, $matches)) &&
  902. (preg_match('/^'.PHPExcel_Calculation::CALCULATION_REGEXP_NAMEDRANGE.'$/i', $pCoordinate, $matches))) {
  903. $namedRange = PHPExcel_NamedRange::resolveRange($pCoordinate, $this);
  904. if (!is_null($namedRange)) {
  905. $pCoordinate = $namedRange->getRange();
  906. if ($this->getHashCode() != $namedRange->getWorksheet()->getHashCode()) {
  907. if (!$namedRange->getLocalOnly()) {
  908. return $namedRange->getWorksheet()->cellExists($pCoordinate);
  909. } else {
  910. throw new Exception('Named range ' . $namedRange->getName() . ' is not accessible from within sheet ' . $this->getTitle());
  911. }
  912. }
  913. }
  914. }
  915. // Uppercase coordinate
  916. $pCoordinate = strtoupper($pCoordinate);
  917. if (strpos($pCoordinate,':') !== false || strpos($pCoordinate,',') !== false) {
  918. throw new Exception('Cell coordinate can not be a range of cells.');
  919. } elseif (strpos($pCoordinate,'$') !== false) {
  920. throw new Exception('Cell coordinate must not be absolute.');
  921. } else {
  922. // Coordinates
  923. $aCoordinates = PHPExcel_Cell::coordinateFromString($pCoordinate);
  924. // Cell exists?
  925. return isset($this->_cellCollection[$pCoordinate]);
  926. }
  927. }
  928. /**
  929. * Cell at a specific coordinate by using numeric cell coordinates exists?
  930. *
  931. * @param string $pColumn Numeric column coordinate of the cell
  932. * @param string $pRow Numeric row coordinate of the cell
  933. * @return boolean
  934. */
  935. public function cellExistsByColumnAndRow($pColumn = 0, $pRow = 0)
  936. {
  937. return $this->cellExists(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow);
  938. }
  939. /**
  940. * Get row dimension at a specific row
  941. *
  942. * @param int $pRow Numeric index of the row
  943. * @return PHPExcel_Worksheet_RowDimension
  944. */
  945. public function getRowDimension($pRow = 0)
  946. {
  947. // Found
  948. $found = null;
  949. // Get row dimension
  950. if (!isset($this->_rowDimensions[$pRow])) {
  951. $this->_rowDimensions[$pRow] = new PHPExcel_Worksheet_RowDimension($pRow);
  952. if ($this->_cachedHighestRow < $pRow)
  953. $this->_cachedHighestRow = $pRow;
  954. }
  955. return $this->_rowDimensions[$pRow];
  956. }
  957. /**
  958. * Get column dimension at a specific column
  959. *
  960. * @param string $pColumn String index of the column
  961. * @return PHPExcel_Worksheet_ColumnDimension
  962. */
  963. public function getColumnDimension($pColumn = 'A')
  964. {
  965. // Uppercase coordinate
  966. $pColumn = strtoupper($pColumn);
  967. // Fetch dimensions
  968. if (!isset($this->_columnDimensions[$pColumn])) {
  969. $this->_columnDimensions[$pColumn] = new PHPExcel_Worksheet_ColumnDimension($pColumn);
  970. if (PHPExcel_Cell::columnIndexFromString($this->_cachedHighestColumn) < PHPExcel_Cell::columnIndexFromString($pColumn))
  971. $this->_cachedHighestColumn = $pColumn;
  972. }
  973. return $this->_columnDimensions[$pColumn];
  974. }
  975. /**
  976. * Get column dimension at a specific column by using numeric cell coordinates
  977. *
  978. * @param string $pColumn Numeric column coordinate of the cell
  979. * @param string $pRow Numeric row coordinate of the cell
  980. * @return PHPExcel_Worksheet_ColumnDimension
  981. */
  982. public function getColumnDimensionByColumn($pColumn = 0)
  983. {
  984. return $this->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($pColumn));
  985. }
  986. /**
  987. * Get styles
  988. *
  989. * @return PHPExcel_Style[]
  990. */
  991. public function getStyles()
  992. {
  993. return $this->_styles;
  994. }
  995. /**
  996. * Get default style of workbork.
  997. *
  998. * @deprecated
  999. * @return PHPExcel_Style
  1000. * @throws Exception
  1001. */
  1002. public function getDefaultStyle()
  1003. {
  1004. return $this->_parent->getDefaultStyle();
  1005. }
  1006. /**
  1007. * Set default style - should only be used by PHPExcel_IReader implementations!
  1008. *
  1009. * @deprecated
  1010. * @param PHPExcel_Style $value
  1011. * @throws Exception
  1012. * @return PHPExcel_Worksheet
  1013. */
  1014. public function setDefaultStyle(PHPExcel_Style $value)
  1015. {
  1016. $this->_parent->setDefaultStyle($value);
  1017. return $this;
  1018. }
  1019. /**
  1020. * Get style for cell
  1021. *
  1022. * @param string $pCellCoordinate Cell coordinate to get style for
  1023. * @return PHPExcel_Style
  1024. * @throws Exception
  1025. */
  1026. public function getStyle($pCellCoordinate = 'A1')
  1027. {
  1028. // set this sheet as active
  1029. $this->_parent->setActiveSheetIndex($this->_parent->getIndex($this));
  1030. // set cell coordinate as active
  1031. $this->setXSelectedCells($pCellCoordinate);
  1032. return $this->_parent->getCellXfSupervisor();
  1033. }
  1034. /**
  1035. * Get conditional styles for a cell
  1036. *
  1037. * @param string $pCoordinate
  1038. * @return PHPExcel_Style_Conditional[]
  1039. */
  1040. public function getConditionalStyles($pCoordinate = 'A1')
  1041. {
  1042. if (!isset($this->_conditionalStylesCollection[$pCoordinate])) {
  1043. $this->_conditionalStylesCollection[$pCoordinate] = array();
  1044. }
  1045. return $this->_conditionalStylesCollection[$pCoordinate];
  1046. }
  1047. /**
  1048. * Do conditional styles exist for this cell?
  1049. *
  1050. * @param string $pCoordinate
  1051. * @return boolean
  1052. */
  1053. public function conditionalStylesExists($pCoordinate = 'A1')
  1054. {
  1055. if (isset($this->_conditionalStylesCollection[$pCoordinate])) {
  1056. return true;
  1057. }
  1058. return false;
  1059. }
  1060. /**
  1061. * Removes conditional styles for a cell
  1062. *
  1063. * @param string $pCoordinate
  1064. * @return PHPExcel_Worksheet
  1065. */
  1066. public function removeConditionalStyles($pCoordinate = 'A1')
  1067. {
  1068. unset($this->_conditionalStylesCollection[$pCoordinate]);
  1069. return $this;
  1070. }
  1071. /**
  1072. * Get collection of conditional styles
  1073. *
  1074. * @return array
  1075. */
  1076. public function getConditionalStylesCollection()
  1077. {
  1078. return $this->_conditionalStylesCollection;
  1079. }
  1080. /**
  1081. * Set conditional styles
  1082. *
  1083. * @param $pCoordinate string E.g. 'A1'
  1084. * @param $pValue PHPExcel_Style_Conditional[]
  1085. * @return PHPExcel_Worksheet
  1086. */
  1087. public function setConditionalStyles($pCoordinate = 'A1', $pValue)
  1088. {
  1089. $this->_conditionalStylesCollection[$pCoordinate] = $pValue;
  1090. return $this;
  1091. }
  1092. /**
  1093. * Get style for cell by using numeric cell coordinates
  1094. *
  1095. * @param int $pColumn Numeric column coordinate of the cell
  1096. * @param int $pRow Numeric row coordinate of the cell
  1097. * @return PHPExcel_Style
  1098. */
  1099. public function getStyleByColumnAndRow($pColumn = 0, $pRow = 0)
  1100. {
  1101. return $this->getStyle(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow);
  1102. }
  1103. /**
  1104. * Set shared cell style to a range of cells
  1105. *
  1106. * Please note that this will overwrite existing cell styles for cells in range!
  1107. *
  1108. * @deprecated
  1109. * @param PHPExcel_Style $pSharedCellStyle Cell style to share
  1110. * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
  1111. * @throws Exception
  1112. * @return PHPExcel_Worksheet
  1113. */
  1114. public function setSharedStyle(PHPExcel_Style $pSharedCellStyle = null, $pRange = '')
  1115. {
  1116. $this->duplicateStyle($pSharedCellStyle, $pRange);
  1117. return $this;
  1118. }
  1119. /**
  1120. * Duplicate cell style to a range of cells
  1121. *
  1122. * Please note that this will overwrite existing cell styles for cells in range!
  1123. *
  1124. * @param PHPExcel_Style $pCellStyle Cell style to duplicate
  1125. * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
  1126. * @throws Exception
  1127. * @return PHPExcel_Worksheet
  1128. */
  1129. public function duplicateStyle(PHPExcel_Style $pCellStyle = null, $pRange = '')
  1130. {
  1131. // make sure we have a real style and not supervisor
  1132. $style = $pCellStyle->getIsSupervisor() ? $pCellStyle->getSharedComponent() : $pCellStyle;
  1133. // Add the style to the workbook if necessary
  1134. $workbook = $this->_parent;
  1135. if ($existingStyle = $this->_parent->getCellXfByHashCode($pCellStyle->getHashCode())) {
  1136. // there is already such cell Xf in our collection
  1137. $xfIndex = $existingStyle->getIndex();
  1138. } else {
  1139. // we don't have such a cell Xf, need to add
  1140. $workbook->addCellXf($pCellStyle);
  1141. $xfIndex = $pCellStyle->getIndex();
  1142. }
  1143. // Uppercase coordinate
  1144. $pRange = strtoupper($pRange);
  1145. // Is it a cell range or a single cell?
  1146. $rangeA = '';
  1147. $rangeB = '';
  1148. if (strpos($pRange, ':') === false) {
  1149. $rangeA = $pRange;
  1150. $rangeB = $pRange;
  1151. } else {
  1152. list($rangeA, $rangeB) = explode(':', $pRange);
  1153. }
  1154. // Calculate range outer borders
  1155. $rangeStart = PHPExcel_Cell::coordinateFromString($rangeA);
  1156. $rangeEnd = PHPExcel_Cell::coordinateFromString($rangeB);
  1157. // Translate column into index
  1158. $rangeStart[0] = PHPExcel_Cell::columnIndexFromString($rangeStart[0]) - 1;
  1159. $rangeEnd[0] = PHPExcel_Cell::columnIndexFromString($rangeEnd[0]) - 1;
  1160. // Make sure we can loop upwards on rows and columns
  1161. if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) {
  1162. $tmp = $rangeStart;
  1163. $rangeStart = $rangeEnd;
  1164. $rangeEnd = $tmp;
  1165. }
  1166. // Loop through cells and apply styles
  1167. for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
  1168. for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
  1169. $this->getCell(PHPExcel_Cell::stringFromColumnIndex($col) . $row)->setXfIndex($xfIndex);
  1170. }
  1171. }
  1172. return $this;
  1173. }
  1174. /**
  1175. * Duplicate cell style array to a range of cells
  1176. *
  1177. * Please note that this will overwrite existing cell styles for cells in range,
  1178. * if they are in the styles array. For example, if you decide to set a range of
  1179. * cells to font bold, only include font bold in the styles array.
  1180. *
  1181. * @deprecated
  1182. * @param array $pStyles Array containing style information
  1183. * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
  1184. * @param boolean $pAdvanced Advanced mode for setting borders.
  1185. * @throws Exception
  1186. * @return PHPExcel_Worksheet
  1187. */
  1188. public function duplicateStyleArray($pStyles = null, $pRange = '', $pAdvanced = true)
  1189. {
  1190. $this->getStyle($pRange)->applyFromArray($pStyles, $pAdvanced);
  1191. return $this;
  1192. }
  1193. /**
  1194. * Set break on a cell
  1195. *
  1196. * @param string $pCell Cell coordinate (e.g. A1)
  1197. * @param int $pBreak Break type (type of PHPExcel_Worksheet::BREAK_*)
  1198. * @throws Exception
  1199. * @return PHPExcel_Worksheet
  1200. */
  1201. public function setBreak($pCell = 'A1', $pBreak = PHPExcel_Worksheet::BREAK_NONE)
  1202. {
  1203. // Uppercase coordinate
  1204. $pCell = strtoupper($pCell);
  1205. if ($pCell != '') {
  1206. $this->_breaks[$pCell] = $pBreak;
  1207. } else {
  1208. throw new Exception('No cell coordinate specified.');
  1209. }
  1210. return $this;
  1211. }
  1212. /**
  1213. * Set break on a cell by using numeric cell coordinates
  1214. *
  1215. * @param int $pColumn Numeric column coordinate of the cell
  1216. * @param int $pRow Numeric row coordinate of the cell
  1217. * @param int $pBreak Break type (type of PHPExcel_Worksheet::BREAK_*)
  1218. * @throws Exception
  1219. * @return PHPExcel_Worksheet
  1220. */
  1221. public function setBreakByColumnAndRow($pColumn = 0, $pRow = 0, $pBreak = PHPExcel_Worksheet::BREAK_NONE)
  1222. {
  1223. return $this->setBreak(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow, $pBreak);
  1224. }
  1225. /**
  1226. * Get breaks
  1227. *
  1228. * @return array[]
  1229. */
  1230. public function getBreaks()
  1231. {
  1232. return $this->_breaks;
  1233. }
  1234. /**
  1235. * Set merge on a cell range
  1236. *
  1237. * @param string $pRange Cell range (e.g. A1:E1)
  1238. * @throws Exception
  1239. * @return PHPExcel_Worksheet
  1240. */
  1241. public function mergeCells($pRange = 'A1:A1')
  1242. {
  1243. // Uppercase coordinate
  1244. $pRange = strtoupper($pRange);
  1245. if (strpos($pRange,':') !== false) {
  1246. $this->_mergeCells[$pRange] = $pRange;
  1247. } else {
  1248. throw new Exception('Merge must be set on a range of cells.');
  1249. }
  1250. return $this;
  1251. }
  1252. /**
  1253. * Set merge on a cell range by using numeric cell coordinates
  1254. *
  1255. * @param int $pColumn1 Numeric column coordinate of the first cell
  1256. * @param int $pRow1 Numeric row coordinate of the first cell
  1257. * @param int $pColumn2 Numeric column coordinate of the last cell
  1258. * @param int $pRow2 Numeric row coordinate of the last cell
  1259. * @throws Exception
  1260. * @return PHPExcel_Worksheet
  1261. */
  1262. public function mergeCellsByColumnAndRow($pColumn1 = 0, $pRow1 = 0, $pColumn2 = 0, $pRow2 = 0)
  1263. {
  1264. $cellRange = PHPExcel_Cell::stringFromColumnIndex($pColumn1) . $pRow1 . ':' . PHPExcel_Cell::stringFromColumnIndex($pColumn2) . $pRow2;
  1265. return $this->mergeCells($cellRange);
  1266. }
  1267. /**
  1268. * Remove merge on a cell range
  1269. *
  1270. * @param string $pRange Cell range (e.g. A1:E1)
  1271. * @throws Exception
  1272. * @return PHPExcel_Worksheet
  1273. */
  1274. public function unmergeCells($pRange = 'A1:A1')
  1275. {
  1276. // Uppercase coordinate
  1277. $pRange = strtoupper($pRange);
  1278. if (strpos($pRange,':') !== false) {
  1279. if (isset($this->_mergeCells[$pRange])) {
  1280. unset($this->_mergeCells[$pRange]);
  1281. } else {
  1282. throw new Exception('Cell range ' . $pRange . ' not known as merged.');
  1283. }
  1284. } else {
  1285. throw new Exception('Merge can only be removed from a range of cells.');
  1286. }
  1287. return $this;
  1288. }
  1289. /**
  1290. * Remove merge on a cell range by using numeric cell coordinates
  1291. *
  1292. * @param int $pColumn1 Numeric column coordinate of the first cell
  1293. * @param int $pRow1 Numeric row coordinate of the first cell
  1294. * @param int $pColumn2 Numeric column coordinate of the last cell
  1295. * @param int $pRow2 Numeric row coordinate of the last cell
  1296. * @throws Exception
  1297. * @return PHPExcel_Worksheet
  1298. */
  1299. public function unmergeCellsByColumnAndRow($pColumn1 = 0, $pRow1 = 0, $pColumn2 = 0, $pRow2 = 0)
  1300. {
  1301. $cellRange = PHPExcel_Cell::stringFromColumnIndex($pColumn1) . $pRow1 . ':' . PHPExcel_Cell::stringFromColumnIndex($pColumn2) . $pRow2;
  1302. return $this->unmergeCells($cellRange);
  1303. }
  1304. /**
  1305. * Get merge cells array.
  1306. *
  1307. * @return array[]
  1308. */
  1309. public function getMergeCells()
  1310. {
  1311. return $this->_mergeCells;
  1312. }
  1313. /**
  1314. * Set merge cells array for the entire sheet. Use instead mergeCells() to merge
  1315. * a single cell range.
  1316. *
  1317. * @param array
  1318. */
  1319. public function setMergeCells($pValue = array())
  1320. {
  1321. $this->_mergeCells = $pValue;
  1322. return $this;
  1323. }
  1324. /**
  1325. * Set protection on a cell range
  1326. *
  1327. * @param string $pRange Cell (e.g. A1) or cell range (e.g. A1:E1)
  1328. * @param string $pPassword Password to unlock the protection
  1329. * @param boolean $pAlreadyHashed If the password has already been hashed, set this to true
  1330. * @throws Exception
  1331. * @return PHPExcel_Worksheet
  1332. */
  1333. public function protectCells($pRange = 'A1', $pPassword = '', $pAlreadyHashed = false)
  1334. {
  1335. // Uppercase coordinate
  1336. $pRange = strtoupper($pRange);
  1337. if (!$pAlreadyHashed) {
  1338. $pPassword = PHPExcel_Shared_PasswordHasher::hashPassword($pPassword);
  1339. }
  1340. $this->_protectedCells[$pRange] = $pPassword;
  1341. return $this;
  1342. }
  1343. /**
  1344. * Set protection on a cell range by using numeric cell coordinates
  1345. *
  1346. * @param int $pColumn1 Numeric column coordinate of the first cell
  1347. * @param int $pRow1 Numeric row coordinate of the first cell
  1348. * @param int $pColumn2 Numeric column coordinate of the last cell
  1349. * @param int $pRow2 Numeric row coordinate of the last cell
  1350. * @param string $pPassword Password to unlock the protection
  1351. * @param boolean $pAlreadyHashed If the password has already been hashed, set this to true
  1352. * @throws Exception
  1353. * @return PHPExcel_Worksheet
  1354. */
  1355. public function protectCellsByColumnAndRow($pColumn1 = 0, $pRow1 = 0, $pColumn2 = 0, $pRow2 = 0, $pPassword = '', $pAlreadyHashed = false)
  1356. {
  1357. $cellRange = PHPExcel_Cell::stringFromColumnIndex($pColumn1) . $pRow1 . ':' . PHPExcel_Cell::stringFromColumnIndex($pColumn2) . $pRow2;
  1358. return $this->protectCells($cellRange, $pPassword, $pAlreadyHashed);
  1359. }
  1360. /**
  1361. * Remove protection on a cell range
  1362. *
  1363. * @param string $pRange Cell (e.g. A1) or cell range (e.g. A1:E1)
  1364. * @throws Exception
  1365. * @return PHPExcel_Worksheet
  1366. */
  1367. public function unprotectCells($pRange = 'A1')
  1368. {
  1369. // Uppercase coordinate
  1370. $pRange = strtoupper($pRange);
  1371. if (isset($this->_protectedCells[$pRange])) {
  1372. unset($this->_protectedCells[$pRange]);
  1373. } else {
  1374. throw new Exception('Cell range ' . $pRange . ' not known as protected.');
  1375. }
  1376. return $this;
  1377. }
  1378. /**
  1379. * Remove protection on a cell range by using numeric cell coordinates
  1380. *
  1381. * @param int $pColumn1 Numeric column coordinate of the first cell
  1382. * @param int $pRow1 Numeric row coordinate of the first cell
  1383. * @param int $pColumn2 Numeric column coordinate of the last cell
  1384. * @param int $pRow2 Numeric row coordinate of the last cell
  1385. * @param string $pPassword Password to unlock the protection
  1386. * @param boolean $pAlreadyHashed If the password has already been hashed, set this to true
  1387. * @throws Exception
  1388. * @return PHPExcel_Worksheet
  1389. */
  1390. public function unprotectCellsByColumnAndRow($pColumn1 = 0, $pRow1 = 0, $pColumn2 = 0, $pRow2 = 0, $pPassword = '', $pAlreadyHashed = false)
  1391. {
  1392. $cellRange = PHPExcel_Cell::stringFromColumnIndex($pColumn1) . $pRow1 . ':' . PHPExcel_Cell::stringFromColumnIndex($pColumn2) . $pRow2;
  1393. return $this->unprotectCells($cellRange, $pPassword, $pAlreadyHashed);
  1394. }
  1395. /**
  1396. * Get protected cells
  1397. *
  1398. * @return array[]
  1399. */
  1400. public function getProtectedCells()
  1401. {
  1402. return $this->_protectedCells;
  1403. }
  1404. /**
  1405. * Get Autofilter Range
  1406. *
  1407. * @return string
  1408. */
  1409. public function getAutoFilter()
  1410. {
  1411. return $this->_autoFilter;
  1412. }
  1413. /**
  1414. * Set Autofilter Range
  1415. *
  1416. * @param string $pRange Cell range (i.e. A1:E10)
  1417. * @throws Exception
  1418. * @return PHPExcel_Worksheet
  1419. */
  1420. public function setAutoFilter($pRange = '')
  1421. {
  1422. // Uppercase coordinate
  1423. $pRange = strtoupper($pRange);
  1424. if (strpos($pRange,':') !== false) {
  1425. $this->_autoFilter = $pRange;
  1426. } else {
  1427. throw new Exception('Autofilter must be set on a range of cells.');
  1428. }
  1429. return $this;
  1430. }
  1431. /**
  1432. * Set Autofilter Range by using numeric cell coordinates
  1433. *
  1434. * @param int $pColumn1 Numeric column coordinate of the first cell
  1435. * @param int $pRow1 Numeric row coordinate of the first cell
  1436. * @param int $pColumn2 Numeric column coordinate of the second cell
  1437. * @param int $pRow2 Numeric row coordinate of the second cell
  1438. * @throws Exception
  1439. * @return PHPExcel_Worksheet
  1440. */
  1441. public function setAutoFilterByColumnAndRow($pColumn1 = 0, $pRow1 = 0, $pColumn2 = 0, $pRow2 = 0)
  1442. {
  1443. return $this->setAutoFilter(
  1444. PHPExcel_Cell::stringFromColumnIndex($pColumn1) . $pRow1
  1445. . ':' .
  1446. PHPExcel_Cell::stringFromColumnIndex($pColumn2) . $pRow2
  1447. );
  1448. }
  1449. /**
  1450. * Get Freeze Pane
  1451. *
  1452. * @return string
  1453. */
  1454. public function getFreezePane()
  1455. {
  1456. return $this->_freezePane;
  1457. }
  1458. /**
  1459. * Freeze Pane
  1460. *
  1461. * @param string $pCell Cell (i.e. A1)
  1462. * @throws Exception
  1463. * @return PHPExcel_Worksheet
  1464. */
  1465. public function freezePane($pCell = '')
  1466. {
  1467. // Uppercase coordinate
  1468. $pCell = strtoupper($pCell);
  1469. if (strpos($pCell,':') === false && strpos($pCell,',') === false) {
  1470. $this->_freezePane = $pCell;
  1471. } else {
  1472. throw new Exception('Freeze pane can not be set on a range of cells.');
  1473. }
  1474. return $this;
  1475. }
  1476. /**
  1477. * Freeze Pane by using numeric cell coordinates
  1478. *
  1479. * @param int $pColumn Numeric column coordinate of the cell
  1480. * @param int $pRow Numeric row coordinate of the cell
  1481. * @throws Exception
  1482. * @return PHPExcel_Worksheet
  1483. */
  1484. public function freezePaneByColumnAndRow($pColumn = 0, $pRow = 0)
  1485. {
  1486. return $this->freezePane(PHPExcel_Cell::stringFromColumnIndex($pColumn) . $pRow);
  1487. }
  1488. /**
  1489. * Unfreeze Pane
  1490. *
  1491. * @return PHPExcel_Worksheet
  1492. */
  1493. public function unfreezePane()
  1494. {
  1495. return $this->freezePane('');
  1496. }
  1497. /**
  1498. * Insert a new row, updating all possible related data
  1499. *
  1500. * @param int $pBefore Insert before this one
  1501. * @param int $pNumRows Number of rows to insert
  1502. * @throws Exception
  1503. * @return PHPExcel_Worksheet
  1504. */
  1505. public function insertNewRowBefore($pBefore = 1, $pNumRows = 1) {
  1506. if ($pBefore >= 1) {
  1507. $objReferenceHelper = PHPExcel_ReferenceHelper::getInstance();
  1508. $objReferenceHelper->insertNewBefore('A' . $pBefore, 0, $pNumRows, $this);
  1509. } else {
  1510. throw new Exception("Rows can only be inserted before at least row 1.");
  1511. }
  1512. return $this;
  1513. }
  1514. /**
  1515. * Insert a new column, updating all possible related data
  1516. *
  1517. * @param int $pBefore Insert before this one
  1518. * @param int $pNumCols Number of columns to insert
  1519. * @throws Exception
  1520. * @return PHPExcel_Worksheet
  1521. */
  1522. public function insertNewColumnBefore($pBefore = 'A', $pNumCols = 1) {
  1523. if (!is_numeric($pBefore)) {
  1524. $objReferenceHelper = PHPExcel_ReferenceHelper::getInstance();
  1525. $objReferenceHelper->insertNewBefore($pBefore . '1', $pNumCols, 0, $this);
  1526. } else {
  1527. throw new Exception("Column references should not be numeric.");
  1528. }
  1529. return $this;
  1530. }
  1531. /**
  1532. * Insert a new column, updating all possible related data
  1533. *
  1534. * @param int $pBefore Insert before this one (numeric column coordinate of the cell)
  1535. * @param int $pNumCols Number of columns to insert
  1536. * @throws Exception
  1537. * @return PHPExcel_Worksheet
  1538. */
  1539. public function insertNewColumnBeforeByIndex($pBefore = 0, $pNumCols = 1) {
  1540. if ($pBefore >= 0) {
  1541. return $this->insertNewColumnBefore(PHPExcel_Cell::stringFromColumnIndex($pBefore), $pNumCols);
  1542. } else {
  1543. throw new Exception("Columns can only be inserted before at least column A (0).");
  1544. }
  1545. }
  1546. /**
  1547. * Delete a row, updating all possible related data
  1548. *
  1549. * @param int $pRow Remove starting with this one
  1550. * @param int $pNumRows Number of rows to remove
  1551. * @throws Exception
  1552. * @return PHPExcel_Worksheet
  1553. */
  1554. public function removeRow($pRow = 1, $pNumRows = 1) {
  1555. if ($pRow >= 1) {
  1556. $objReferenceHelper = PHPExcel_ReferenceHelper::getInstance();
  1557. $objReferenceHelper->insertNewBefore('A' . ($pRow + $pNumRows), 0, -$pNumRows, $this);
  1558. } else {
  1559. throw new Exception("Rows to be deleted should at least start from row 1.");
  1560. }
  1561. return $this;
  1562. }
  1563. /**
  1564. * Remove a column, updating all possible related data
  1565. *
  1566. * @param int $pColumn Remove starting with this one
  1567. * @param int $pNumCols Number of columns to remove
  1568. * @throws Exception
  1569. * @return PHPExcel_Worksheet
  1570. */
  1571. public function removeColumn($pColumn = 'A', $pNumCols = 1) {
  1572. if (!is_numeric($pColumn)) {
  1573. $pColumn = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($pColumn) - 1 + $pNumCols);
  1574. $objReferenceHelper = PHPExcel_ReferenceHelper::getInstance();
  1575. $objReferenceHelper->insertNewBefore($pColumn . '1', -$pNumCols, 0, $this);
  1576. } else {
  1577. throw new Exception("Column references should not be numeric.");
  1578. }
  1579. return $this;
  1580. }
  1581. /**
  1582. * Remove a column, updating all possible related data
  1583. *
  1584. * @param int $pColumn Remove starting with this one (numeric column coordinate of the cell)
  1585. * @param int $pNumCols Number of columns to re…

Large files files are truncated, but you can click here to view the full file