Spreadsheet.php 44 KB

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