截流自动化的商城平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Spreadsheet.php 42KB

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 $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. * @param Style $cellStyleIndex
  1033. *
  1034. * @return bool
  1035. */
  1036. public function cellXfExists($cellStyleIndex)
  1037. {
  1038. return in_array($cellStyleIndex, $this->cellXfCollection, true);
  1039. }
  1040. /**
  1041. * Get default style.
  1042. *
  1043. * @return Style
  1044. */
  1045. public function getDefaultStyle()
  1046. {
  1047. if (isset($this->cellXfCollection[0])) {
  1048. return $this->cellXfCollection[0];
  1049. }
  1050. throw new Exception('No default style found for this workbook');
  1051. }
  1052. /**
  1053. * Add a cellXf to the workbook.
  1054. */
  1055. public function addCellXf(Style $style): void
  1056. {
  1057. $this->cellXfCollection[] = $style;
  1058. $style->setIndex(count($this->cellXfCollection) - 1);
  1059. }
  1060. /**
  1061. * Remove cellXf by index. It is ensured that all cells get their xf index updated.
  1062. *
  1063. * @param int $cellStyleIndex Index to cellXf
  1064. */
  1065. public function removeCellXfByIndex($cellStyleIndex): void
  1066. {
  1067. if ($cellStyleIndex > count($this->cellXfCollection) - 1) {
  1068. throw new Exception('CellXf index is out of bounds.');
  1069. }
  1070. // first remove the cellXf
  1071. array_splice($this->cellXfCollection, $cellStyleIndex, 1);
  1072. // then update cellXf indexes for cells
  1073. foreach ($this->workSheetCollection as $worksheet) {
  1074. foreach ($worksheet->getCoordinates(false) as $coordinate) {
  1075. $cell = $worksheet->getCell($coordinate);
  1076. $xfIndex = $cell->getXfIndex();
  1077. if ($xfIndex > $cellStyleIndex) {
  1078. // decrease xf index by 1
  1079. $cell->setXfIndex($xfIndex - 1);
  1080. } elseif ($xfIndex == $cellStyleIndex) {
  1081. // set to default xf index 0
  1082. $cell->setXfIndex(0);
  1083. }
  1084. }
  1085. }
  1086. }
  1087. /**
  1088. * Get the cellXf supervisor.
  1089. *
  1090. * @return Style
  1091. */
  1092. public function getCellXfSupervisor()
  1093. {
  1094. return $this->cellXfSupervisor;
  1095. }
  1096. /**
  1097. * Get the workbook collection of cellStyleXfs.
  1098. *
  1099. * @return Style[]
  1100. */
  1101. public function getCellStyleXfCollection()
  1102. {
  1103. return $this->cellStyleXfCollection;
  1104. }
  1105. /**
  1106. * Get cellStyleXf by index.
  1107. *
  1108. * @param int $cellStyleIndex Index to cellXf
  1109. *
  1110. * @return Style
  1111. */
  1112. public function getCellStyleXfByIndex($cellStyleIndex)
  1113. {
  1114. return $this->cellStyleXfCollection[$cellStyleIndex];
  1115. }
  1116. /**
  1117. * Get cellStyleXf by hash code.
  1118. *
  1119. * @param string $hashcode
  1120. *
  1121. * @return false|Style
  1122. */
  1123. public function getCellStyleXfByHashCode($hashcode)
  1124. {
  1125. foreach ($this->cellStyleXfCollection as $cellStyleXf) {
  1126. if ($cellStyleXf->getHashCode() === $hashcode) {
  1127. return $cellStyleXf;
  1128. }
  1129. }
  1130. return false;
  1131. }
  1132. /**
  1133. * Add a cellStyleXf to the workbook.
  1134. */
  1135. public function addCellStyleXf(Style $style): void
  1136. {
  1137. $this->cellStyleXfCollection[] = $style;
  1138. $style->setIndex(count($this->cellStyleXfCollection) - 1);
  1139. }
  1140. /**
  1141. * Remove cellStyleXf by index.
  1142. *
  1143. * @param int $cellStyleIndex Index to cellXf
  1144. */
  1145. public function removeCellStyleXfByIndex($cellStyleIndex): void
  1146. {
  1147. if ($cellStyleIndex > count($this->cellStyleXfCollection) - 1) {
  1148. throw new Exception('CellStyleXf index is out of bounds.');
  1149. }
  1150. array_splice($this->cellStyleXfCollection, $cellStyleIndex, 1);
  1151. }
  1152. /**
  1153. * Eliminate all unneeded cellXf and afterwards update the xfIndex for all cells
  1154. * and columns in the workbook.
  1155. */
  1156. public function garbageCollect(): void
  1157. {
  1158. // how many references are there to each cellXf ?
  1159. $countReferencesCellXf = [];
  1160. foreach ($this->cellXfCollection as $index => $cellXf) {
  1161. $countReferencesCellXf[$index] = 0;
  1162. }
  1163. foreach ($this->getWorksheetIterator() as $sheet) {
  1164. // from cells
  1165. foreach ($sheet->getCoordinates(false) as $coordinate) {
  1166. $cell = $sheet->getCell($coordinate);
  1167. ++$countReferencesCellXf[$cell->getXfIndex()];
  1168. }
  1169. // from row dimensions
  1170. foreach ($sheet->getRowDimensions() as $rowDimension) {
  1171. if ($rowDimension->getXfIndex() !== null) {
  1172. ++$countReferencesCellXf[$rowDimension->getXfIndex()];
  1173. }
  1174. }
  1175. // from column dimensions
  1176. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  1177. ++$countReferencesCellXf[$columnDimension->getXfIndex()];
  1178. }
  1179. }
  1180. // remove cellXfs without references and create mapping so we can update xfIndex
  1181. // for all cells and columns
  1182. $countNeededCellXfs = 0;
  1183. $map = [];
  1184. foreach ($this->cellXfCollection as $index => $cellXf) {
  1185. if ($countReferencesCellXf[$index] > 0 || $index == 0) { // we must never remove the first cellXf
  1186. ++$countNeededCellXfs;
  1187. } else {
  1188. unset($this->cellXfCollection[$index]);
  1189. }
  1190. $map[$index] = $countNeededCellXfs - 1;
  1191. }
  1192. $this->cellXfCollection = array_values($this->cellXfCollection);
  1193. // update the index for all cellXfs
  1194. foreach ($this->cellXfCollection as $i => $cellXf) {
  1195. $cellXf->setIndex($i);
  1196. }
  1197. // make sure there is always at least one cellXf (there should be)
  1198. if (empty($this->cellXfCollection)) {
  1199. $this->cellXfCollection[] = new Style();
  1200. }
  1201. // update the xfIndex for all cells, row dimensions, column dimensions
  1202. foreach ($this->getWorksheetIterator() as $sheet) {
  1203. // for all cells
  1204. foreach ($sheet->getCoordinates(false) as $coordinate) {
  1205. $cell = $sheet->getCell($coordinate);
  1206. $cell->setXfIndex($map[$cell->getXfIndex()]);
  1207. }
  1208. // for all row dimensions
  1209. foreach ($sheet->getRowDimensions() as $rowDimension) {
  1210. if ($rowDimension->getXfIndex() !== null) {
  1211. $rowDimension->setXfIndex($map[$rowDimension->getXfIndex()]);
  1212. }
  1213. }
  1214. // for all column dimensions
  1215. foreach ($sheet->getColumnDimensions() as $columnDimension) {
  1216. $columnDimension->setXfIndex($map[$columnDimension->getXfIndex()]);
  1217. }
  1218. // also do garbage collection for all the sheets
  1219. $sheet->garbageCollect();
  1220. }
  1221. }
  1222. /**
  1223. * Return the unique ID value assigned to this spreadsheet workbook.
  1224. *
  1225. * @return string
  1226. */
  1227. public function getID()
  1228. {
  1229. return $this->uniqueID;
  1230. }
  1231. /**
  1232. * Get the visibility of the horizonal scroll bar in the application.
  1233. *
  1234. * @return bool True if horizonal scroll bar is visible
  1235. */
  1236. public function getShowHorizontalScroll()
  1237. {
  1238. return $this->showHorizontalScroll;
  1239. }
  1240. /**
  1241. * Set the visibility of the horizonal scroll bar in the application.
  1242. *
  1243. * @param bool $showHorizontalScroll True if horizonal scroll bar is visible
  1244. */
  1245. public function setShowHorizontalScroll($showHorizontalScroll): void
  1246. {
  1247. $this->showHorizontalScroll = (bool) $showHorizontalScroll;
  1248. }
  1249. /**
  1250. * Get the visibility of the vertical scroll bar in the application.
  1251. *
  1252. * @return bool True if vertical scroll bar is visible
  1253. */
  1254. public function getShowVerticalScroll()
  1255. {
  1256. return $this->showVerticalScroll;
  1257. }
  1258. /**
  1259. * Set the visibility of the vertical scroll bar in the application.
  1260. *
  1261. * @param bool $showVerticalScroll True if vertical scroll bar is visible
  1262. */
  1263. public function setShowVerticalScroll($showVerticalScroll): void
  1264. {
  1265. $this->showVerticalScroll = (bool) $showVerticalScroll;
  1266. }
  1267. /**
  1268. * Get the visibility of the sheet tabs in the application.
  1269. *
  1270. * @return bool True if the sheet tabs are visible
  1271. */
  1272. public function getShowSheetTabs()
  1273. {
  1274. return $this->showSheetTabs;
  1275. }
  1276. /**
  1277. * Set the visibility of the sheet tabs in the application.
  1278. *
  1279. * @param bool $showSheetTabs True if sheet tabs are visible
  1280. */
  1281. public function setShowSheetTabs($showSheetTabs): void
  1282. {
  1283. $this->showSheetTabs = (bool) $showSheetTabs;
  1284. }
  1285. /**
  1286. * Return whether the workbook window is minimized.
  1287. *
  1288. * @return bool true if workbook window is minimized
  1289. */
  1290. public function getMinimized()
  1291. {
  1292. return $this->minimized;
  1293. }
  1294. /**
  1295. * Set whether the workbook window is minimized.
  1296. *
  1297. * @param bool $minimized true if workbook window is minimized
  1298. */
  1299. public function setMinimized($minimized): void
  1300. {
  1301. $this->minimized = (bool) $minimized;
  1302. }
  1303. /**
  1304. * Return whether to group dates when presenting the user with
  1305. * filtering optiomd in the user interface.
  1306. *
  1307. * @return bool true if workbook window is minimized
  1308. */
  1309. public function getAutoFilterDateGrouping()
  1310. {
  1311. return $this->autoFilterDateGrouping;
  1312. }
  1313. /**
  1314. * Set whether to group dates when presenting the user with
  1315. * filtering optiomd in the user interface.
  1316. *
  1317. * @param bool $autoFilterDateGrouping true if workbook window is minimized
  1318. */
  1319. public function setAutoFilterDateGrouping($autoFilterDateGrouping): void
  1320. {
  1321. $this->autoFilterDateGrouping = (bool) $autoFilterDateGrouping;
  1322. }
  1323. /**
  1324. * Return the first sheet in the book view.
  1325. *
  1326. * @return int First sheet in book view
  1327. */
  1328. public function getFirstSheetIndex()
  1329. {
  1330. return $this->firstSheetIndex;
  1331. }
  1332. /**
  1333. * Set the first sheet in the book view.
  1334. *
  1335. * @param int $firstSheetIndex First sheet in book view
  1336. */
  1337. public function setFirstSheetIndex($firstSheetIndex): void
  1338. {
  1339. if ($firstSheetIndex >= 0) {
  1340. $this->firstSheetIndex = (int) $firstSheetIndex;
  1341. } else {
  1342. throw new Exception('First sheet index must be a positive integer.');
  1343. }
  1344. }
  1345. /**
  1346. * Return the visibility status of the workbook.
  1347. *
  1348. * This may be one of the following three values:
  1349. * - visibile
  1350. *
  1351. * @return string Visible status
  1352. */
  1353. public function getVisibility()
  1354. {
  1355. return $this->visibility;
  1356. }
  1357. /**
  1358. * Set the visibility status of the workbook.
  1359. *
  1360. * Valid values are:
  1361. * - 'visible' (self::VISIBILITY_VISIBLE):
  1362. * Workbook window is visible
  1363. * - 'hidden' (self::VISIBILITY_HIDDEN):
  1364. * Workbook window is hidden, but can be shown by the user
  1365. * via the user interface
  1366. * - 'veryHidden' (self::VISIBILITY_VERY_HIDDEN):
  1367. * Workbook window is hidden and cannot be shown in the
  1368. * user interface.
  1369. *
  1370. * @param string $visibility visibility status of the workbook
  1371. */
  1372. public function setVisibility($visibility): void
  1373. {
  1374. if ($visibility === null) {
  1375. $visibility = self::VISIBILITY_VISIBLE;
  1376. }
  1377. if (in_array($visibility, self::$workbookViewVisibilityValues)) {
  1378. $this->visibility = $visibility;
  1379. } else {
  1380. throw new Exception('Invalid visibility value.');
  1381. }
  1382. }
  1383. /**
  1384. * Get the ratio between the workbook tabs bar and the horizontal scroll bar.
  1385. * TabRatio is assumed to be out of 1000 of the horizontal window width.
  1386. *
  1387. * @return int Ratio between the workbook tabs bar and the horizontal scroll bar
  1388. */
  1389. public function getTabRatio()
  1390. {
  1391. return $this->tabRatio;
  1392. }
  1393. /**
  1394. * Set the ratio between the workbook tabs bar and the horizontal scroll bar
  1395. * TabRatio is assumed to be out of 1000 of the horizontal window width.
  1396. *
  1397. * @param int $tabRatio Ratio between the tabs bar and the horizontal scroll bar
  1398. */
  1399. public function setTabRatio($tabRatio): void
  1400. {
  1401. if ($tabRatio >= 0 || $tabRatio <= 1000) {
  1402. $this->tabRatio = (int) $tabRatio;
  1403. } else {
  1404. throw new Exception('Tab ratio must be between 0 and 1000.');
  1405. }
  1406. }
  1407. }