Spreadsheet.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet;
  3. use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
  4. use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
  5. use PhpOffice\PhpSpreadsheet\Shared\File;
  6. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  7. use PhpOffice\PhpSpreadsheet\Style\Style;
  8. use PhpOffice\PhpSpreadsheet\Worksheet\Iterator;
  9. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  10. use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
  11. class Spreadsheet
  12. {
  13. // Allowable values for workbook window visilbity
  14. const VISIBILITY_VISIBLE = 'visible';
  15. const VISIBILITY_HIDDEN = 'hidden';
  16. const VISIBILITY_VERY_HIDDEN = 'veryHidden';
  17. private const DEFINED_NAME_IS_RANGE = false;
  18. private const DEFINED_NAME_IS_FORMULA = true;
  19. private static $workbookViewVisibilityValues = [
  20. self::VISIBILITY_VISIBLE,
  21. self::VISIBILITY_HIDDEN,
  22. self::VISIBILITY_VERY_HIDDEN,
  23. ];
  24. /**
  25. * Unique ID.
  26. *
  27. * @var string
  28. */
  29. private $uniqueID;
  30. /**
  31. * Document properties.
  32. *
  33. * @var Document\Properties
  34. */
  35. private $properties;
  36. /**
  37. * Document security.
  38. *
  39. * @var Document\Security
  40. */
  41. private $security;
  42. /**
  43. * Collection of Worksheet objects.
  44. *
  45. * @var Worksheet[]
  46. */
  47. private $workSheetCollection = [];
  48. /**
  49. * Calculation Engine.
  50. *
  51. * @var null|Calculation
  52. */
  53. private $calculationEngine;
  54. /**
  55. * Active sheet index.
  56. *
  57. * @var int
  58. */
  59. private $activeSheetIndex = 0;
  60. /**
  61. * Named ranges.
  62. *
  63. * @var DefinedName[]
  64. */
  65. private $definedNames = [];
  66. /**
  67. * CellXf supervisor.
  68. *
  69. * @var Style
  70. */
  71. private $cellXfSupervisor;
  72. /**
  73. * CellXf collection.
  74. *
  75. * @var Style[]
  76. */
  77. private $cellXfCollection = [];
  78. /**
  79. * CellStyleXf collection.
  80. *
  81. * @var Style[]
  82. */
  83. private $cellStyleXfCollection = [];
  84. /**
  85. * hasMacros : this workbook have macros ?
  86. *
  87. * @var bool
  88. */
  89. private $hasMacros = false;
  90. /**
  91. * macrosCode : all macros code as binary data (the vbaProject.bin file, this include form, code, etc.), null if no macro.
  92. *
  93. * @var null|string
  94. */
  95. private $macrosCode;
  96. /**
  97. * macrosCertificate : if macros are signed, contains binary data vbaProjectSignature.bin file, null if not signed.
  98. *
  99. * @var null|string
  100. */
  101. private $macrosCertificate;
  102. /**
  103. * ribbonXMLData : null if workbook is'nt Excel 2007 or not contain a customized UI.
  104. *
  105. * @var null|array{target: string, data: string}
  106. */
  107. private $ribbonXMLData;
  108. /**
  109. * ribbonBinObjects : null if workbook is'nt Excel 2007 or not contain embedded objects (picture(s)) for Ribbon Elements
  110. * ignored if $ribbonXMLData is null.
  111. *
  112. * @var null|array
  113. */
  114. private $ribbonBinObjects;
  115. /**
  116. * List of unparsed loaded data for export to same format with better compatibility.
  117. * It has to be minimized when the library start to support currently unparsed data.
  118. *
  119. * @var array
  120. */
  121. private $unparsedLoadedData = [];
  122. /**
  123. * Controls visibility of the horizonal scroll bar in the application.
  124. *
  125. * @var bool
  126. */
  127. private $showHorizontalScroll = true;
  128. /**
  129. * Controls visibility of the horizonal scroll bar in the application.
  130. *
  131. * @var bool
  132. */
  133. private $showVerticalScroll = true;
  134. /**
  135. * Controls visibility of the sheet tabs in the application.
  136. *
  137. * @var bool
  138. */
  139. private $showSheetTabs = true;
  140. /**
  141. * Specifies a boolean value that indicates whether the workbook window
  142. * is minimized.
  143. *
  144. * @var bool
  145. */
  146. private $minimized = false;
  147. /**
  148. * Specifies a boolean value that indicates whether to group dates
  149. * when presenting the user with filtering optiomd in the user
  150. * interface.
  151. *
  152. * @var bool
  153. */
  154. private $autoFilterDateGrouping = true;
  155. /**
  156. * Specifies the index to the first sheet in the book view.
  157. *
  158. * @var int
  159. */
  160. private $firstSheetIndex = 0;
  161. /**
  162. * Specifies the visible status of the workbook.
  163. *
  164. * @var string
  165. */
  166. private $visibility = self::VISIBILITY_VISIBLE;
  167. /**
  168. * Specifies the ratio between the workbook tabs bar and the horizontal
  169. * scroll bar. TabRatio is assumed to be out of 1000 of the horizontal
  170. * window width.
  171. *
  172. * @var int
  173. */
  174. private $tabRatio = 600;
  175. /**
  176. * The workbook has macros ?
  177. *
  178. * @return bool
  179. */
  180. public function hasMacros()
  181. {
  182. return $this->hasMacros;
  183. }
  184. /**
  185. * Define if a workbook has macros.
  186. *
  187. * @param bool $hasMacros true|false
  188. */
  189. public function setHasMacros($hasMacros): void
  190. {
  191. $this->hasMacros = (bool) $hasMacros;
  192. }
  193. /**
  194. * Set the macros code.
  195. *
  196. * @param string $macroCode string|null
  197. */
  198. public function setMacrosCode($macroCode): void
  199. {
  200. $this->macrosCode = $macroCode;
  201. $this->setHasMacros($macroCode !== null);
  202. }
  203. /**
  204. * Return the macros code.
  205. *
  206. * @return null|string
  207. */
  208. public function getMacrosCode()
  209. {
  210. return $this->macrosCode;
  211. }
  212. /**
  213. * Set the macros certificate.
  214. *
  215. * @param null|string $certificate
  216. */
  217. public function setMacrosCertificate($certificate): void
  218. {
  219. $this->macrosCertificate = $certificate;
  220. }
  221. /**
  222. * Is the project signed ?
  223. *
  224. * @return bool true|false
  225. */
  226. public function hasMacrosCertificate()
  227. {
  228. return $this->macrosCertificate !== null;
  229. }
  230. /**
  231. * Return the macros certificate.
  232. *
  233. * @return null|string
  234. */
  235. public function getMacrosCertificate()
  236. {
  237. return $this->macrosCertificate;
  238. }
  239. /**
  240. * Remove all macros, certificate from spreadsheet.
  241. */
  242. public function discardMacros(): void
  243. {
  244. $this->hasMacros = false;
  245. $this->macrosCode = null;
  246. $this->macrosCertificate = null;
  247. }
  248. /**
  249. * set ribbon XML data.
  250. *
  251. * @param null|mixed $target
  252. * @param null|mixed $xmlData
  253. */
  254. public function setRibbonXMLData($target, $xmlData): void
  255. {
  256. if ($target !== null && $xmlData !== null) {
  257. $this->ribbonXMLData = ['target' => $target, 'data' => $xmlData];
  258. } else {
  259. $this->ribbonXMLData = null;
  260. }
  261. }
  262. /**
  263. * retrieve ribbon XML Data.
  264. *
  265. * @param string $what
  266. *
  267. * @return null|array|string
  268. */
  269. public function getRibbonXMLData($what = 'all') //we need some constants here...
  270. {
  271. $returnData = null;
  272. $what = strtolower($what);
  273. switch ($what) {
  274. case 'all':
  275. $returnData = $this->ribbonXMLData;
  276. break;
  277. case 'target':
  278. case 'data':
  279. if (is_array($this->ribbonXMLData)) {
  280. $returnData = $this->ribbonXMLData[$what];
  281. }
  282. break;
  283. }
  284. return $returnData;
  285. }
  286. /**
  287. * store binaries ribbon objects (pictures).
  288. *
  289. * @param null|mixed $BinObjectsNames
  290. * @param null|mixed $BinObjectsData
  291. */
  292. public function setRibbonBinObjects($BinObjectsNames, $BinObjectsData): void
  293. {
  294. if ($BinObjectsNames !== null && $BinObjectsData !== null) {
  295. $this->ribbonBinObjects = ['names' => $BinObjectsNames, 'data' => $BinObjectsData];
  296. } else {
  297. $this->ribbonBinObjects = null;
  298. }
  299. }
  300. /**
  301. * List of unparsed loaded data for export to same format with better compatibility.
  302. * It has to be minimized when the library start to support currently unparsed data.
  303. *
  304. * @internal
  305. *
  306. * @return array
  307. */
  308. public function getUnparsedLoadedData()
  309. {
  310. return $this->unparsedLoadedData;
  311. }
  312. /**
  313. * List of unparsed loaded data for export to same format with better compatibility.
  314. * It has to be minimized when the library start to support currently unparsed data.
  315. *
  316. * @internal
  317. */
  318. public function setUnparsedLoadedData(array $unparsedLoadedData): void
  319. {
  320. $this->unparsedLoadedData = $unparsedLoadedData;
  321. }
  322. /**
  323. * return the extension of a filename. Internal use for a array_map callback (php<5.3 don't like lambda function).
  324. *
  325. * @param mixed $path
  326. *
  327. * @return string
  328. */
  329. private function getExtensionOnly($path)
  330. {
  331. $extension = pathinfo($path, PATHINFO_EXTENSION);
  332. return is_array($extension) ? '' : $extension;
  333. }
  334. /**
  335. * retrieve Binaries Ribbon Objects.
  336. *
  337. * @param string $what
  338. *
  339. * @return null|array
  340. */
  341. public function getRibbonBinObjects($what = 'all')
  342. {
  343. $ReturnData = null;
  344. $what = strtolower($what);
  345. switch ($what) {
  346. case 'all':
  347. return $this->ribbonBinObjects;
  348. break;
  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->getSheetByName($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->getParent()->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::$workbookViewVisibilityValues)) {
  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. }