Spreadsheet.php 44 KB

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