Spreadsheet.php 42 KB

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