You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Spreadsheet.php 37 KiB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Style\Style;
  5. use PhpOffice\PhpSpreadsheet\Worksheet\Iterator;
  6. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  7. class Spreadsheet
  8. {
  9. // Allowable values for workbook window visilbity
  10. const VISIBILITY_VISIBLE = 'visible';
  11. const VISIBILITY_HIDDEN = 'hidden';
  12. const VISIBILITY_VERY_HIDDEN = 'veryHidden';
  13. private static $workbookViewVisibilityValues = [
  14. self::VISIBILITY_VISIBLE,
  15. self::VISIBILITY_HIDDEN,
  16. self::VISIBILITY_VERY_HIDDEN,
  17. ];
  18. /**
  19. * Unique ID.
  20. *
  21. * @var string
  22. */
  23. private $uniqueID;
  24. /**
  25. * Document properties.
  26. *
  27. * @var Document\Properties
  28. */
  29. private $properties;
  30. /**
  31. * Document security.
  32. *
  33. * @var Document\Security
  34. */
  35. private $security;
  36. /**
  37. * Collection of Worksheet objects.
  38. *
  39. * @var Worksheet[]
  40. */
  41. private $workSheetCollection = [];
  42. /**
  43. * Calculation Engine.
  44. *
  45. * @var Calculation
  46. */
  47. private $calculationEngine;
  48. /**
  49. * Active sheet index.
  50. *
  51. * @var int
  52. */
  53. private $activeSheetIndex = 0;
  54. /**
  55. * Named ranges.
  56. *
  57. * @var NamedRange[]
  58. */
  59. private $namedRanges = [];
  60. /**
  61. * CellXf supervisor.
  62. *
  63. * @var Style
  64. */
  65. private $cellXfSupervisor;
  66. /**
  67. * CellXf collection.
  68. *
  69. * @var Style[]
  70. */
  71. private $cellXfCollection = [];
  72. /**
  73. * CellStyleXf collection.
  74. *
  75. * @var Style[]
  76. */
  77. private $cellStyleXfCollection = [];
  78. /**
  79. * hasMacros : this workbook have macros ?
  80. *
  81. * @var bool
  82. */
  83. private $hasMacros = false;
  84. /**
  85. * macrosCode : all macros code as binary data (the vbaProject.bin file, this include form, code, etc.), null if no macro.
  86. *
  87. * @var string
  88. */
  89. private $macrosCode;
  90. /**
  91. * macrosCertificate : if macros are signed, contains binary data vbaProjectSignature.bin file, null if not signed.
  92. *
  93. * @var string
  94. */
  95. private $macrosCertificate;
  96. /**
  97. * ribbonXMLData : null if workbook is'nt Excel 2007 or not contain a customized UI.
  98. *
  99. * @var null|string
  100. */
  101. private $ribbonXMLData;
  102. /**
  103. * ribbonBinObjects : null if workbook is'nt Excel 2007 or not contain embedded objects (picture(s)) for Ribbon Elements
  104. * ignored if $ribbonXMLData is null.
  105. *
  106. * @var null|array
  107. */
  108. private $ribbonBinObjects;
  109. /**
  110. * List of unparsed loaded data for export to same format with better compatibility.
  111. * It has to be minimized when the library start to support currently unparsed data.
  112. *
  113. * @var array
  114. */
  115. private $unparsedLoadedData = [];
  116. /**
  117. * Controls visibility of the horizonal scroll bar in the application.
  118. *
  119. * @var bool
  120. */
  121. private $showHorizontalScroll = true;
  122. /**
  123. * Controls visibility of the horizonal scroll bar in the application.
  124. *
  125. * @var bool
  126. */
  127. private $showVerticalScroll = true;
  128. /**
  129. * Controls visibility of the sheet tabs in the application.
  130. *
  131. * @var bool
  132. */
  133. private $showSheetTabs = true;
  134. /**
  135. * Specifies a boolean value that indicates whether the workbook window
  136. * is minimized.
  137. *
  138. * @var bool
  139. */
  140. private $minimized = false;
  141. /**
  142. * Specifies a boolean value that indicates whether to group dates
  143. * when presenting the user with filtering optiomd in the user
  144. * interface.
  145. *
  146. * @var bool
  147. */
  148. private $autoFilterDateGrouping = true;
  149. /**
  150. * Specifies the index to the first sheet in the book view.
  151. *
  152. * @var int
  153. */
  154. private $firstSheetIndex = 0;
  155. /**
  156. * Specifies the visible status of the workbook.
  157. *
  158. * @var string
  159. */
  160. private $visibility = self::VISIBILITY_VISIBLE;
  161. /**
  162. * Specifies the ratio between the workbook tabs bar and the horizontal
  163. * scroll bar. TabRatio is assumed to be out of 1000 of the horizontal
  164. * window width.
  165. *
  166. * @var int
  167. */
  168. private $tabRatio = 600;
  169. /**
  170. * The workbook has macros ?
  171. *
  172. * @return bool
  173. */
  174. public function hasMacros()
  175. {
  176. return $this->hasMacros;
  177. }
  178. /**
  179. * Define if a workbook has macros.
  180. *
  181. * @param bool $hasMacros true|false
  182. */
  183. public function setHasMacros($hasMacros)
  184. {
  185. $this->hasMacros = (bool) $hasMacros;
  186. }
  187. /**
  188. * Set the macros code.
  189. *
  190. * @param string $macroCode string|null
  191. */
  192. public function setMacrosCode($macroCode)
  193. {
  194. $this->macrosCode = $macroCode;
  195. $this->setHasMacros($macroCode !== null);
  196. }
  197. /**
  198. * Return the macros code.
  199. *
  200. * @return null|string
  201. */
  202. public function getMacrosCode()
  203. {
  204. return $this->macrosCode;
  205. }
  206. /**
  207. * Set the macros certificate.
  208. *
  209. * @param null|string $certificate
  210. */
  211. public function setMacrosCertificate($certificate)
  212. {
  213. $this->macrosCertificate = $certificate;
  214. }
  215. /**
  216. * Is the project signed ?
  217. *
  218. * @return bool true|false
  219. */
  220. public function hasMacrosCertificate()
  221. {
  222. return $this->macrosCertificate !== null;
  223. }
  224. /**
  225. * Return the macros certificate.
  226. *
  227. * @return null|string
  228. */
  229. public function getMacrosCertificate()
  230. {
  231. return $this->macrosCertificate;
  232. }
  233. /**
  234. * Remove all macros, certificate from spreadsheet.
  235. */
  236. public function discardMacros()
  237. {
  238. $this->hasMacros = false;
  239. $this->macrosCode = null;
  240. $this->macrosCertificate = null;
  241. }
  242. /**
  243. * set ribbon XML data.
  244. *
  245. * @param null|mixed $target
  246. * @param null|mixed $xmlData
  247. */
  248. public function setRibbonXMLData($target, $xmlData)
  249. {
  250. if ($target !== null && $xmlData !== null) {
  251. $this->ribbonXMLData = ['target' => $target, 'data' => $xmlData];
  252. } else {
  253. $this->ribbonXMLData = null;
  254. }
  255. }
  256. /**
  257. * retrieve ribbon XML Data.
  258. *
  259. * return string|null|array
  260. *
  261. * @param string $what
  262. *
  263. * @return string
  264. */
  265. public function getRibbonXMLData($what = 'all') //we need some constants here...
  266. {
  267. $returnData = null;
  268. $what = strtolower($what);
  269. switch ($what) {
  270. case 'all':
  271. $returnData = $this->ribbonXMLData;
  272. break;
  273. case 'target':
  274. case 'data':
  275. if (is_array($this->ribbonXMLData) && isset($this->ribbonXMLData[$what])) {
  276. $returnData = $this->ribbonXMLData[$what];
  277. }
  278. break;
  279. }
  280. return $returnData;
  281. }
  282. /**
  283. * store binaries ribbon objects (pictures).
  284. *
  285. * @param null|mixed $BinObjectsNames
  286. * @param null|mixed $BinObjectsData
  287. */
  288. public function setRibbonBinObjects($BinObjectsNames, $BinObjectsData)
  289. {
  290. if ($BinObjectsNames !== null && $BinObjectsData !== null) {
  291. $this->ribbonBinObjects = ['names' => $BinObjectsNames, 'data' => $BinObjectsData];
  292. } else {
  293. $this->ribbonBinObjects = null;
  294. }
  295. }
  296. /**
  297. * List of unparsed loaded data for export to same format with better compatibility.
  298. * It has to be minimized when the library start to support currently unparsed data.
  299. *
  300. * @internal
  301. *
  302. * @return array
  303. */
  304. public function getUnparsedLoadedData()
  305. {
  306. return $this->unparsedLoadedData;
  307. }
  308. /**
  309. * List of unparsed loaded data for export to same format with better compatibility.
  310. * It has to be minimized when the library start to support currently unparsed data.
  311. *
  312. * @internal
  313. *
  314. * @param array $unparsedLoadedData
  315. */
  316. public function setUnparsedLoadedData(array $unparsedLoadedData)
  317. {
  318. $this->unparsedLoadedData = $unparsedLoadedData;
  319. }
  320. /**
  321. * return the extension of a filename. Internal use for a array_map callback (php<5.3 don't like lambda function).
  322. *
  323. * @param mixed $path
  324. *
  325. * @return string
  326. */
  327. private function getExtensionOnly($path)
  328. {
  329. return pathinfo($path, PATHINFO_EXTENSION);
  330. }
  331. /**
  332. * retrieve Binaries Ribbon Objects.
  333. *
  334. * @param string $what
  335. *
  336. * @return null|array
  337. */
  338. public function getRibbonBinObjects($what = 'all')
  339. {
  340. $ReturnData = null;
  341. $what = strtolower($what);
  342. switch ($what) {
  343. case 'all':
  344. return $this->ribbonBinObjects;
  345. break;
  346. case 'names':
  347. case 'data':
  348. if (is_array($this->ribbonBinObjects) && isset($this->ribbonBinObjects[$what])) {
  349. $ReturnData = $this->ribbonBinObjects[$what];
  350. }
  351. break;
  352. case 'types':
  353. if (is_array($this->ribbonBinObjects) &&
  354. isset($this->ribbonBinObjects['data']) && is_array($this->ribbonBinObjects['data'])) {
  355. $tmpTypes = array_keys($this->ribbonBinObjects['data']);
  356. $ReturnData = array_unique(array_map([$this, 'getExtensionOnly'], $tmpTypes));
  357. } else {
  358. $ReturnData = []; // the caller want an array... not null if empty
  359. }
  360. break;
  361. }
  362. return $ReturnData;
  363. }
  364. /**
  365. * This workbook have a custom UI ?
  366. *
  367. * @return bool
  368. */
  369. public function hasRibbon()
  370. {
  371. return $this->ribbonXMLData !== null;
  372. }
  373. /**
  374. * This workbook have additionnal object for the ribbon ?
  375. *
  376. * @return bool
  377. */
  378. public function hasRibbonBinObjects()
  379. {
  380. return $this->ribbonBinObjects !== null;
  381. }
  382. /**
  383. * Check if a sheet with a specified code name already exists.
  384. *
  385. * @param string $pSheetCodeName Name of the worksheet to check
  386. *
  387. * @return bool
  388. */
  389. public function sheetCodeNameExists($pSheetCodeName)
  390. {
  391. return $this->getSheetByCodeName($pSheetCodeName) !== null;
  392. }
  393. /**
  394. * Get sheet by code name. Warning : sheet don't have always a code name !
  395. *
  396. * @param string $pName Sheet name
  397. *
  398. * @return Worksheet
  399. */
  400. public function getSheetByCodeName($pName)
  401. {
  402. $worksheetCount = count($this->workSheetCollection);
  403. for ($i = 0; $i < $worksheetCount; ++$i) {
  404. if ($this->workSheetCollection[$i]->getCodeName() == $pName) {
  405. return $this->workSheetCollection[$i];
  406. }
  407. }
  408. return null;
  409. }
  410. /**
  411. * Create a new PhpSpreadsheet with one Worksheet.
  412. */
  413. public function __construct()
  414. {
  415. $this->uniqueID = uniqid('', true);
  416. $this->calculationEngine = new Calculation($this);
  417. // Initialise worksheet collection and add one worksheet
  418. $this->workSheetCollection = [];
  419. $this->workSheetCollection[] = new Worksheet($this);
  420. $this->activeSheetIndex = 0;
  421. // Create document properties
  422. $this->properties = new Document\Properties();
  423. // Create document security
  424. $this->security = new Document\Security();
  425. // Set named ranges
  426. $this->namedRanges = [];
  427. // Create the cellXf supervisor
  428. $this->cellXfSupervisor = new Style(true);
  429. $this->cellXfSupervisor->bindParent($this);
  430. // Create the default style
  431. $this->addCellXf(new Style());
  432. $this->addCellStyleXf(new Style());
  433. }
  434. /**
  435. * Code to execute when this worksheet is unset().
  436. */
  437. public function __destruct()
  438. {
  439. $this->calculationEngine = null;
  440. $this->disconnectWorksheets();
  441. }
  442. /**
  443. * Disconnect all worksheets from this PhpSpreadsheet workbook object,
  444. * typically so that the PhpSpreadsheet object can be unset.
  445. */
  446. public function disconnectWorksheets()
  447. {
  448. $worksheet = null;
  449. foreach ($this->workSheetCollection as $k => &$worksheet) {
  450. $worksheet->disconnectCells();
  451. $this->workSheetCollection[$k] = null;
  452. }
  453. unset($worksheet);
  454. $this->workSheetCollection = [];
  455. }
  456. /**
  457. * Return the calculation engine for this worksheet.
  458. *
  459. * @return Calculation
  460. */
  461. public function getCalculationEngine()
  462. {
  463. return $this->calculationEngine;
  464. }
  465. /**
  466. * Get properties.
  467. *
  468. * @return Document\Properties
  469. */
  470. public function getProperties()
  471. {
  472. return $this->properties;
  473. }
  474. /**
  475. * Set properties.
  476. *
  477. * @param Document\Properties $pValue
  478. */
  479. public function setProperties(Document\Properties $pValue)
  480. {
  481. $this->properties = $pValue;
  482. }
  483. /**
  484. * Get security.
  485. *
  486. * @return Document\Security
  487. */
  488. public function getSecurity()
  489. {
  490. return $this->security;
  491. }
  492. /**
  493. * Set security.
  494. *
  495. * @param Document\Security $pValue
  496. */
  497. public function setSecurity(Document\Security $pValue)
  498. {
  499. $this->security = $pValue;
  500. }
  501. /**
  502. * Get active sheet.
  503. *
  504. * @throws Exception
  505. *
  506. * @return Worksheet
  507. */
  508. public function getActiveSheet()
  509. {
  510. return $this->getSheet($this->activeSheetIndex);
  511. }
  512. /**
  513. * Create sheet and add it to this workbook.
  514. *
  515. * @param null|int $sheetIndex Index where sheet should go (0,1,..., or null for last)
  516. *
  517. * @throws Exception
  518. *
  519. * @return Worksheet
  520. */
  521. public function createSheet($sheetIndex = null)
  522. {
  523. $newSheet = new Worksheet($this);
  524. $this->addSheet($newSheet, $sheetIndex);
  525. return $newSheet;
  526. }
  527. /**
  528. * Check if a sheet with a specified name already exists.
  529. *
  530. * @param string $pSheetName Name of the worksheet to check
  531. *
  532. * @return bool
  533. */
  534. public function sheetNameExists($pSheetName)
  535. {
  536. return $this->getSheetByName($pSheetName) !== null;
  537. }
  538. /**
  539. * Add sheet.
  540. *
  541. * @param Worksheet $pSheet
  542. * @param null|int $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  543. *
  544. * @throws Exception
  545. *
  546. * @return Worksheet
  547. */
  548. public function addSheet(Worksheet $pSheet, $iSheetIndex = null)
  549. {
  550. if ($this->sheetNameExists($pSheet->getTitle())) {
  551. throw new Exception(
  552. "Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first."
  553. );
  554. }
  555. if ($iSheetIndex === null) {
  556. if ($this->activeSheetIndex < 0) {
  557. $this->activeSheetIndex = 0;
  558. }
  559. $this->workSheetCollection[] = $pSheet;
  560. } else {
  561. // Insert the sheet at the requested index
  562. array_splice(
  563. $this->workSheetCollection,
  564. $iSheetIndex,
  565. 0,
  566. [$pSheet]
  567. );
  568. // Adjust active sheet index if necessary
  569. if ($this->activeSheetIndex >= $iSheetIndex) {
  570. ++$this->activeSheetIndex;
  571. }
  572. }
  573. if ($pSheet->getParent() === null) {
  574. $pSheet->rebindParent($this);
  575. }
  576. return $pSheet;
  577. }
  578. /**
  579. * Remove sheet by index.
  580. *
  581. * @param int $pIndex Active sheet index
  582. *
  583. * @throws Exception
  584. */
  585. public function removeSheetByIndex($pIndex)
  586. {
  587. $numSheets = count($this->workSheetCollection);
  588. if ($pIndex > $numSheets - 1) {
  589. throw new Exception(
  590. "You tried to remove a sheet by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
  591. );
  592. }
  593. array_splice($this->workSheetCollection, $pIndex, 1);
  594. // Adjust active sheet index if necessary
  595. if (($this->activeSheetIndex >= $pIndex) &&
  596. ($pIndex > count($this->workSheetCollection) - 1)) {
  597. --$this->activeSheetIndex;
  598. }
  599. }
  600. /**
  601. * Get sheet by index.
  602. *
  603. * @param int $pIndex Sheet index
  604. *
  605. * @throws Exception
  606. *
  607. * @return Worksheet
  608. */
  609. public function getSheet($pIndex)
  610. {
  611. if (!isset($this->workSheetCollection[$pIndex])) {
  612. $numSheets = $this->getSheetCount();
  613. throw new Exception(
  614. "Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}."
  615. );
  616. }
  617. return $this->workSheetCollection[$pIndex];
  618. }
  619. /**
  620. * Get all sheets.
  621. *
  622. * @return Worksheet[]
  623. */
  624. public function getAllSheets()
  625. {
  626. return $this->workSheetCollection;
  627. }
  628. /**
  629. * Get sheet by name.
  630. *
  631. * @param string $pName Sheet name
  632. *
  633. * @return null|Worksheet
  634. */
  635. public function getSheetByName($pName)
  636. {
  637. $worksheetCount = count($this->workSheetCollection);
  638. for ($i = 0; $i < $worksheetCount; ++$i) {
  639. if ($this->workSheetCollection[$i]->getTitle() === trim($pName, "'")) {
  640. return $this->workSheetCollection[$i];
  641. }
  642. }
  643. return null;
  644. }
  645. /**
  646. * Get index for sheet.
  647. *
  648. * @param Worksheet $pSheet
  649. *
  650. * @throws Exception
  651. *
  652. * @return int index
  653. */
  654. public function getIndex(Worksheet $pSheet)
  655. {
  656. foreach ($this->workSheetCollection as $key => $value) {
  657. if ($value->getHashCode() == $pSheet->getHashCode()) {
  658. return $key;
  659. }
  660. }
  661. throw new Exception('Sheet does not exist.');
  662. }
  663. /**
  664. * Set index for sheet by sheet name.
  665. *
  666. * @param string $sheetName Sheet name to modify index for
  667. * @param int $newIndex New index for the sheet
  668. *
  669. * @throws Exception
  670. *
  671. * @return int New sheet index
  672. */
  673. public function setIndexByName($sheetName, $newIndex)
  674. {
  675. $oldIndex = $this->getIndex($this->getSheetByName($sheetName));
  676. $pSheet = array_splice(
  677. $this->workSheetCollection,
  678. $oldIndex,
  679. 1
  680. );
  681. array_splice(
  682. $this->workSheetCollection,
  683. $newIndex,
  684. 0,
  685. $pSheet
  686. );
  687. return $newIndex;
  688. }
  689. /**
  690. * Get sheet count.
  691. *
  692. * @return int
  693. */
  694. public function getSheetCount()
  695. {
  696. return count($this->workSheetCollection);
  697. }
  698. /**
  699. * Get active sheet index.
  700. *
  701. * @return int Active sheet index
  702. */
  703. public function getActiveSheetIndex()
  704. {
  705. return $this->activeSheetIndex;
  706. }
  707. /**
  708. * Set active sheet index.
  709. *
  710. * @param int $pIndex Active sheet index
  711. *
  712. * @throws Exception
  713. *
  714. * @return Worksheet
  715. */
  716. public function setActiveSheetIndex($pIndex)
  717. {
  718. $numSheets = count($this->workSheetCollection);
  719. if ($pIndex > $numSheets - 1) {
  720. throw new Exception(
  721. "You tried to set a sheet active by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
  722. );
  723. }
  724. $this->activeSheetIndex = $pIndex;
  725. return $this->getActiveSheet();
  726. }
  727. /**
  728. * Set active sheet index by name.
  729. *
  730. * @param string $pValue Sheet title
  731. *
  732. * @throws Exception
  733. *
  734. * @return Worksheet
  735. */
  736. public function setActiveSheetIndexByName($pValue)
  737. {
  738. if (($worksheet = $this->getSheetByName($pValue)) instanceof Worksheet) {
  739. $this->setActiveSheetIndex($this->getIndex($worksheet));
  740. return $worksheet;
  741. }
  742. throw new Exception('Workbook does not contain sheet:' . $pValue);
  743. }
  744. /**
  745. * Get sheet names.
  746. *
  747. * @return string[]
  748. */
  749. public function getSheetNames()
  750. {
  751. $returnValue = [];
  752. $worksheetCount = $this->getSheetCount();
  753. for ($i = 0; $i < $worksheetCount; ++$i) {
  754. $returnValue[] = $this->getSheet($i)->getTitle();
  755. }
  756. return $returnValue;
  757. }
  758. /**
  759. * Add external sheet.
  760. *
  761. * @param Worksheet $pSheet External sheet to add
  762. * @param null|int $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  763. *
  764. * @throws Exception
  765. *
  766. * @return Worksheet
  767. */
  768. public function addExternalSheet(Worksheet $pSheet, $iSheetIndex = null)
  769. {
  770. if ($this->sheetNameExists($pSheet->getTitle())) {
  771. throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
  772. }
  773. // count how many cellXfs there are in this workbook currently, we will need this below
  774. $countCellXfs = count($this->cellXfCollection);
  775. // copy all the shared cellXfs from the external workbook and append them to the current
  776. foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) {
  777. $this->addCellXf(clone $cellXf);
  778. }
  779. // move sheet to this workbook
  780. $pSheet->rebindParent($this);
  781. // update the cellXfs
  782. foreach ($pSheet->getCoordinates(false) as $coordinate) {
  783. $cell = $pSheet->getCell($coordinate);
  784. $cell->setXfIndex($cell->getXfIndex() + $countCellXfs);
  785. }
  786. return $this->addSheet($pSheet, $iSheetIndex);
  787. }
  788. /**
  789. * Get named ranges.
  790. *
  791. * @return NamedRange[]
  792. */
  793. public function getNamedRanges()
  794. {
  795. return $this->namedRanges;
  796. }
  797. /**
  798. * Add named range.
  799. *
  800. * @param NamedRange $namedRange
  801. *
  802. * @return bool
  803. */
  804. public function addNamedRange(NamedRange $namedRange)
  805. {
  806. if ($namedRange->getScope() == null) {
  807. // global scope
  808. $this->namedRanges[$namedRange->getName()] = $namedRange;
  809. } else {
  810. // local scope
  811. $this->namedRanges[$namedRange->getScope()->getTitle() . '!' . $namedRange->getName()] = $namedRange;
  812. }
  813. return true;
  814. }
  815. /**
  816. * Get named range.
  817. *
  818. * @param string $namedRange
  819. * @param null|Worksheet $pSheet Scope. Use null for global scope
  820. *
  821. * @return null|NamedRange
  822. */
  823. public function getNamedRange($namedRange, Worksheet $pSheet = null)
  824. {
  825. $returnValue = null;
  826. if ($namedRange != '' && ($namedRange !== null)) {
  827. // first look for global defined name
  828. if (isset($this->namedRanges[$namedRange])) {
  829. $returnValue = $this->namedRanges[$namedRange];
  830. }
  831. // then look for local defined name (has priority over global defined name if both names exist)
  832. if (($pSheet !== null) && isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
  833. $returnValue = $this->namedRanges[$pSheet->getTitle() . '!' . $namedRange];
  834. }
  835. }
  836. return $returnValue;
  837. }
  838. /**
  839. * Remove named range.
  840. *
  841. * @param string $namedRange
  842. * @param null|Worksheet $pSheet scope: use null for global scope
  843. *
  844. * @return $this
  845. */
  846. public function removeNamedRange($namedRange, Worksheet $pSheet = null)
  847. {
  848. if ($pSheet === null) {
  849. if (isset($this->namedRanges[$namedRange])) {
  850. unset($this->namedRanges[$namedRange]);
  851. }
  852. } else {
  853. if (isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) {
  854. unset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange]);
  855. }
  856. }
  857. return $this;
  858. }
  859. /**
  860. * Get worksheet iterator.
  861. *
  862. * @return Iterator
  863. */
  864. public function getWorksheetIterator()
  865. {
  866. return new Iterator($this);
  867. }
  868. /**
  869. * Copy workbook (!= clone!).
  870. *
  871. * @return Spreadsheet
  872. */
  873. public function copy()
  874. {
  875. $copied = clone $this;
  876. $worksheetCount = count($this->workSheetCollection);
  877. for ($i = 0; $i < $worksheetCount; ++$i) {
  878. $this->workSheetCollection[$i] = $this->workSheetCollection[$i]->copy();
  879. $this->workSheetCollection[$i]->rebindParent($this);
  880. }
  881. return $copied;
  882. }
  883. /**
  884. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  885. */
  886. public function __clone()
  887. {
  888. foreach ($this as $key => $val) {
  889. if (is_object($val) || (is_array($val))) {
  890. $this->{$key} = unserialize(serialize($val));
  891. }
  892. }
  893. }
  894. /**
  895. * Get the workbook collection of cellXfs.
  896. *
  897. * @return Style[]
  898. */
  899. public function getCellXfCollection()
  900. {
  901. return $this->cellXfCollection;
  902. }
  903. /**
  904. * Get cellXf by index.
  905. *
  906. * @param int $pIndex
  907. *
  908. * @return Style
  909. */
  910. public function getCellXfByIndex($pIndex)
  911. {
  912. return $this->cellXfCollection[$pIndex];
  913. }
  914. /**
  915. * Get cellXf by hash code.
  916. *
  917. * @param string $pValue
  918. *
  919. * @return false|Style
  920. */
  921. public function getCellXfByHashCode($pValue)
  922. {
  923. foreach ($this->cellXfCollection as $cellXf) {
  924. if ($cellXf->getHashCode() == $pValue) {
  925. return $cellXf;
  926. }
  927. }
  928. return false;
  929. }
  930. /**
  931. * Check if style exists in style collection.
  932. *
  933. * @param Style $pCellStyle
  934. *
  935. * @return bool
  936. */
  937. public function cellXfExists($pCellStyle)
  938. {
  939. return in_array($pCellStyle, $this->cellXfCollection, true);
  940. }
  941. /**
  942. * Get default style.
  943. *
  944. * @throws Exception
  945. *
  946. * @return Style
  947. */
  948. public function getDefaultStyle()
  949. {
  950. if (isset($this->cellXfCollection[0])) {
  951. return $this->cellXfCollection[0];
  952. }
  953. throw new Exception('No default style found for this workbook');
  954. }
  955. /**
  956. * Add a cellXf to the workbook.
  957. *
  958. * @param Style $style
  959. */
  960. public function addCellXf(Style $style)
  961. {
  962. $this->cellXfCollection[] = $style;
  963. $style->setIndex(count($this->cellXfCollection) - 1);
  964. }
  965. /**
  966. * Remove cellXf by index. It is ensured that all cells get their xf index updated.
  967. *
  968. * @param int $pIndex Index to cellXf
  969. *
  970. * @throws Exception
  971. */
  972. public function removeCellXfByIndex($pIndex)
  973. {
  974. if ($pIndex > count($this->cellXfCollection) - 1) {
  975. throw new Exception('CellXf index is out of bounds.');
  976. }
  977. // first remove the cellXf
  978. array_splice($this->cellXfCollection, $pIndex, 1);
  979. // then update cellXf indexes for cells
  980. foreach ($this->workSheetCollection as $worksheet) {
  981. foreach ($worksheet->getCoordinates(false) as $coordinate) {
  982. $cell = $worksheet->getCell($coordinate);
  983. $xfIndex = $cell->getXfIndex();
  984. if ($xfIndex > $pIndex) {
  985. // decrease xf index by 1
  986. $cell->setXfIndex($xfIndex - 1);
  987. } elseif ($xfIndex == $pIndex) {
  988. // set to default xf index 0
  989. $cell->setXfIndex(0);
  990. }
  991. }
  992. }
  993. }
  994. /**
  995. * Get the cellXf supervisor.
  996. *
  997. * @return Style
  998. */
  999. public function getCellXfSupervisor()
  1000. {
  1001. return $this->cellXfSupervisor;
  1002. }
  1003. /**
  1004. * Get the workbook collection of cellStyleXfs.
  1005. *
  1006. * @return Style[]
  1007. */
  1008. public function getCellStyleXfCollection()
  1009. {
  1010. return $this->cellStyleXfCollection;
  1011. }
  1012. /**
  1013. * Get cellStyleXf by index.
  1014. *
  1015. * @param int $pIndex Index to cellXf
  1016. *
  1017. * @return Style
  1018. */
  1019. public function getCellStyleXfByIndex($pIndex)
  1020. {
  1021. return $this->cellStyleXfCollection[$pIndex];
  1022. }
  1023. /**
  1024. * Get cellStyleXf by hash code.
  1025. *
  1026. * @param string $pValue
  1027. *
  1028. * @return false|Style
  1029. */
  1030. public function getCellStyleXfByHashCode($pValue)
  1031. {
  1032. foreach ($this->cellStyleXfCollection as $cellStyleXf) {
  1033. if ($cellStyleXf->getHashCode() == $pValue) {
  1034. return $cellStyleXf;
  1035. }
  1036. }
  1037. return false;
  1038. }
  1039. /**
  1040. * Add a cellStyleXf to the workbook.
  1041. *
  1042. * @param Style $pStyle
  1043. */
  1044. public function addCellStyleXf(Style $pStyle)
  1045. {
  1046. $this->cellStyleXfCollection[] = $pStyle;
  1047. $pStyle->setIndex(count($this->cellStyleXfCollection) - 1);
  1048. }
  1049. /**
  1050. * Remove cellStyleXf by index.
  1051. *
  1052. * @param int $pIndex Index to cellXf
  1053. *
  1054. * @throws Exception
  1055. */
  1056. public function removeCellStyleXfByIndex($pIndex)
  1057. {
  1058. if ($pIndex > count($this->cellStyleXfCollection) - 1) {
  1059. throw new Exception('CellStyleXf index is out of bounds.');
  1060. }
  1061. array_splice($this->cellStyleXfCollection, $pIndex, 1);
  1062. }
  1063. /**
  1064. * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells
  1065. * and columns in the workbook.
  1066. */
  1067. public function garbageCollect()
  1068. {
  1069. // how many references are there to each cellXf ?
  1070. $countReferencesCellXf = [];
  1071. foreach ($this->cellXfCollection as $index => $cellXf) {
  1072. $countReferencesCellXf[$index] = 0;
  1073. }
  1074. foreach ($this->getWorksheetIterator() as $sheet) {
  1075. // from cells
  1076. foreach ($sheet->getCoordinates(false) as $coordinate) {
  1077. $cell = $sheet->getCell($coordinate);
  1078. ++$countReferencesCellXf[$cell->getXfIndex()];
  1079. }
  1080. // from row dimensions
  1081. foreach ($sheet->getRowDimensions() as $rowDimension) {
  1082. if ($rowDimension->getXfIndex() !== null) {
  1083. ++$countReferencesCellXf[$rowDimension->getXfIndex()];
  1084. }
  1085. }
  1086. // from column dimensions
  1087. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  1088. ++$countReferencesCellXf[$columnDimension->getXfIndex()];
  1089. }
  1090. }
  1091. // remove cellXfs without references and create mapping so we can update xfIndex
  1092. // for all cells and columns
  1093. $countNeededCellXfs = 0;
  1094. foreach ($this->cellXfCollection as $index => $cellXf) {
  1095. if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf
  1096. ++$countNeededCellXfs;
  1097. } else {
  1098. unset($this->cellXfCollection[$index]);
  1099. }
  1100. $map[$index] = $countNeededCellXfs - 1;
  1101. }
  1102. $this->cellXfCollection = array_values($this->cellXfCollection);
  1103. // update the index for all cellXfs
  1104. foreach ($this->cellXfCollection as $i => $cellXf) {
  1105. $cellXf->setIndex($i);
  1106. }
  1107. // make sure there is always at least one cellXf (there should be)
  1108. if (empty($this->cellXfCollection)) {
  1109. $this->cellXfCollection[] = new Style();
  1110. }
  1111. // update the xfIndex for all cells, row dimensions, column dimensions
  1112. foreach ($this->getWorksheetIterator() as $sheet) {
  1113. // for all cells
  1114. foreach ($sheet->getCoordinates(false) as $coordinate) {
  1115. $cell = $sheet->getCell($coordinate);
  1116. $cell->setXfIndex($map[$cell->getXfIndex()]);
  1117. }
  1118. // for all row dimensions
  1119. foreach ($sheet->getRowDimensions() as $rowDimension) {
  1120. if ($rowDimension->getXfIndex() !== null) {
  1121. $rowDimension->setXfIndex($map[$rowDimension->getXfIndex()]);
  1122. }
  1123. }
  1124. // for all column dimensions
  1125. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  1126. $columnDimension->setXfIndex($map[$columnDimension->getXfIndex()]);
  1127. }
  1128. // also do garbage collection for all the sheets
  1129. $sheet->garbageCollect();
  1130. }
  1131. }
  1132. /**
  1133. * Return the unique ID value assigned to this spreadsheet workbook.
  1134. *
  1135. * @return string
  1136. */
  1137. public function getID()
  1138. {
  1139. return $this->uniqueID;
  1140. }
  1141. /**
  1142. * Get the visibility of the horizonal scroll bar in the application.
  1143. *
  1144. * @return bool True if horizonal scroll bar is visible
  1145. */
  1146. public function getShowHorizontalScroll()
  1147. {
  1148. return $this->showHorizontalScroll;
  1149. }
  1150. /**
  1151. * Set the visibility of the horizonal scroll bar in the application.
  1152. *
  1153. * @param bool $showHorizontalScroll True if horizonal scroll bar is visible
  1154. */
  1155. public function setShowHorizontalScroll($showHorizontalScroll)
  1156. {
  1157. $this->showHorizontalScroll = (bool) $showHorizontalScroll;
  1158. }
  1159. /**
  1160. * Get the visibility of the vertical scroll bar in the application.
  1161. *
  1162. * @return bool True if vertical scroll bar is visible
  1163. */
  1164. public function getShowVerticalScroll()
  1165. {
  1166. return $this->showVerticalScroll;
  1167. }
  1168. /**
  1169. * Set the visibility of the vertical scroll bar in the application.
  1170. *
  1171. * @param bool $showVerticalScroll True if vertical scroll bar is visible
  1172. */
  1173. public function setShowVerticalScroll($showVerticalScroll)
  1174. {
  1175. $this->showVerticalScroll = (bool) $showVerticalScroll;
  1176. }
  1177. /**
  1178. * Get the visibility of the sheet tabs in the application.
  1179. *
  1180. * @return bool True if the sheet tabs are visible
  1181. */
  1182. public function getShowSheetTabs()
  1183. {
  1184. return $this->showSheetTabs;
  1185. }
  1186. /**
  1187. * Set the visibility of the sheet tabs in the application.
  1188. *
  1189. * @param bool $showSheetTabs True if sheet tabs are visible
  1190. */
  1191. public function setShowSheetTabs($showSheetTabs)
  1192. {
  1193. $this->showSheetTabs = (bool) $showSheetTabs;
  1194. }
  1195. /**
  1196. * Return whether the workbook window is minimized.
  1197. *
  1198. * @return bool true if workbook window is minimized
  1199. */
  1200. public function getMinimized()
  1201. {
  1202. return $this->minimized;
  1203. }
  1204. /**
  1205. * Set whether the workbook window is minimized.
  1206. *
  1207. * @param bool $minimized true if workbook window is minimized
  1208. */
  1209. public function setMinimized($minimized)
  1210. {
  1211. $this->minimized = (bool) $minimized;
  1212. }
  1213. /**
  1214. * Return whether to group dates when presenting the user with
  1215. * filtering optiomd in the user interface.
  1216. *
  1217. * @return bool true if workbook window is minimized
  1218. */
  1219. public function getAutoFilterDateGrouping()
  1220. {
  1221. return $this->autoFilterDateGrouping;
  1222. }
  1223. /**
  1224. * Set whether to group dates when presenting the user with
  1225. * filtering optiomd in the user interface.
  1226. *
  1227. * @param bool $autoFilterDateGrouping true if workbook window is minimized
  1228. */
  1229. public function setAutoFilterDateGrouping($autoFilterDateGrouping)
  1230. {
  1231. $this->autoFilterDateGrouping = (bool) $autoFilterDateGrouping;
  1232. }
  1233. /**
  1234. * Return the first sheet in the book view.
  1235. *
  1236. * @return int First sheet in book view
  1237. */
  1238. public function getFirstSheetIndex()
  1239. {
  1240. return $this->firstSheetIndex;
  1241. }
  1242. /**
  1243. * Set the first sheet in the book view.
  1244. *
  1245. * @param int $firstSheetIndex First sheet in book view
  1246. *
  1247. * @throws Exception if the given value is invalid
  1248. */
  1249. public function setFirstSheetIndex($firstSheetIndex)
  1250. {
  1251. if ($firstSheetIndex >= 0) {
  1252. $this->firstSheetIndex = (int) $firstSheetIndex;
  1253. } else {
  1254. throw new Exception('First sheet index must be a positive integer.');
  1255. }
  1256. }
  1257. /**
  1258. * Return the visibility status of the workbook.
  1259. *
  1260. * This may be one of the following three values:
  1261. * - visibile
  1262. *
  1263. * @return string Visible status
  1264. */
  1265. public function getVisibility()
  1266. {
  1267. return $this->visibility;
  1268. }
  1269. /**
  1270. * Set the visibility status of the workbook.
  1271. *
  1272. * Valid values are:
  1273. * - 'visible' (self::VISIBILITY_VISIBLE):
  1274. * Workbook window is visible
  1275. * - 'hidden' (self::VISIBILITY_HIDDEN):
  1276. * Workbook window is hidden, but can be shown by the user
  1277. * via the user interface
  1278. * - 'veryHidden' (self::VISIBILITY_VERY_HIDDEN):
  1279. * Workbook window is hidden and cannot be shown in the
  1280. * user interface.
  1281. *
  1282. * @param string $visibility visibility status of the workbook
  1283. *
  1284. * @throws Exception if the given value is invalid
  1285. */
  1286. public function setVisibility($visibility)
  1287. {
  1288. if ($visibility === null) {
  1289. $visibility = self::VISIBILITY_VISIBLE;
  1290. }
  1291. if (in_array($visibility, self::$workbookViewVisibilityValues)) {
  1292. $this->visibility = $visibility;
  1293. } else {
  1294. throw new Exception('Invalid visibility value.');
  1295. }
  1296. }
  1297. /**
  1298. * Get the ratio between the workbook tabs bar and the horizontal scroll bar.
  1299. * TabRatio is assumed to be out of 1000 of the horizontal window width.
  1300. *
  1301. * @return int Ratio between the workbook tabs bar and the horizontal scroll bar
  1302. */
  1303. public function getTabRatio()
  1304. {
  1305. return $this->tabRatio;
  1306. }
  1307. /**
  1308. * Set the ratio between the workbook tabs bar and the horizontal scroll bar
  1309. * TabRatio is assumed to be out of 1000 of the horizontal window width.
  1310. *
  1311. * @param int $tabRatio Ratio between the tabs bar and the horizontal scroll bar
  1312. *
  1313. * @throws Exception if the given value is invalid
  1314. */
  1315. public function setTabRatio($tabRatio)
  1316. {
  1317. if ($tabRatio >= 0 || $tabRatio <= 1000) {
  1318. $this->tabRatio = (int) $tabRatio;
  1319. } else {
  1320. throw new Exception('Tab ratio must be between 0 and 1000.');
  1321. }
  1322. }
  1323. }