Spreadsheet.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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 $pSheetCodeName Name of the worksheet to check
  388. *
  389. * @return bool
  390. */
  391. public function sheetCodeNameExists($pSheetCodeName)
  392. {
  393. return $this->getSheetByCodeName($pSheetCodeName) !== null;
  394. }
  395. /**
  396. * Get sheet by code name. Warning : sheet don't have always a code name !
  397. *
  398. * @param string $pName Sheet name
  399. *
  400. * @return null|Worksheet
  401. */
  402. public function getSheetByCodeName($pName)
  403. {
  404. $worksheetCount = count($this->workSheetCollection);
  405. for ($i = 0; $i < $worksheetCount; ++$i) {
  406. if ($this->workSheetCollection[$i]->getCodeName() == $pName) {
  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 $pValue): void
  480. {
  481. $this->properties = $pValue;
  482. }
  483. /**
  484. * Get security.
  485. *
  486. * @return Document\Security
  487. */
  488. public function getSecurity()
  489. {
  490. return $this->security;
  491. }
  492. /**
  493. * Set security.
  494. */
  495. public function setSecurity(Document\Security $pValue): void
  496. {
  497. $this->security = $pValue;
  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 $pSheetName Name of the worksheet to check
  525. *
  526. * @return bool
  527. */
  528. public function sheetNameExists($pSheetName)
  529. {
  530. return $this->getSheetByName($pSheetName) !== null;
  531. }
  532. /**
  533. * Add sheet.
  534. *
  535. * @param null|int $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  536. *
  537. * @return Worksheet
  538. */
  539. public function addSheet(Worksheet $pSheet, $iSheetIndex = null)
  540. {
  541. if ($this->sheetNameExists($pSheet->getTitle())) {
  542. throw new Exception(
  543. "Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first."
  544. );
  545. }
  546. if ($iSheetIndex === null) {
  547. if ($this->activeSheetIndex < 0) {
  548. $this->activeSheetIndex = 0;
  549. }
  550. $this->workSheetCollection[] = $pSheet;
  551. } else {
  552. // Insert the sheet at the requested index
  553. array_splice(
  554. $this->workSheetCollection,
  555. $iSheetIndex,
  556. 0,
  557. [$pSheet]
  558. );
  559. // Adjust active sheet index if necessary
  560. if ($this->activeSheetIndex >= $iSheetIndex) {
  561. ++$this->activeSheetIndex;
  562. }
  563. }
  564. if ($pSheet->getParent() === null) {
  565. $pSheet->rebindParent($this);
  566. }
  567. return $pSheet;
  568. }
  569. /**
  570. * Remove sheet by index.
  571. *
  572. * @param int $pIndex Active sheet index
  573. */
  574. public function removeSheetByIndex($pIndex): void
  575. {
  576. $numSheets = count($this->workSheetCollection);
  577. if ($pIndex > $numSheets - 1) {
  578. throw new Exception(
  579. "You tried to remove a sheet by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
  580. );
  581. }
  582. array_splice($this->workSheetCollection, $pIndex, 1);
  583. // Adjust active sheet index if necessary
  584. if (
  585. ($this->activeSheetIndex >= $pIndex) &&
  586. ($this->activeSheetIndex > 0 || $numSheets <= 1)
  587. ) {
  588. --$this->activeSheetIndex;
  589. }
  590. }
  591. /**
  592. * Get sheet by index.
  593. *
  594. * @param int $pIndex Sheet index
  595. *
  596. * @return Worksheet
  597. */
  598. public function getSheet($pIndex)
  599. {
  600. if (!isset($this->workSheetCollection[$pIndex])) {
  601. $numSheets = $this->getSheetCount();
  602. throw new Exception(
  603. "Your requested sheet index: {$pIndex} is out of bounds. The actual number of sheets is {$numSheets}."
  604. );
  605. }
  606. return $this->workSheetCollection[$pIndex];
  607. }
  608. /**
  609. * Get all sheets.
  610. *
  611. * @return Worksheet[]
  612. */
  613. public function getAllSheets()
  614. {
  615. return $this->workSheetCollection;
  616. }
  617. /**
  618. * Get sheet by name.
  619. *
  620. * @param string $pName Sheet name
  621. *
  622. * @return null|Worksheet
  623. */
  624. public function getSheetByName($pName)
  625. {
  626. $worksheetCount = count($this->workSheetCollection);
  627. for ($i = 0; $i < $worksheetCount; ++$i) {
  628. if ($this->workSheetCollection[$i]->getTitle() === trim($pName, "'")) {
  629. return $this->workSheetCollection[$i];
  630. }
  631. }
  632. return null;
  633. }
  634. /**
  635. * Get index for sheet.
  636. *
  637. * @return int index
  638. */
  639. public function getIndex(Worksheet $pSheet)
  640. {
  641. foreach ($this->workSheetCollection as $key => $value) {
  642. if ($value->getHashCode() === $pSheet->getHashCode()) {
  643. return $key;
  644. }
  645. }
  646. throw new Exception('Sheet does not exist.');
  647. }
  648. /**
  649. * Set index for sheet by sheet name.
  650. *
  651. * @param string $sheetName Sheet name to modify index for
  652. * @param int $newIndex New index for the sheet
  653. *
  654. * @return int New sheet index
  655. */
  656. public function setIndexByName($sheetName, $newIndex)
  657. {
  658. $oldIndex = $this->getIndex($this->getSheetByName($sheetName));
  659. $pSheet = array_splice(
  660. $this->workSheetCollection,
  661. $oldIndex,
  662. 1
  663. );
  664. array_splice(
  665. $this->workSheetCollection,
  666. $newIndex,
  667. 0,
  668. $pSheet
  669. );
  670. return $newIndex;
  671. }
  672. /**
  673. * Get sheet count.
  674. *
  675. * @return int
  676. */
  677. public function getSheetCount()
  678. {
  679. return count($this->workSheetCollection);
  680. }
  681. /**
  682. * Get active sheet index.
  683. *
  684. * @return int Active sheet index
  685. */
  686. public function getActiveSheetIndex()
  687. {
  688. return $this->activeSheetIndex;
  689. }
  690. /**
  691. * Set active sheet index.
  692. *
  693. * @param int $pIndex Active sheet index
  694. *
  695. * @return Worksheet
  696. */
  697. public function setActiveSheetIndex($pIndex)
  698. {
  699. $numSheets = count($this->workSheetCollection);
  700. if ($pIndex > $numSheets - 1) {
  701. throw new Exception(
  702. "You tried to set a sheet active by the out of bounds index: {$pIndex}. The actual number of sheets is {$numSheets}."
  703. );
  704. }
  705. $this->activeSheetIndex = $pIndex;
  706. return $this->getActiveSheet();
  707. }
  708. /**
  709. * Set active sheet index by name.
  710. *
  711. * @param string $pValue Sheet title
  712. *
  713. * @return Worksheet
  714. */
  715. public function setActiveSheetIndexByName($pValue)
  716. {
  717. if (($worksheet = $this->getSheetByName($pValue)) instanceof Worksheet) {
  718. $this->setActiveSheetIndex($this->getIndex($worksheet));
  719. return $worksheet;
  720. }
  721. throw new Exception('Workbook does not contain sheet:' . $pValue);
  722. }
  723. /**
  724. * Get sheet names.
  725. *
  726. * @return string[]
  727. */
  728. public function getSheetNames()
  729. {
  730. $returnValue = [];
  731. $worksheetCount = $this->getSheetCount();
  732. for ($i = 0; $i < $worksheetCount; ++$i) {
  733. $returnValue[] = $this->getSheet($i)->getTitle();
  734. }
  735. return $returnValue;
  736. }
  737. /**
  738. * Add external sheet.
  739. *
  740. * @param Worksheet $pSheet External sheet to add
  741. * @param null|int $iSheetIndex Index where sheet should go (0,1,..., or null for last)
  742. *
  743. * @return Worksheet
  744. */
  745. public function addExternalSheet(Worksheet $pSheet, $iSheetIndex = null)
  746. {
  747. if ($this->sheetNameExists($pSheet->getTitle())) {
  748. throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first.");
  749. }
  750. // count how many cellXfs there are in this workbook currently, we will need this below
  751. $countCellXfs = count($this->cellXfCollection);
  752. // copy all the shared cellXfs from the external workbook and append them to the current
  753. foreach ($pSheet->getParent()->getCellXfCollection() as $cellXf) {
  754. $this->addCellXf(clone $cellXf);
  755. }
  756. // move sheet to this workbook
  757. $pSheet->rebindParent($this);
  758. // update the cellXfs
  759. foreach ($pSheet->getCoordinates(false) as $coordinate) {
  760. $cell = $pSheet->getCell($coordinate);
  761. $cell->setXfIndex($cell->getXfIndex() + $countCellXfs);
  762. }
  763. return $this->addSheet($pSheet, $iSheetIndex);
  764. }
  765. /**
  766. * Get an array of all Named Ranges.
  767. *
  768. * @return DefinedName[]
  769. */
  770. public function getNamedRanges(): array
  771. {
  772. return array_filter(
  773. $this->definedNames,
  774. function (DefinedName $definedName) {
  775. return $definedName->isFormula() === self::DEFINED_NAME_IS_RANGE;
  776. }
  777. );
  778. }
  779. /**
  780. * Get an array of all Named Formulae.
  781. *
  782. * @return DefinedName[]
  783. */
  784. public function getNamedFormulae(): array
  785. {
  786. return array_filter(
  787. $this->definedNames,
  788. function (DefinedName $definedName) {
  789. return $definedName->isFormula() === self::DEFINED_NAME_IS_FORMULA;
  790. }
  791. );
  792. }
  793. /**
  794. * Get an array of all Defined Names (both named ranges and named formulae).
  795. *
  796. * @return DefinedName[]
  797. */
  798. public function getDefinedNames(): array
  799. {
  800. return $this->definedNames;
  801. }
  802. /**
  803. * Add a named range.
  804. * If a named range with this name already exists, then this will replace the existing value.
  805. */
  806. public function addNamedRange(NamedRange $namedRange): void
  807. {
  808. $this->addDefinedName($namedRange);
  809. }
  810. /**
  811. * Add a named formula.
  812. * If a named formula with this name already exists, then this will replace the existing value.
  813. */
  814. public function addNamedFormula(NamedFormula $namedFormula): void
  815. {
  816. $this->addDefinedName($namedFormula);
  817. }
  818. /**
  819. * Add a defined name (either a named range or a named formula).
  820. * If a defined named with this name already exists, then this will replace the existing value.
  821. */
  822. public function addDefinedName(DefinedName $definedName): void
  823. {
  824. $upperCaseName = StringHelper::strToUpper($definedName->getName());
  825. if ($definedName->getScope() == null) {
  826. // global scope
  827. $this->definedNames[$upperCaseName] = $definedName;
  828. } else {
  829. // local scope
  830. $this->definedNames[$definedName->getScope()->getTitle() . '!' . $upperCaseName] = $definedName;
  831. }
  832. }
  833. /**
  834. * Get named range.
  835. *
  836. * @param null|Worksheet $pSheet Scope. Use null for global scope
  837. */
  838. public function getNamedRange(string $namedRange, ?Worksheet $pSheet = null): ?NamedRange
  839. {
  840. $returnValue = null;
  841. if ($namedRange !== '') {
  842. $namedRange = StringHelper::strToUpper($namedRange);
  843. // first look for global named range
  844. $returnValue = $this->getGlobalDefinedNameByType($namedRange, self::DEFINED_NAME_IS_RANGE);
  845. // then look for local named range (has priority over global named range if both names exist)
  846. $returnValue = $this->getLocalDefinedNameByType($namedRange, self::DEFINED_NAME_IS_RANGE, $pSheet) ?: $returnValue;
  847. }
  848. return $returnValue instanceof NamedRange ? $returnValue : null;
  849. }
  850. /**
  851. * Get named formula.
  852. *
  853. * @param null|Worksheet $pSheet Scope. Use null for global scope
  854. */
  855. public function getNamedFormula(string $namedFormula, ?Worksheet $pSheet = null): ?NamedFormula
  856. {
  857. $returnValue = null;
  858. if ($namedFormula !== '') {
  859. $namedFormula = StringHelper::strToUpper($namedFormula);
  860. // first look for global named formula
  861. $returnValue = $this->getGlobalDefinedNameByType($namedFormula, self::DEFINED_NAME_IS_FORMULA);
  862. // then look for local named formula (has priority over global named formula if both names exist)
  863. $returnValue = $this->getLocalDefinedNameByType($namedFormula, self::DEFINED_NAME_IS_FORMULA, $pSheet) ?: $returnValue;
  864. }
  865. return $returnValue instanceof NamedFormula ? $returnValue : null;
  866. }
  867. private function getGlobalDefinedNameByType(string $name, bool $type): ?DefinedName
  868. {
  869. if (isset($this->definedNames[$name]) && $this->definedNames[$name]->isFormula() === $type) {
  870. return $this->definedNames[$name];
  871. }
  872. return null;
  873. }
  874. private function getLocalDefinedNameByType(string $name, bool $type, ?Worksheet $pSheet = null): ?DefinedName
  875. {
  876. if (
  877. ($pSheet !== null) && isset($this->definedNames[$pSheet->getTitle() . '!' . $name])
  878. && $this->definedNames[$pSheet->getTitle() . '!' . $name]->isFormula() === $type
  879. ) {
  880. return $this->definedNames[$pSheet->getTitle() . '!' . $name];
  881. }
  882. return null;
  883. }
  884. /**
  885. * Get named range.
  886. *
  887. * @param null|Worksheet $pSheet Scope. Use null for global scope
  888. */
  889. public function getDefinedName(string $definedName, ?Worksheet $pSheet = null): ?DefinedName
  890. {
  891. $returnValue = null;
  892. if ($definedName !== '') {
  893. $definedName = StringHelper::strToUpper($definedName);
  894. // first look for global defined name
  895. if (isset($this->definedNames[$definedName])) {
  896. $returnValue = $this->definedNames[$definedName];
  897. }
  898. // then look for local defined name (has priority over global defined name if both names exist)
  899. if (($pSheet !== null) && isset($this->definedNames[$pSheet->getTitle() . '!' . $definedName])) {
  900. $returnValue = $this->definedNames[$pSheet->getTitle() . '!' . $definedName];
  901. }
  902. }
  903. return $returnValue;
  904. }
  905. /**
  906. * Remove named range.
  907. *
  908. * @param null|Worksheet $pSheet scope: use null for global scope
  909. *
  910. * @return $this
  911. */
  912. public function removeNamedRange(string $namedRange, ?Worksheet $pSheet = null): self
  913. {
  914. if ($this->getNamedRange($namedRange, $pSheet) === null) {
  915. return $this;
  916. }
  917. return $this->removeDefinedName($namedRange, $pSheet);
  918. }
  919. /**
  920. * Remove named formula.
  921. *
  922. * @param null|Worksheet $pSheet scope: use null for global scope
  923. *
  924. * @return $this
  925. */
  926. public function removeNamedFormula(string $namedFormula, ?Worksheet $pSheet = null): self
  927. {
  928. if ($this->getNamedFormula($namedFormula, $pSheet) === null) {
  929. return $this;
  930. }
  931. return $this->removeDefinedName($namedFormula, $pSheet);
  932. }
  933. /**
  934. * Remove defined name.
  935. *
  936. * @param null|Worksheet $pSheet scope: use null for global scope
  937. *
  938. * @return $this
  939. */
  940. public function removeDefinedName(string $definedName, ?Worksheet $pSheet = null): self
  941. {
  942. $definedName = StringHelper::strToUpper($definedName);
  943. if ($pSheet === null) {
  944. if (isset($this->definedNames[$definedName])) {
  945. unset($this->definedNames[$definedName]);
  946. }
  947. } else {
  948. if (isset($this->definedNames[$pSheet->getTitle() . '!' . $definedName])) {
  949. unset($this->definedNames[$pSheet->getTitle() . '!' . $definedName]);
  950. } elseif (isset($this->definedNames[$definedName])) {
  951. unset($this->definedNames[$definedName]);
  952. }
  953. }
  954. return $this;
  955. }
  956. /**
  957. * Get worksheet iterator.
  958. *
  959. * @return Iterator
  960. */
  961. public function getWorksheetIterator()
  962. {
  963. return new Iterator($this);
  964. }
  965. /**
  966. * Copy workbook (!= clone!).
  967. *
  968. * @return Spreadsheet
  969. */
  970. public function copy()
  971. {
  972. $copied = clone $this;
  973. $worksheetCount = count($this->workSheetCollection);
  974. for ($i = 0; $i < $worksheetCount; ++$i) {
  975. $this->workSheetCollection[$i] = $this->workSheetCollection[$i]->copy();
  976. $this->workSheetCollection[$i]->rebindParent($this);
  977. }
  978. return $copied;
  979. }
  980. /**
  981. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  982. */
  983. public function __clone()
  984. {
  985. // @phpstan-ignore-next-line
  986. foreach ($this as $key => $val) {
  987. if (is_object($val) || (is_array($val))) {
  988. $this->{$key} = unserialize(serialize($val));
  989. }
  990. }
  991. }
  992. /**
  993. * Get the workbook collection of cellXfs.
  994. *
  995. * @return Style[]
  996. */
  997. public function getCellXfCollection()
  998. {
  999. return $this->cellXfCollection;
  1000. }
  1001. /**
  1002. * Get cellXf by index.
  1003. *
  1004. * @param int $pIndex
  1005. *
  1006. * @return Style
  1007. */
  1008. public function getCellXfByIndex($pIndex)
  1009. {
  1010. return $this->cellXfCollection[$pIndex];
  1011. }
  1012. /**
  1013. * Get cellXf by hash code.
  1014. *
  1015. * @param string $pValue
  1016. *
  1017. * @return false|Style
  1018. */
  1019. public function getCellXfByHashCode($pValue)
  1020. {
  1021. foreach ($this->cellXfCollection as $cellXf) {
  1022. if ($cellXf->getHashCode() === $pValue) {
  1023. return $cellXf;
  1024. }
  1025. }
  1026. return false;
  1027. }
  1028. /**
  1029. * Check if style exists in style collection.
  1030. *
  1031. * @param Style $pCellStyle
  1032. *
  1033. * @return bool
  1034. */
  1035. public function cellXfExists($pCellStyle)
  1036. {
  1037. return in_array($pCellStyle, $this->cellXfCollection, true);
  1038. }
  1039. /**
  1040. * Get default style.
  1041. *
  1042. * @return Style
  1043. */
  1044. public function getDefaultStyle()
  1045. {
  1046. if (isset($this->cellXfCollection[0])) {
  1047. return $this->cellXfCollection[0];
  1048. }
  1049. throw new Exception('No default style found for this workbook');
  1050. }
  1051. /**
  1052. * Add a cellXf to the workbook.
  1053. */
  1054. public function addCellXf(Style $style): void
  1055. {
  1056. $this->cellXfCollection[] = $style;
  1057. $style->setIndex(count($this->cellXfCollection) - 1);
  1058. }
  1059. /**
  1060. * Remove cellXf by index. It is ensured that all cells get their xf index updated.
  1061. *
  1062. * @param int $pIndex Index to cellXf
  1063. */
  1064. public function removeCellXfByIndex($pIndex): void
  1065. {
  1066. if ($pIndex > count($this->cellXfCollection) - 1) {
  1067. throw new Exception('CellXf index is out of bounds.');
  1068. }
  1069. // first remove the cellXf
  1070. array_splice($this->cellXfCollection, $pIndex, 1);
  1071. // then update cellXf indexes for cells
  1072. foreach ($this->workSheetCollection as $worksheet) {
  1073. foreach ($worksheet->getCoordinates(false) as $coordinate) {
  1074. $cell = $worksheet->getCell($coordinate);
  1075. $xfIndex = $cell->getXfIndex();
  1076. if ($xfIndex > $pIndex) {
  1077. // decrease xf index by 1
  1078. $cell->setXfIndex($xfIndex - 1);
  1079. } elseif ($xfIndex == $pIndex) {
  1080. // set to default xf index 0
  1081. $cell->setXfIndex(0);
  1082. }
  1083. }
  1084. }
  1085. }
  1086. /**
  1087. * Get the cellXf supervisor.
  1088. *
  1089. * @return Style
  1090. */
  1091. public function getCellXfSupervisor()
  1092. {
  1093. return $this->cellXfSupervisor;
  1094. }
  1095. /**
  1096. * Get the workbook collection of cellStyleXfs.
  1097. *
  1098. * @return Style[]
  1099. */
  1100. public function getCellStyleXfCollection()
  1101. {
  1102. return $this->cellStyleXfCollection;
  1103. }
  1104. /**
  1105. * Get cellStyleXf by index.
  1106. *
  1107. * @param int $pIndex Index to cellXf
  1108. *
  1109. * @return Style
  1110. */
  1111. public function getCellStyleXfByIndex($pIndex)
  1112. {
  1113. return $this->cellStyleXfCollection[$pIndex];
  1114. }
  1115. /**
  1116. * Get cellStyleXf by hash code.
  1117. *
  1118. * @param string $pValue
  1119. *
  1120. * @return false|Style
  1121. */
  1122. public function getCellStyleXfByHashCode($pValue)
  1123. {
  1124. foreach ($this->cellStyleXfCollection as $cellStyleXf) {
  1125. if ($cellStyleXf->getHashCode() === $pValue) {
  1126. return $cellStyleXf;
  1127. }
  1128. }
  1129. return false;
  1130. }
  1131. /**
  1132. * Add a cellStyleXf to the workbook.
  1133. */
  1134. public function addCellStyleXf(Style $pStyle): void
  1135. {
  1136. $this->cellStyleXfCollection[] = $pStyle;
  1137. $pStyle->setIndex(count($this->cellStyleXfCollection) - 1);
  1138. }
  1139. /**
  1140. * Remove cellStyleXf by index.
  1141. *
  1142. * @param int $pIndex Index to cellXf
  1143. */
  1144. public function removeCellStyleXfByIndex($pIndex): void
  1145. {
  1146. if ($pIndex > count($this->cellStyleXfCollection) - 1) {
  1147. throw new Exception('CellStyleXf index is out of bounds.');
  1148. }
  1149. array_splice($this->cellStyleXfCollection, $pIndex, 1);
  1150. }
  1151. /**
  1152. * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells
  1153. * and columns in the workbook.
  1154. */
  1155. public function garbageCollect(): void
  1156. {
  1157. // how many references are there to each cellXf ?
  1158. $countReferencesCellXf = [];
  1159. foreach ($this->cellXfCollection as $index => $cellXf) {
  1160. $countReferencesCellXf[$index] = 0;
  1161. }
  1162. foreach ($this->getWorksheetIterator() as $sheet) {
  1163. // from cells
  1164. foreach ($sheet->getCoordinates(false) as $coordinate) {
  1165. $cell = $sheet->getCell($coordinate);
  1166. ++$countReferencesCellXf[$cell->getXfIndex()];
  1167. }
  1168. // from row dimensions
  1169. foreach ($sheet->getRowDimensions() as $rowDimension) {
  1170. if ($rowDimension->getXfIndex() !== null) {
  1171. ++$countReferencesCellXf[$rowDimension->getXfIndex()];
  1172. }
  1173. }
  1174. // from column dimensions
  1175. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  1176. ++$countReferencesCellXf[$columnDimension->getXfIndex()];
  1177. }
  1178. }
  1179. // remove cellXfs without references and create mapping so we can update xfIndex
  1180. // for all cells and columns
  1181. $countNeededCellXfs = 0;
  1182. $map = [];
  1183. foreach ($this->cellXfCollection as $index => $cellXf) {
  1184. if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf
  1185. ++$countNeededCellXfs;
  1186. } else {
  1187. unset($this->cellXfCollection[$index]);
  1188. }
  1189. $map[$index] = $countNeededCellXfs - 1;
  1190. }
  1191. $this->cellXfCollection = array_values($this->cellXfCollection);
  1192. // update the index for all cellXfs
  1193. foreach ($this->cellXfCollection as $i => $cellXf) {
  1194. $cellXf->setIndex($i);
  1195. }
  1196. // make sure there is always at least one cellXf (there should be)
  1197. if (empty($this->cellXfCollection)) {
  1198. $this->cellXfCollection[] = new Style();
  1199. }
  1200. // update the xfIndex for all cells, row dimensions, column dimensions
  1201. foreach ($this->getWorksheetIterator() as $sheet) {
  1202. // for all cells
  1203. foreach ($sheet->getCoordinates(false) as $coordinate) {
  1204. $cell = $sheet->getCell($coordinate);
  1205. $cell->setXfIndex($map[$cell->getXfIndex()]);
  1206. }
  1207. // for all row dimensions
  1208. foreach ($sheet->getRowDimensions() as $rowDimension) {
  1209. if ($rowDimension->getXfIndex() !== null) {
  1210. $rowDimension->setXfIndex($map[$rowDimension->getXfIndex()]);
  1211. }
  1212. }
  1213. // for all column dimensions
  1214. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  1215. $columnDimension->setXfIndex($map[$columnDimension->getXfIndex()]);
  1216. }
  1217. // also do garbage collection for all the sheets
  1218. $sheet->garbageCollect();
  1219. }
  1220. }
  1221. /**
  1222. * Return the unique ID value assigned to this spreadsheet workbook.
  1223. *
  1224. * @return string
  1225. */
  1226. public function getID()
  1227. {
  1228. return $this->uniqueID;
  1229. }
  1230. /**
  1231. * Get the visibility of the horizonal scroll bar in the application.
  1232. *
  1233. * @return bool True if horizonal scroll bar is visible
  1234. */
  1235. public function getShowHorizontalScroll()
  1236. {
  1237. return $this->showHorizontalScroll;
  1238. }
  1239. /**
  1240. * Set the visibility of the horizonal scroll bar in the application.
  1241. *
  1242. * @param bool $showHorizontalScroll True if horizonal scroll bar is visible
  1243. */
  1244. public function setShowHorizontalScroll($showHorizontalScroll): void
  1245. {
  1246. $this->showHorizontalScroll = (bool) $showHorizontalScroll;
  1247. }
  1248. /**
  1249. * Get the visibility of the vertical scroll bar in the application.
  1250. *
  1251. * @return bool True if vertical scroll bar is visible
  1252. */
  1253. public function getShowVerticalScroll()
  1254. {
  1255. return $this->showVerticalScroll;
  1256. }
  1257. /**
  1258. * Set the visibility of the vertical scroll bar in the application.
  1259. *
  1260. * @param bool $showVerticalScroll True if vertical scroll bar is visible
  1261. */
  1262. public function setShowVerticalScroll($showVerticalScroll): void
  1263. {
  1264. $this->showVerticalScroll = (bool) $showVerticalScroll;
  1265. }
  1266. /**
  1267. * Get the visibility of the sheet tabs in the application.
  1268. *
  1269. * @return bool True if the sheet tabs are visible
  1270. */
  1271. public function getShowSheetTabs()
  1272. {
  1273. return $this->showSheetTabs;
  1274. }
  1275. /**
  1276. * Set the visibility of the sheet tabs in the application.
  1277. *
  1278. * @param bool $showSheetTabs True if sheet tabs are visible
  1279. */
  1280. public function setShowSheetTabs($showSheetTabs): void
  1281. {
  1282. $this->showSheetTabs = (bool) $showSheetTabs;
  1283. }
  1284. /**
  1285. * Return whether the workbook window is minimized.
  1286. *
  1287. * @return bool true if workbook window is minimized
  1288. */
  1289. public function getMinimized()
  1290. {
  1291. return $this->minimized;
  1292. }
  1293. /**
  1294. * Set whether the workbook window is minimized.
  1295. *
  1296. * @param bool $minimized true if workbook window is minimized
  1297. */
  1298. public function setMinimized($minimized): void
  1299. {
  1300. $this->minimized = (bool) $minimized;
  1301. }
  1302. /**
  1303. * Return whether to group dates when presenting the user with
  1304. * filtering optiomd in the user interface.
  1305. *
  1306. * @return bool true if workbook window is minimized
  1307. */
  1308. public function getAutoFilterDateGrouping()
  1309. {
  1310. return $this->autoFilterDateGrouping;
  1311. }
  1312. /**
  1313. * Set whether to group dates when presenting the user with
  1314. * filtering optiomd in the user interface.
  1315. *
  1316. * @param bool $autoFilterDateGrouping true if workbook window is minimized
  1317. */
  1318. public function setAutoFilterDateGrouping($autoFilterDateGrouping): void
  1319. {
  1320. $this->autoFilterDateGrouping = (bool) $autoFilterDateGrouping;
  1321. }
  1322. /**
  1323. * Return the first sheet in the book view.
  1324. *
  1325. * @return int First sheet in book view
  1326. */
  1327. public function getFirstSheetIndex()
  1328. {
  1329. return $this->firstSheetIndex;
  1330. }
  1331. /**
  1332. * Set the first sheet in the book view.
  1333. *
  1334. * @param int $firstSheetIndex First sheet in book view
  1335. */
  1336. public function setFirstSheetIndex($firstSheetIndex): void
  1337. {
  1338. if ($firstSheetIndex >= 0) {
  1339. $this->firstSheetIndex = (int) $firstSheetIndex;
  1340. } else {
  1341. throw new Exception('First sheet index must be a positive integer.');
  1342. }
  1343. }
  1344. /**
  1345. * Return the visibility status of the workbook.
  1346. *
  1347. * This may be one of the following three values:
  1348. * - visibile
  1349. *
  1350. * @return string Visible status
  1351. */
  1352. public function getVisibility()
  1353. {
  1354. return $this->visibility;
  1355. }
  1356. /**
  1357. * Set the visibility status of the workbook.
  1358. *
  1359. * Valid values are:
  1360. * - 'visible' (self::VISIBILITY_VISIBLE):
  1361. * Workbook window is visible
  1362. * - 'hidden' (self::VISIBILITY_HIDDEN):
  1363. * Workbook window is hidden, but can be shown by the user
  1364. * via the user interface
  1365. * - 'veryHidden' (self::VISIBILITY_VERY_HIDDEN):
  1366. * Workbook window is hidden and cannot be shown in the
  1367. * user interface.
  1368. *
  1369. * @param string $visibility visibility status of the workbook
  1370. */
  1371. public function setVisibility($visibility): void
  1372. {
  1373. if ($visibility === null) {
  1374. $visibility = self::VISIBILITY_VISIBLE;
  1375. }
  1376. if (in_array($visibility, self::$workbookViewVisibilityValues)) {
  1377. $this->visibility = $visibility;
  1378. } else {
  1379. throw new Exception('Invalid visibility value.');
  1380. }
  1381. }
  1382. /**
  1383. * Get the ratio between the workbook tabs bar and the horizontal scroll bar.
  1384. * TabRatio is assumed to be out of 1000 of the horizontal window width.
  1385. *
  1386. * @return int Ratio between the workbook tabs bar and the horizontal scroll bar
  1387. */
  1388. public function getTabRatio()
  1389. {
  1390. return $this->tabRatio;
  1391. }
  1392. /**
  1393. * Set the ratio between the workbook tabs bar and the horizontal scroll bar
  1394. * TabRatio is assumed to be out of 1000 of the horizontal window width.
  1395. *
  1396. * @param int $tabRatio Ratio between the tabs bar and the horizontal scroll bar
  1397. */
  1398. public function setTabRatio($tabRatio): void
  1399. {
  1400. if ($tabRatio >= 0 || $tabRatio <= 1000) {
  1401. $this->tabRatio = (int) $tabRatio;
  1402. } else {
  1403. throw new Exception('Tab ratio must be between 0 and 1000.');
  1404. }
  1405. }
  1406. }