PHPExcel.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. <?php
  2. namespace PHPExcel;
  3. /** PHPExcel root directory */
  4. if (!defined('PHPEXCEL_ROOT')) {
  5. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/');
  6. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  7. }
  8. /**
  9. * PHPExcel
  10. *
  11. * Copyright (c) 2006 - 2015 PHPExcel
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. * @category PHPExcel
  28. * @package PHPExcel
  29. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  30. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  31. * @version ##VERSION##, ##DATE##
  32. */
  33. class PHPExcel
  34. {
  35. /**
  36. * Unique ID
  37. *
  38. * @var string
  39. */
  40. private $uniqueID;
  41. /**
  42. * Document properties
  43. *
  44. * @var PHPExcel_DocumentProperties
  45. */
  46. private $properties;
  47. /**
  48. * Document security
  49. *
  50. * @var PHPExcel_DocumentSecurity
  51. */
  52. private $security;
  53. /**
  54. * Collection of Worksheet objects
  55. *
  56. * @var PHPExcel_Worksheet[]
  57. */
  58. private $workSheetCollection = array();
  59. /**
  60. * Calculation Engine
  61. *
  62. * @var PHPExcel_Calculation
  63. */
  64. private $calculationEngine;
  65. /**
  66. * Active sheet index
  67. *
  68. * @var integer
  69. */
  70. private $activeSheetIndex = 0;
  71. /**
  72. * Named ranges
  73. *
  74. * @var PHPExcel_NamedRange[]
  75. */
  76. private $namedRanges = array();
  77. /**
  78. * CellXf supervisor
  79. *
  80. * @var PHPExcel_Style
  81. */
  82. private $cellXfSupervisor;
  83. /**
  84. * CellXf collection
  85. *
  86. * @var PHPExcel_Style[]
  87. */
  88. private $cellXfCollection = array();
  89. /**
  90. * CellStyleXf collection
  91. *
  92. * @var PHPExcel_Style[]
  93. */
  94. private $cellStyleXfCollection = array();
  95. /**
  96. * hasMacros : this workbook have macros ?
  97. *
  98. * @var bool
  99. */
  100. private $hasMacros = false;
  101. /**
  102. * macrosCode : all macros code (the vbaProject.bin file, this include form, code, etc.), null if no macro
  103. *
  104. * @var binary
  105. */
  106. private $macrosCode;
  107. /**
  108. * macrosCertificate : if macros are signed, contains vbaProjectSignature.bin file, null if not signed
  109. *
  110. * @var binary
  111. */
  112. private $macrosCertificate;
  113. /**
  114. * ribbonXMLData : null if workbook is'nt Excel 2007 or not contain a customized UI
  115. *
  116. * @var null|string
  117. */
  118. private $ribbonXMLData;
  119. /**
  120. * ribbonBinObjects : null if workbook is'nt Excel 2007 or not contain embedded objects (picture(s)) for Ribbon Elements
  121. * ignored if $ribbonXMLData is null
  122. *
  123. * @var null|array
  124. */
  125. private $ribbonBinObjects;
  126. /**
  127. * The workbook has macros ?
  128. *
  129. * @return boolean true if workbook has macros, false if not
  130. */
  131. public function hasMacros()
  132. {
  133. return $this->hasMacros;
  134. }
  135. /**
  136. * Define if a workbook has macros
  137. *
  138. * @param boolean $hasMacros true|false
  139. */
  140. public function setHasMacros($hasMacros = false)
  141. {
  142. $this->hasMacros = (bool) $hasMacros;
  143. }
  144. /**
  145. * Set the macros code
  146. *
  147. * @param string $MacrosCode string|null
  148. */
  149. public function setMacrosCode($MacrosCode = null)
  150. {
  151. $this->macrosCode=$MacrosCode;
  152. $this->setHasMacros(!is_null($MacrosCode));
  153. }
  154. /**
  155. * Return the macros code
  156. *
  157. * @return string|null
  158. */
  159. public function getMacrosCode()
  160. {
  161. return $this->macrosCode;
  162. }
  163. /**
  164. * Set the macros certificate
  165. *
  166. * @param string|null $Certificate
  167. */
  168. public function setMacrosCertificate($Certificate = null)
  169. {
  170. $this->macrosCertificate=$Certificate;
  171. }
  172. /**
  173. * Is the project signed ?
  174. *
  175. * @return boolean true|false
  176. */
  177. public function hasMacrosCertificate()
  178. {
  179. return !is_null($this->macrosCertificate);
  180. }
  181. /**
  182. * Return the macros certificate
  183. *
  184. * @return string|null
  185. */
  186. public function getMacrosCertificate()
  187. {
  188. return $this->macrosCertificate;
  189. }
  190. /**
  191. * Remove all macros, certificate from spreadsheet
  192. *
  193. */
  194. public function discardMacros()
  195. {
  196. $this->hasMacros=false;
  197. $this->macrosCode=null;
  198. $this->macrosCertificate=null;
  199. }
  200. /**
  201. * set ribbon XML data
  202. *
  203. */
  204. public function setRibbonXMLData($Target = null, $XMLData = null)
  205. {
  206. if (!is_null($Target) && !is_null($XMLData)) {
  207. $this->ribbonXMLData = array('target' => $Target, 'data' => $XMLData);
  208. } else {
  209. $this->ribbonXMLData = null;
  210. }
  211. }
  212. /**
  213. * retrieve ribbon XML Data
  214. *
  215. * return string|null|array
  216. */
  217. public function getRibbonXMLData($What = 'all') //we need some constants here...
  218. {
  219. $ReturnData = null;
  220. $What = strtolower($What);
  221. switch ($What){
  222. case 'all':
  223. $ReturnData = $this->ribbonXMLData;
  224. break;
  225. case 'target':
  226. case 'data':
  227. if (is_array($this->ribbonXMLData) && array_key_exists($What, $this->ribbonXMLData)) {
  228. $ReturnData = $this->ribbonXMLData[$What];
  229. }
  230. break;
  231. }
  232. return $ReturnData;
  233. }
  234. /**
  235. * store binaries ribbon objects (pictures)
  236. *
  237. */
  238. public function setRibbonBinObjects($BinObjectsNames = null, $BinObjectsData = null)
  239. {
  240. if (!is_null($BinObjectsNames) && !is_null($BinObjectsData)) {
  241. $this->ribbonBinObjects = array('names' => $BinObjectsNames, 'data' => $BinObjectsData);
  242. } else {
  243. $this->ribbonBinObjects = null;
  244. }
  245. }
  246. /**
  247. * return the extension of a filename. Internal use for a array_map callback (php<5.3 don't like lambda function)
  248. *
  249. */
  250. private function getExtensionOnly($ThePath)
  251. {
  252. return pathinfo($ThePath, PATHINFO_EXTENSION);
  253. }
  254. /**
  255. * retrieve Binaries Ribbon Objects
  256. *
  257. */
  258. public function getRibbonBinObjects($What = 'all')
  259. {
  260. $ReturnData = null;
  261. $What = strtolower($What);
  262. switch($What) {
  263. case 'all':
  264. return $this->ribbonBinObjects;
  265. break;
  266. case 'names':
  267. case 'data':
  268. if (is_array($this->ribbonBinObjects) && array_key_exists($What, $this->ribbonBinObjects)) {
  269. $ReturnData=$this->ribbonBinObjects[$What];
  270. }
  271. break;
  272. case 'types':
  273. if (is_array($this->ribbonBinObjects) &&
  274. array_key_exists('data', $this->ribbonBinObjects) && is_array($this->ribbonBinObjects['data'])) {
  275. $tmpTypes=array_keys($this->ribbonBinObjects['data']);
  276. $ReturnData = array_unique(array_map(array($this, 'getExtensionOnly'), $tmpTypes));
  277. } else {
  278. $ReturnData=array(); // the caller want an array... not null if empty
  279. }
  280. break;
  281. }
  282. return $ReturnData;
  283. }
  284. /**
  285. * This workbook have a custom UI ?
  286. *
  287. * @return boolean true|false
  288. */
  289. public function hasRibbon()
  290. {
  291. return !is_null($this->ribbonXMLData);
  292. }
  293. /**
  294. * This workbook have additionnal object for the ribbon ?
  295. *
  296. * @return boolean true|false
  297. */
  298. public function hasRibbonBinObjects()
  299. {
  300. return !is_null($this->ribbonBinObjects);
  301. }
  302. /**
  303. * Check if a sheet with a specified code name already exists
  304. *
  305. * @param string $pSheetCodeName Name of the worksheet to check
  306. * @return boolean
  307. */
  308. public function sheetCodeNameExists($pSheetCodeName)
  309. {
  310. return ($this->getSheetByCodeName($pSheetCodeName) !== null);
  311. }
  312. /**
  313. * Get sheet by code name. Warning : sheet don't have always a code name !
  314. *
  315. * @param string $pName Sheet name
  316. * @return PHPExcel_Worksheet
  317. */
  318. public function getSheetByCodeName($pName = '')
  319. {
  320. $worksheetCount = count($this->workSheetCollection);
  321. for ($i = 0; $i < $worksheetCount; ++$i) {
  322. if ($this->workSheetCollection[$i]->getCodeName() == $pName) {
  323. return $this->workSheetCollection[$i];
  324. }
  325. }
  326. return null;
  327. }
  328. /**
  329. * Create a new PHPExcel with one Worksheet
  330. */
  331. public function __construct()
  332. {
  333. $this->uniqueID = uniqid();
  334. $this->calculationEngine = new PHPExcel_Calculation($this);
  335. // Initialise worksheet collection and add one worksheet
  336. $this->workSheetCollection = array();
  337. $this->workSheetCollection[] = new PHPExcel_Worksheet($this);
  338. $this->activeSheetIndex = 0;
  339. // Create document properties
  340. $this->properties = new PHPExcel_DocumentProperties();
  341. // Create document security
  342. $this->security = new PHPExcel_DocumentSecurity();
  343. // Set named ranges
  344. $this->namedRanges = array();
  345. // Create the cellXf supervisor
  346. $this->cellXfSupervisor = new PHPExcel_Style(true);
  347. $this->cellXfSupervisor->bindParent($this);
  348. // Create the default style
  349. $this->addCellXf(new PHPExcel_Style);
  350. $this->addCellStyleXf(new PHPExcel_Style);
  351. }
  352. /**
  353. * Code to execute when this worksheet is unset()
  354. *
  355. */
  356. public function __destruct()
  357. {
  358. $this->calculationEngine = null;
  359. $this->disconnectWorksheets();
  360. }
  361. /**
  362. * Disconnect all worksheets from this PHPExcel workbook object,
  363. * typically so that the PHPExcel object can be unset
  364. *
  365. */
  366. public function disconnectWorksheets()
  367. {
  368. $worksheet = null;
  369. foreach ($this->workSheetCollection as $k => &$worksheet) {
  370. $worksheet->disconnectCells();
  371. $this->workSheetCollection[$k] = null;
  372. }
  373. unset($worksheet);
  374. $this->workSheetCollection = array();
  375. }
  376. /**
  377. * Return the calculation engine for this worksheet
  378. *
  379. * @return PHPExcel_Calculation
  380. */
  381. public function getCalculationEngine()
  382. {
  383. return $this->calculationEngine;
  384. } // function getCellCacheController()
  385. /**
  386. * Get properties
  387. *
  388. * @return PHPExcel_DocumentProperties
  389. */
  390. public function getProperties()
  391. {
  392. return $this->properties;
  393. }
  394. /**
  395. * Set properties
  396. *
  397. * @param PHPExcel_DocumentProperties $pValue
  398. */
  399. public function setProperties(PHPExcel_DocumentProperties $pValue)
  400. {
  401. $this->properties = $pValue;
  402. }
  403. /**
  404. * Get security
  405. *
  406. * @return PHPExcel_DocumentSecurity
  407. */
  408. public function getSecurity()
  409. {
  410. return $this->security;
  411. }
  412. /**
  413. * Set security
  414. *
  415. * @param PHPExcel_DocumentSecurity $pValue
  416. */
  417. public function setSecurity(PHPExcel_DocumentSecurity $pValue)
  418. {
  419. $this->security = $pValue;
  420. }
  421. /**
  422. * Get active sheet
  423. *
  424. * @return PHPExcel_Worksheet
  425. *
  426. * @throws PHPExcel_Exception
  427. */
  428. public function getActiveSheet()
  429. {
  430. return $this->getSheet($this->activeSheetIndex);
  431. }
  432. /**
  433. * Create sheet and add it to this workbook
  434. *
  435. * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  436. * @return PHPExcel_Worksheet
  437. * @throws PHPExcel_Exception
  438. */
  439. public function createSheet($iSheetIndex = null)
  440. {
  441. $newSheet = new PHPExcel_Worksheet($this);
  442. $this->addSheet($newSheet, $iSheetIndex);
  443. return $newSheet;
  444. }
  445. /**
  446. * Check if a sheet with a specified name already exists
  447. *
  448. * @param string $pSheetName Name of the worksheet to check
  449. * @return boolean
  450. */
  451. public function sheetNameExists($pSheetName)
  452. {
  453. return ($this->getSheetByName($pSheetName) !== null);
  454. }
  455. /**
  456. * Add sheet
  457. *
  458. * @param PHPExcel_Worksheet $pSheet
  459. * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  460. * @return PHPExcel_Worksheet
  461. * @throws PHPExcel_Exception
  462. */
  463. public function addSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null)
  464. {
  465. if ($this->sheetNameExists($pSheet->getTitle())) {
  466. throw new PHPExcel_Exception(
  467. "Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first."
  468. );
  469. }
  470. if ($iSheetIndex === null) {
  471. if ($this->activeSheetIndex < 0) {
  472. $this->activeSheetIndex = 0;
  473. }
  474. $this->workSheetCollection[] = $pSheet;
  475. } else {
  476. // Insert the sheet at the requested index
  477. array_splice(
  478. $this->workSheetCollection,
  479. $iSheetIndex,
  480. 0,
  481. array($pSheet)
  482. );
  483. // Adjust active sheet index if necessary
  484. if ($this->activeSheetIndex >= $iSheetIndex) {
  485. ++$this->activeSheetIndex;
  486. }
  487. }
  488. if ($pSheet->getParent() === null) {
  489. $pSheet->rebindParent($this);
  490. }
  491. return $pSheet;
  492. }
  493. /**
  494. * Remove sheet by index
  495. *
  496. * @param int $pIndex Active sheet index
  497. * @throws PHPExcel_Exception
  498. */
  499. public function removeSheetByIndex($pIndex = 0)
  500. {
  501. $numSheets = count($this->workSheetCollection);
  502. if ($pIndex > $numSheets - 1) {
  503. throw new PHPExcel_Exception(
  504. "You tried to remove a sheet by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
  505. );
  506. } else {
  507. array_splice($this->workSheetCollection, $pIndex, 1);
  508. }
  509. // Adjust active sheet index if necessary
  510. if (($this->activeSheetIndex >= $pIndex) &&
  511. ($pIndex > count($this->workSheetCollection) - 1)) {
  512. --$this->activeSheetIndex;
  513. }
  514. }
  515. /**
  516. * Get sheet by index
  517. *
  518. * @param int $pIndex Sheet index
  519. * @return PHPExcel_Worksheet
  520. * @throws PHPExcel_Exception
  521. */
  522. public function getSheet($pIndex = 0)
  523. {
  524. if (!isset($this->workSheetCollection[$pIndex])) {
  525. $numSheets = $this->getSheetCount();
  526. throw new PHPExcel_Exception(
  527. "Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}."
  528. );
  529. }
  530. return $this->workSheetCollection[$pIndex];
  531. }
  532. /**
  533. * Get all sheets
  534. *
  535. * @return PHPExcel_Worksheet[]
  536. */
  537. public function getAllSheets()
  538. {
  539. return $this->workSheetCollection;
  540. }
  541. /**
  542. * Get sheet by name
  543. *
  544. * @param string $pName Sheet name
  545. * @return PHPExcel_Worksheet
  546. */
  547. public function getSheetByName($pName = '')
  548. {
  549. $worksheetCount = count($this->workSheetCollection);
  550. for ($i = 0; $i < $worksheetCount; ++$i) {
  551. if ($this->workSheetCollection[$i]->getTitle() === $pName) {
  552. return $this->workSheetCollection[$i];
  553. }
  554. }
  555. return null;
  556. }
  557. /**
  558. * Get index for sheet
  559. *
  560. * @param PHPExcel_Worksheet $pSheet
  561. * @return int Sheet index
  562. * @throws PHPExcel_Exception
  563. */
  564. public function getIndex(PHPExcel_Worksheet $pSheet)
  565. {
  566. foreach ($this->workSheetCollection as $key => $value) {
  567. if ($value->getHashCode() == $pSheet->getHashCode()) {
  568. return $key;
  569. }
  570. }
  571. throw new PHPExcel_Exception("Sheet does not exist.");
  572. }
  573. /**
  574. * Set index for sheet by sheet name.
  575. *
  576. * @param string $sheetName Sheet name to modify index for
  577. * @param int $newIndex New index for the sheet
  578. * @return int New sheet index
  579. * @throws PHPExcel_Exception
  580. */
  581. public function setIndexByName($sheetName, $newIndex)
  582. {
  583. $oldIndex = $this->getIndex($this->getSheetByName($sheetName));
  584. $pSheet = array_splice(
  585. $this->workSheetCollection,
  586. $oldIndex,
  587. 1
  588. );
  589. array_splice(
  590. $this->workSheetCollection,
  591. $newIndex,
  592. 0,
  593. $pSheet
  594. );
  595. return $newIndex;
  596. }
  597. /**
  598. * Get sheet count
  599. *
  600. * @return int
  601. */
  602. public function getSheetCount()
  603. {
  604. return count($this->workSheetCollection);
  605. }
  606. /**
  607. * Get active sheet index
  608. *
  609. * @return int Active sheet index
  610. */
  611. public function getActiveSheetIndex()
  612. {
  613. return $this->activeSheetIndex;
  614. }
  615. /**
  616. * Set active sheet index
  617. *
  618. * @param int $pIndex Active sheet index
  619. * @throws PHPExcel_Exception
  620. * @return PHPExcel_Worksheet
  621. */
  622. public function setActiveSheetIndex($pIndex = 0)
  623. {
  624. $numSheets = count($this->workSheetCollection);
  625. if ($pIndex > $numSheets - 1) {
  626. throw new PHPExcel_Exception(
  627. "You tried to set a sheet active by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
  628. );
  629. } else {
  630. $this->activeSheetIndex = $pIndex;
  631. }
  632. return $this->getActiveSheet();
  633. }
  634. /**
  635. * Set active sheet index by name
  636. *
  637. * @param string $pValue Sheet title
  638. * @return PHPExcel_Worksheet
  639. * @throws PHPExcel_Exception
  640. */
  641. public function setActiveSheetIndexByName($pValue = '')
  642. {
  643. if (($worksheet = $this->getSheetByName($pValue)) instanceof PHPExcel_Worksheet) {
  644. $this->setActiveSheetIndex($this->getIndex($worksheet));
  645. return $worksheet;
  646. }
  647. throw new PHPExcel_Exception('Workbook does not contain sheet:' . $pValue);
  648. }
  649. /**
  650. * Get sheet names
  651. *
  652. * @return string[]
  653. */
  654. public function getSheetNames()
  655. {
  656. $returnValue = array();
  657. $worksheetCount = $this->getSheetCount();
  658. for ($i = 0; $i < $worksheetCount; ++$i) {
  659. $returnValue[] = $this->getSheet($i)->getTitle();
  660. }
  661. return $returnValue;
  662. }
  663. /**
  664. * Add external sheet
  665. *
  666. * @param PHPExcel_Worksheet $pSheet External sheet to add
  667. * @param int|null $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  668. * @throws PHPExcel_Exception
  669. * @return PHPExcel_Worksheet
  670. */
  671. public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null)
  672. {
  673. if ($this->sheetNameExists($pSheet->getTitle())) {
  674. throw new PHPExcel_Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
  675. }
  676. // count how many cellXfs there are in this workbook currently, we will need this below
  677. $countCellXfs = count($this->cellXfCollection);
  678. // copy all the shared cellXfs from the external workbook and append them to the current
  679. foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) {
  680. $this->addCellXf(clone $cellXf);
  681. }
  682. // move sheet to this workbook
  683. $pSheet->rebindParent($this);
  684. // update the cellXfs
  685. foreach ($pSheet->getCellCollection(false) as $cellID) {
  686. $cell = $pSheet->getCell($cellID);
  687. $cell->setXfIndex($cell->getXfIndex() + $countCellXfs);
  688. }
  689. return $this->addSheet($pSheet, $iSheetIndex);
  690. }
  691. /**
  692. * Get named ranges
  693. *
  694. * @return PHPExcel_NamedRange[]
  695. */
  696. public function getNamedRanges()
  697. {
  698. return $this->namedRanges;
  699. }
  700. /**
  701. * Add named range
  702. *
  703. * @param PHPExcel_NamedRange $namedRange
  704. * @return boolean
  705. */
  706. public function addNamedRange(PHPExcel_NamedRange $namedRange)
  707. {
  708. if ($namedRange->getScope() == null) {
  709. // global scope
  710. $this->namedRanges[$namedRange->getName()] = $namedRange;
  711. } else {
  712. // local scope
  713. $this->namedRanges[$namedRange->getScope()->getTitle().'!'.$namedRange->getName()] = $namedRange;
  714. }
  715. return true;
  716. }
  717. /**
  718. * Get named range
  719. *
  720. * @param string $namedRange
  721. * @param PHPExcel_Worksheet|null $pSheet Scope. Use null for global scope
  722. * @return PHPExcel_NamedRange|null
  723. */
  724. public function getNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null)
  725. {
  726. $returnValue = null;
  727. if ($namedRange != '' && ($namedRange !== null)) {
  728. // first look for global defined name
  729. if (isset($this->namedRanges[$namedRange])) {
  730. $returnValue = $this->namedRanges[$namedRange];
  731. }
  732. // then look for local defined name (has priority over global defined name if both names exist)
  733. if (($pSheet !== null) && isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
  734. $returnValue = $this->namedRanges[$pSheet->getTitle() . '!' . $namedRange];
  735. }
  736. }
  737. return $returnValue;
  738. }
  739. /**
  740. * Remove named range
  741. *
  742. * @param string $namedRange
  743. * @param PHPExcel_Worksheet|null $pSheet Scope: use null for global scope.
  744. * @return PHPExcel
  745. */
  746. public function removeNamedRange($namedRange, PHPExcel_Worksheet $pSheet = null)
  747. {
  748. if ($pSheet === null) {
  749. if (isset($this->namedRanges[$namedRange])) {
  750. unset($this->namedRanges[$namedRange]);
  751. }
  752. } else {
  753. if (isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
  754. unset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange]);
  755. }
  756. }
  757. return $this;
  758. }
  759. /**
  760. * Get worksheet iterator
  761. *
  762. * @return PHPExcel_WorksheetIterator
  763. */
  764. public function getWorksheetIterator()
  765. {
  766. return new PHPExcel_WorksheetIterator($this);
  767. }
  768. /**
  769. * Copy workbook (!= clone!)
  770. *
  771. * @return PHPExcel
  772. */
  773. public function copy()
  774. {
  775. $copied = clone $this;
  776. $worksheetCount = count($this->workSheetCollection);
  777. for ($i = 0; $i < $worksheetCount; ++$i) {
  778. $this->workSheetCollection[$i] = $this->workSheetCollection[$i]->copy();
  779. $this->workSheetCollection[$i]->rebindParent($this);
  780. }
  781. return $copied;
  782. }
  783. /**
  784. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  785. */
  786. public function __clone()
  787. {
  788. foreach ($this as $key => $val) {
  789. if (is_object($val) || (is_array($val))) {
  790. $this->{$key} = unserialize(serialize($val));
  791. }
  792. }
  793. }
  794. /**
  795. * Get the workbook collection of cellXfs
  796. *
  797. * @return PHPExcel_Style[]
  798. */
  799. public function getCellXfCollection()
  800. {
  801. return $this->cellXfCollection;
  802. }
  803. /**
  804. * Get cellXf by index
  805. *
  806. * @param int $pIndex
  807. * @return PHPExcel_Style
  808. */
  809. public function getCellXfByIndex($pIndex = 0)
  810. {
  811. return $this->cellXfCollection[$pIndex];
  812. }
  813. /**
  814. * Get cellXf by hash code
  815. *
  816. * @param string $pValue
  817. * @return PHPExcel_Style|boolean False if no match found
  818. */
  819. public function getCellXfByHashCode($pValue = '')
  820. {
  821. foreach ($this->cellXfCollection as $cellXf) {
  822. if ($cellXf->getHashCode() == $pValue) {
  823. return $cellXf;
  824. }
  825. }
  826. return false;
  827. }
  828. /**
  829. * Check if style exists in style collection
  830. *
  831. * @param PHPExcel_Style $pCellStyle
  832. * @return boolean
  833. */
  834. public function cellXfExists($pCellStyle = null)
  835. {
  836. return in_array($pCellStyle, $this->cellXfCollection, true);
  837. }
  838. /**
  839. * Get default style
  840. *
  841. * @return PHPExcel_Style
  842. * @throws PHPExcel_Exception
  843. */
  844. public function getDefaultStyle()
  845. {
  846. if (isset($this->cellXfCollection[0])) {
  847. return $this->cellXfCollection[0];
  848. }
  849. throw new PHPExcel_Exception('No default style found for this workbook');
  850. }
  851. /**
  852. * Add a cellXf to the workbook
  853. *
  854. * @param PHPExcel_Style $style
  855. */
  856. public function addCellXf(PHPExcel_Style $style)
  857. {
  858. $this->cellXfCollection[] = $style;
  859. $style->setIndex(count($this->cellXfCollection) - 1);
  860. }
  861. /**
  862. * Remove cellXf by index. It is ensured that all cells get their xf index updated.
  863. *
  864. * @param integer $pIndex Index to cellXf
  865. * @throws PHPExcel_Exception
  866. */
  867. public function removeCellXfByIndex($pIndex = 0)
  868. {
  869. if ($pIndex > count($this->cellXfCollection) - 1) {
  870. throw new PHPExcel_Exception("CellXf index is out of bounds.");
  871. } else {
  872. // first remove the cellXf
  873. array_splice($this->cellXfCollection, $pIndex, 1);
  874. // then update cellXf indexes for cells
  875. foreach ($this->workSheetCollection as $worksheet) {
  876. foreach ($worksheet->getCellCollection(false) as $cellID) {
  877. $cell = $worksheet->getCell($cellID);
  878. $xfIndex = $cell->getXfIndex();
  879. if ($xfIndex > $pIndex) {
  880. // decrease xf index by 1
  881. $cell->setXfIndex($xfIndex - 1);
  882. } elseif ($xfIndex == $pIndex) {
  883. // set to default xf index 0
  884. $cell->setXfIndex(0);
  885. }
  886. }
  887. }
  888. }
  889. }
  890. /**
  891. * Get the cellXf supervisor
  892. *
  893. * @return PHPExcel_Style
  894. */
  895. public function getCellXfSupervisor()
  896. {
  897. return $this->cellXfSupervisor;
  898. }
  899. /**
  900. * Get the workbook collection of cellStyleXfs
  901. *
  902. * @return PHPExcel_Style[]
  903. */
  904. public function getCellStyleXfCollection()
  905. {
  906. return $this->cellStyleXfCollection;
  907. }
  908. /**
  909. * Get cellStyleXf by index
  910. *
  911. * @param integer $pIndex Index to cellXf
  912. * @return PHPExcel_Style
  913. */
  914. public function getCellStyleXfByIndex($pIndex = 0)
  915. {
  916. return $this->cellStyleXfCollection[$pIndex];
  917. }
  918. /**
  919. * Get cellStyleXf by hash code
  920. *
  921. * @param string $pValue
  922. * @return PHPExcel_Style|boolean False if no match found
  923. */
  924. public function getCellStyleXfByHashCode($pValue = '')
  925. {
  926. foreach ($this->cellStyleXfCollection as $cellStyleXf) {
  927. if ($cellStyleXf->getHashCode() == $pValue) {
  928. return $cellStyleXf;
  929. }
  930. }
  931. return false;
  932. }
  933. /**
  934. * Add a cellStyleXf to the workbook
  935. *
  936. * @param PHPExcel_Style $pStyle
  937. */
  938. public function addCellStyleXf(PHPExcel_Style $pStyle)
  939. {
  940. $this->cellStyleXfCollection[] = $pStyle;
  941. $pStyle->setIndex(count($this->cellStyleXfCollection) - 1);
  942. }
  943. /**
  944. * Remove cellStyleXf by index
  945. *
  946. * @param integer $pIndex Index to cellXf
  947. * @throws PHPExcel_Exception
  948. */
  949. public function removeCellStyleXfByIndex($pIndex = 0)
  950. {
  951. if ($pIndex > count($this->cellStyleXfCollection) - 1) {
  952. throw new PHPExcel_Exception("CellStyleXf index is out of bounds.");
  953. } else {
  954. array_splice($this->cellStyleXfCollection, $pIndex, 1);
  955. }
  956. }
  957. /**
  958. * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells
  959. * and columns in the workbook
  960. */
  961. public function garbageCollect()
  962. {
  963. // how many references are there to each cellXf ?
  964. $countReferencesCellXf = array();
  965. foreach ($this->cellXfCollection as $index => $cellXf) {
  966. $countReferencesCellXf[$index] = 0;
  967. }
  968. foreach ($this->getWorksheetIterator() as $sheet) {
  969. // from cells
  970. foreach ($sheet->getCellCollection(false) as $cellID) {
  971. $cell = $sheet->getCell($cellID);
  972. ++$countReferencesCellXf[$cell->getXfIndex()];
  973. }
  974. // from row dimensions
  975. foreach ($sheet->getRowDimensions() as $rowDimension) {
  976. if ($rowDimension->getXfIndex() !== null) {
  977. ++$countReferencesCellXf[$rowDimension->getXfIndex()];
  978. }
  979. }
  980. // from column dimensions
  981. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  982. ++$countReferencesCellXf[$columnDimension->getXfIndex()];
  983. }
  984. }
  985. // remove cellXfs without references and create mapping so we can update xfIndex
  986. // for all cells and columns
  987. $countNeededCellXfs = 0;
  988. $map = array();
  989. foreach ($this->cellXfCollection as $index => $cellXf) {
  990. if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf
  991. ++$countNeededCellXfs;
  992. } else {
  993. unset($this->cellXfCollection[$index]);
  994. }
  995. $map[$index] = $countNeededCellXfs - 1;
  996. }
  997. $this->cellXfCollection = array_values($this->cellXfCollection);
  998. // update the index for all cellXfs
  999. foreach ($this->cellXfCollection as $i => $cellXf) {
  1000. $cellXf->setIndex($i);
  1001. }
  1002. // make sure there is always at least one cellXf (there should be)
  1003. if (empty($this->cellXfCollection)) {
  1004. $this->cellXfCollection[] = new PHPExcel_Style();
  1005. }
  1006. // update the xfIndex for all cells, row dimensions, column dimensions
  1007. foreach ($this->getWorksheetIterator() as $sheet) {
  1008. // for all cells
  1009. foreach ($sheet->getCellCollection(false) as $cellID) {
  1010. $cell = $sheet->getCell($cellID);
  1011. $cell->setXfIndex($map[$cell->getXfIndex()]);
  1012. }
  1013. // for all row dimensions
  1014. foreach ($sheet->getRowDimensions() as $rowDimension) {
  1015. if ($rowDimension->getXfIndex() !== null) {
  1016. $rowDimension->setXfIndex($map[$rowDimension->getXfIndex()]);
  1017. }
  1018. }
  1019. // for all column dimensions
  1020. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  1021. $columnDimension->setXfIndex($map[$columnDimension->getXfIndex()]);
  1022. }
  1023. // also do garbage collection for all the sheets
  1024. $sheet->garbageCollect();
  1025. }
  1026. }
  1027. /**
  1028. * Return the unique ID value assigned to this spreadsheet workbook
  1029. *
  1030. * @return string
  1031. */
  1032. public function getID()
  1033. {
  1034. return $this->uniqueID;
  1035. }
  1036. }