Spreadsheet.php 38 KB

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