PageSetup.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  4. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  5. /**
  6. * <code>
  7. * Paper size taken from Office Open XML Part 4 - Markup Language Reference, page 1988:.
  8. *
  9. * 1 = Letter paper (8.5 in. by 11 in.)
  10. * 2 = Letter small paper (8.5 in. by 11 in.)
  11. * 3 = Tabloid paper (11 in. by 17 in.)
  12. * 4 = Ledger paper (17 in. by 11 in.)
  13. * 5 = Legal paper (8.5 in. by 14 in.)
  14. * 6 = Statement paper (5.5 in. by 8.5 in.)
  15. * 7 = Executive paper (7.25 in. by 10.5 in.)
  16. * 8 = A3 paper (297 mm by 420 mm)
  17. * 9 = A4 paper (210 mm by 297 mm)
  18. * 10 = A4 small paper (210 mm by 297 mm)
  19. * 11 = A5 paper (148 mm by 210 mm)
  20. * 12 = B4 paper (250 mm by 353 mm)
  21. * 13 = B5 paper (176 mm by 250 mm)
  22. * 14 = Folio paper (8.5 in. by 13 in.)
  23. * 15 = Quarto paper (215 mm by 275 mm)
  24. * 16 = Standard paper (10 in. by 14 in.)
  25. * 17 = Standard paper (11 in. by 17 in.)
  26. * 18 = Note paper (8.5 in. by 11 in.)
  27. * 19 = #9 envelope (3.875 in. by 8.875 in.)
  28. * 20 = #10 envelope (4.125 in. by 9.5 in.)
  29. * 21 = #11 envelope (4.5 in. by 10.375 in.)
  30. * 22 = #12 envelope (4.75 in. by 11 in.)
  31. * 23 = #14 envelope (5 in. by 11.5 in.)
  32. * 24 = C paper (17 in. by 22 in.)
  33. * 25 = D paper (22 in. by 34 in.)
  34. * 26 = E paper (34 in. by 44 in.)
  35. * 27 = DL envelope (110 mm by 220 mm)
  36. * 28 = C5 envelope (162 mm by 229 mm)
  37. * 29 = C3 envelope (324 mm by 458 mm)
  38. * 30 = C4 envelope (229 mm by 324 mm)
  39. * 31 = C6 envelope (114 mm by 162 mm)
  40. * 32 = C65 envelope (114 mm by 229 mm)
  41. * 33 = B4 envelope (250 mm by 353 mm)
  42. * 34 = B5 envelope (176 mm by 250 mm)
  43. * 35 = B6 envelope (176 mm by 125 mm)
  44. * 36 = Italy envelope (110 mm by 230 mm)
  45. * 37 = Monarch envelope (3.875 in. by 7.5 in.).
  46. * 38 = 6 3/4 envelope (3.625 in. by 6.5 in.)
  47. * 39 = US standard fanfold (14.875 in. by 11 in.)
  48. * 40 = German standard fanfold (8.5 in. by 12 in.)
  49. * 41 = German legal fanfold (8.5 in. by 13 in.)
  50. * 42 = ISO B4 (250 mm by 353 mm)
  51. * 43 = Japanese double postcard (200 mm by 148 mm)
  52. * 44 = Standard paper (9 in. by 11 in.)
  53. * 45 = Standard paper (10 in. by 11 in.)
  54. * 46 = Standard paper (15 in. by 11 in.)
  55. * 47 = Invite envelope (220 mm by 220 mm)
  56. * 50 = Letter extra paper (9.275 in. by 12 in.)
  57. * 51 = Legal extra paper (9.275 in. by 15 in.)
  58. * 52 = Tabloid extra paper (11.69 in. by 18 in.)
  59. * 53 = A4 extra paper (236 mm by 322 mm)
  60. * 54 = Letter transverse paper (8.275 in. by 11 in.)
  61. * 55 = A4 transverse paper (210 mm by 297 mm)
  62. * 56 = Letter extra transverse paper (9.275 in. by 12 in.)
  63. * 57 = SuperA/SuperA/A4 paper (227 mm by 356 mm)
  64. * 58 = SuperB/SuperB/A3 paper (305 mm by 487 mm)
  65. * 59 = Letter plus paper (8.5 in. by 12.69 in.)
  66. * 60 = A4 plus paper (210 mm by 330 mm)
  67. * 61 = A5 transverse paper (148 mm by 210 mm)
  68. * 62 = JIS B5 transverse paper (182 mm by 257 mm)
  69. * 63 = A3 extra paper (322 mm by 445 mm)
  70. * 64 = A5 extra paper (174 mm by 235 mm)
  71. * 65 = ISO B5 extra paper (201 mm by 276 mm)
  72. * 66 = A2 paper (420 mm by 594 mm)
  73. * 67 = A3 transverse paper (297 mm by 420 mm)
  74. * 68 = A3 extra transverse paper (322 mm by 445 mm)
  75. * </code>
  76. */
  77. class PageSetup
  78. {
  79. // Paper size
  80. const PAPERSIZE_LETTER = 1;
  81. const PAPERSIZE_LETTER_SMALL = 2;
  82. const PAPERSIZE_TABLOID = 3;
  83. const PAPERSIZE_LEDGER = 4;
  84. const PAPERSIZE_LEGAL = 5;
  85. const PAPERSIZE_STATEMENT = 6;
  86. const PAPERSIZE_EXECUTIVE = 7;
  87. const PAPERSIZE_A3 = 8;
  88. const PAPERSIZE_A4 = 9;
  89. const PAPERSIZE_A4_SMALL = 10;
  90. const PAPERSIZE_A5 = 11;
  91. const PAPERSIZE_B4 = 12;
  92. const PAPERSIZE_B5 = 13;
  93. const PAPERSIZE_FOLIO = 14;
  94. const PAPERSIZE_QUARTO = 15;
  95. const PAPERSIZE_STANDARD_1 = 16;
  96. const PAPERSIZE_STANDARD_2 = 17;
  97. const PAPERSIZE_NOTE = 18;
  98. const PAPERSIZE_NO9_ENVELOPE = 19;
  99. const PAPERSIZE_NO10_ENVELOPE = 20;
  100. const PAPERSIZE_NO11_ENVELOPE = 21;
  101. const PAPERSIZE_NO12_ENVELOPE = 22;
  102. const PAPERSIZE_NO14_ENVELOPE = 23;
  103. const PAPERSIZE_C = 24;
  104. const PAPERSIZE_D = 25;
  105. const PAPERSIZE_E = 26;
  106. const PAPERSIZE_DL_ENVELOPE = 27;
  107. const PAPERSIZE_C5_ENVELOPE = 28;
  108. const PAPERSIZE_C3_ENVELOPE = 29;
  109. const PAPERSIZE_C4_ENVELOPE = 30;
  110. const PAPERSIZE_C6_ENVELOPE = 31;
  111. const PAPERSIZE_C65_ENVELOPE = 32;
  112. const PAPERSIZE_B4_ENVELOPE = 33;
  113. const PAPERSIZE_B5_ENVELOPE = 34;
  114. const PAPERSIZE_B6_ENVELOPE = 35;
  115. const PAPERSIZE_ITALY_ENVELOPE = 36;
  116. const PAPERSIZE_MONARCH_ENVELOPE = 37;
  117. const PAPERSIZE_6_3_4_ENVELOPE = 38;
  118. const PAPERSIZE_US_STANDARD_FANFOLD = 39;
  119. const PAPERSIZE_GERMAN_STANDARD_FANFOLD = 40;
  120. const PAPERSIZE_GERMAN_LEGAL_FANFOLD = 41;
  121. const PAPERSIZE_ISO_B4 = 42;
  122. const PAPERSIZE_JAPANESE_DOUBLE_POSTCARD = 43;
  123. const PAPERSIZE_STANDARD_PAPER_1 = 44;
  124. const PAPERSIZE_STANDARD_PAPER_2 = 45;
  125. const PAPERSIZE_STANDARD_PAPER_3 = 46;
  126. const PAPERSIZE_INVITE_ENVELOPE = 47;
  127. const PAPERSIZE_LETTER_EXTRA_PAPER = 48;
  128. const PAPERSIZE_LEGAL_EXTRA_PAPER = 49;
  129. const PAPERSIZE_TABLOID_EXTRA_PAPER = 50;
  130. const PAPERSIZE_A4_EXTRA_PAPER = 51;
  131. const PAPERSIZE_LETTER_TRANSVERSE_PAPER = 52;
  132. const PAPERSIZE_A4_TRANSVERSE_PAPER = 53;
  133. const PAPERSIZE_LETTER_EXTRA_TRANSVERSE_PAPER = 54;
  134. const PAPERSIZE_SUPERA_SUPERA_A4_PAPER = 55;
  135. const PAPERSIZE_SUPERB_SUPERB_A3_PAPER = 56;
  136. const PAPERSIZE_LETTER_PLUS_PAPER = 57;
  137. const PAPERSIZE_A4_PLUS_PAPER = 58;
  138. const PAPERSIZE_A5_TRANSVERSE_PAPER = 59;
  139. const PAPERSIZE_JIS_B5_TRANSVERSE_PAPER = 60;
  140. const PAPERSIZE_A3_EXTRA_PAPER = 61;
  141. const PAPERSIZE_A5_EXTRA_PAPER = 62;
  142. const PAPERSIZE_ISO_B5_EXTRA_PAPER = 63;
  143. const PAPERSIZE_A2_PAPER = 64;
  144. const PAPERSIZE_A3_TRANSVERSE_PAPER = 65;
  145. const PAPERSIZE_A3_EXTRA_TRANSVERSE_PAPER = 66;
  146. // Page orientation
  147. const ORIENTATION_DEFAULT = 'default';
  148. const ORIENTATION_LANDSCAPE = 'landscape';
  149. const ORIENTATION_PORTRAIT = 'portrait';
  150. // Print Range Set Method
  151. const SETPRINTRANGE_OVERWRITE = 'O';
  152. const SETPRINTRANGE_INSERT = 'I';
  153. const PAGEORDER_OVER_THEN_DOWN = 'overThenDown';
  154. const PAGEORDER_DOWN_THEN_OVER = 'downThenOver';
  155. /**
  156. * Paper size default.
  157. *
  158. * @var int
  159. */
  160. private static $paperSizeDefault = self::PAPERSIZE_LETTER;
  161. /**
  162. * Paper size.
  163. *
  164. * @var ?int
  165. */
  166. private $paperSize;
  167. /**
  168. * Orientation default.
  169. *
  170. * @var string
  171. */
  172. private static $orientationDefault = self::ORIENTATION_DEFAULT;
  173. /**
  174. * Orientation.
  175. *
  176. * @var string
  177. */
  178. private $orientation;
  179. /**
  180. * Scale (Print Scale).
  181. *
  182. * Print scaling. Valid values range from 10 to 400
  183. * This setting is overridden when fitToWidth and/or fitToHeight are in use
  184. *
  185. * @var null|int
  186. */
  187. private $scale = 100;
  188. /**
  189. * Fit To Page
  190. * Whether scale or fitToWith / fitToHeight applies.
  191. *
  192. * @var bool
  193. */
  194. private $fitToPage = false;
  195. /**
  196. * Fit To Height
  197. * Number of vertical pages to fit on.
  198. *
  199. * @var null|int
  200. */
  201. private $fitToHeight = 1;
  202. /**
  203. * Fit To Width
  204. * Number of horizontal pages to fit on.
  205. *
  206. * @var null|int
  207. */
  208. private $fitToWidth = 1;
  209. /**
  210. * Columns to repeat at left.
  211. *
  212. * @var array Containing start column and end column, empty array if option unset
  213. */
  214. private $columnsToRepeatAtLeft = ['', ''];
  215. /**
  216. * Rows to repeat at top.
  217. *
  218. * @var array Containing start row number and end row number, empty array if option unset
  219. */
  220. private $rowsToRepeatAtTop = [0, 0];
  221. /**
  222. * Center page horizontally.
  223. *
  224. * @var bool
  225. */
  226. private $horizontalCentered = false;
  227. /**
  228. * Center page vertically.
  229. *
  230. * @var bool
  231. */
  232. private $verticalCentered = false;
  233. /**
  234. * Print area.
  235. *
  236. * @var null|string
  237. */
  238. private $printArea;
  239. /**
  240. * First page number.
  241. *
  242. * @var ?int
  243. */
  244. private $firstPageNumber;
  245. /** @var string */
  246. private $pageOrder = self::PAGEORDER_DOWN_THEN_OVER;
  247. /**
  248. * Create a new PageSetup.
  249. */
  250. public function __construct()
  251. {
  252. $this->orientation = self::$orientationDefault;
  253. }
  254. /**
  255. * Get Paper Size.
  256. *
  257. * @return int
  258. */
  259. public function getPaperSize()
  260. {
  261. return $this->paperSize ?? self::$paperSizeDefault;
  262. }
  263. /**
  264. * Set Paper Size.
  265. *
  266. * @param int $paperSize see self::PAPERSIZE_*
  267. *
  268. * @return $this
  269. */
  270. public function setPaperSize($paperSize)
  271. {
  272. $this->paperSize = $paperSize;
  273. return $this;
  274. }
  275. /**
  276. * Get Paper Size default.
  277. */
  278. public static function getPaperSizeDefault(): int
  279. {
  280. return self::$paperSizeDefault;
  281. }
  282. /**
  283. * Set Paper Size Default.
  284. */
  285. public static function setPaperSizeDefault(int $paperSize): void
  286. {
  287. self::$paperSizeDefault = $paperSize;
  288. }
  289. /**
  290. * Get Orientation.
  291. *
  292. * @return string
  293. */
  294. public function getOrientation()
  295. {
  296. return $this->orientation;
  297. }
  298. /**
  299. * Set Orientation.
  300. *
  301. * @param string $orientation see self::ORIENTATION_*
  302. *
  303. * @return $this
  304. */
  305. public function setOrientation($orientation)
  306. {
  307. if ($orientation === self::ORIENTATION_LANDSCAPE || $orientation === self::ORIENTATION_PORTRAIT || $orientation === self::ORIENTATION_DEFAULT) {
  308. $this->orientation = $orientation;
  309. }
  310. return $this;
  311. }
  312. public static function getOrientationDefault(): string
  313. {
  314. return self::$orientationDefault;
  315. }
  316. public static function setOrientationDefault(string $orientation): void
  317. {
  318. if ($orientation === self::ORIENTATION_LANDSCAPE || $orientation === self::ORIENTATION_PORTRAIT || $orientation === self::ORIENTATION_DEFAULT) {
  319. self::$orientationDefault = $orientation;
  320. }
  321. }
  322. /**
  323. * Get Scale.
  324. *
  325. * @return null|int
  326. */
  327. public function getScale()
  328. {
  329. return $this->scale;
  330. }
  331. /**
  332. * Set Scale.
  333. * Print scaling. Valid values range from 10 to 400
  334. * This setting is overridden when fitToWidth and/or fitToHeight are in use.
  335. *
  336. * @param null|int $scale
  337. * @param bool $update Update fitToPage so scaling applies rather than fitToHeight / fitToWidth
  338. *
  339. * @return $this
  340. */
  341. public function setScale($scale, $update = true)
  342. {
  343. // Microsoft Office Excel 2007 only allows setting a scale between 10 and 400 via the user interface,
  344. // but it is apparently still able to handle any scale >= 0, where 0 results in 100
  345. if ($scale === null || $scale >= 0) {
  346. $this->scale = $scale;
  347. if ($update) {
  348. $this->fitToPage = false;
  349. }
  350. } else {
  351. throw new PhpSpreadsheetException('Scale must not be negative');
  352. }
  353. return $this;
  354. }
  355. /**
  356. * Get Fit To Page.
  357. *
  358. * @return bool
  359. */
  360. public function getFitToPage()
  361. {
  362. return $this->fitToPage;
  363. }
  364. /**
  365. * Set Fit To Page.
  366. *
  367. * @param bool $fitToPage
  368. *
  369. * @return $this
  370. */
  371. public function setFitToPage($fitToPage)
  372. {
  373. $this->fitToPage = $fitToPage;
  374. return $this;
  375. }
  376. /**
  377. * Get Fit To Height.
  378. *
  379. * @return null|int
  380. */
  381. public function getFitToHeight()
  382. {
  383. return $this->fitToHeight;
  384. }
  385. /**
  386. * Set Fit To Height.
  387. *
  388. * @param null|int $fitToHeight
  389. * @param bool $update Update fitToPage so it applies rather than scaling
  390. *
  391. * @return $this
  392. */
  393. public function setFitToHeight($fitToHeight, $update = true)
  394. {
  395. $this->fitToHeight = $fitToHeight;
  396. if ($update) {
  397. $this->fitToPage = true;
  398. }
  399. return $this;
  400. }
  401. /**
  402. * Get Fit To Width.
  403. *
  404. * @return null|int
  405. */
  406. public function getFitToWidth()
  407. {
  408. return $this->fitToWidth;
  409. }
  410. /**
  411. * Set Fit To Width.
  412. *
  413. * @param null|int $value
  414. * @param bool $update Update fitToPage so it applies rather than scaling
  415. *
  416. * @return $this
  417. */
  418. public function setFitToWidth($value, $update = true)
  419. {
  420. $this->fitToWidth = $value;
  421. if ($update) {
  422. $this->fitToPage = true;
  423. }
  424. return $this;
  425. }
  426. /**
  427. * Is Columns to repeat at left set?
  428. *
  429. * @return bool
  430. */
  431. public function isColumnsToRepeatAtLeftSet()
  432. {
  433. if (!empty($this->columnsToRepeatAtLeft)) {
  434. if ($this->columnsToRepeatAtLeft[0] != '' && $this->columnsToRepeatAtLeft[1] != '') {
  435. return true;
  436. }
  437. }
  438. return false;
  439. }
  440. /**
  441. * Get Columns to repeat at left.
  442. *
  443. * @return array Containing start column and end column, empty array if option unset
  444. */
  445. public function getColumnsToRepeatAtLeft()
  446. {
  447. return $this->columnsToRepeatAtLeft;
  448. }
  449. /**
  450. * Set Columns to repeat at left.
  451. *
  452. * @param array $columnsToRepeatAtLeft Containing start column and end column, empty array if option unset
  453. *
  454. * @return $this
  455. */
  456. public function setColumnsToRepeatAtLeft(array $columnsToRepeatAtLeft)
  457. {
  458. $this->columnsToRepeatAtLeft = $columnsToRepeatAtLeft;
  459. return $this;
  460. }
  461. /**
  462. * Set Columns to repeat at left by start and end.
  463. *
  464. * @param string $start eg: 'A'
  465. * @param string $end eg: 'B'
  466. *
  467. * @return $this
  468. */
  469. public function setColumnsToRepeatAtLeftByStartAndEnd($start, $end)
  470. {
  471. $this->columnsToRepeatAtLeft = [$start, $end];
  472. return $this;
  473. }
  474. /**
  475. * Is Rows to repeat at top set?
  476. *
  477. * @return bool
  478. */
  479. public function isRowsToRepeatAtTopSet()
  480. {
  481. if (!empty($this->rowsToRepeatAtTop)) {
  482. if ($this->rowsToRepeatAtTop[0] != 0 && $this->rowsToRepeatAtTop[1] != 0) {
  483. return true;
  484. }
  485. }
  486. return false;
  487. }
  488. /**
  489. * Get Rows to repeat at top.
  490. *
  491. * @return array Containing start column and end column, empty array if option unset
  492. */
  493. public function getRowsToRepeatAtTop()
  494. {
  495. return $this->rowsToRepeatAtTop;
  496. }
  497. /**
  498. * Set Rows to repeat at top.
  499. *
  500. * @param array $rowsToRepeatAtTop Containing start column and end column, empty array if option unset
  501. *
  502. * @return $this
  503. */
  504. public function setRowsToRepeatAtTop(array $rowsToRepeatAtTop)
  505. {
  506. $this->rowsToRepeatAtTop = $rowsToRepeatAtTop;
  507. return $this;
  508. }
  509. /**
  510. * Set Rows to repeat at top by start and end.
  511. *
  512. * @param int $start eg: 1
  513. * @param int $end eg: 1
  514. *
  515. * @return $this
  516. */
  517. public function setRowsToRepeatAtTopByStartAndEnd($start, $end)
  518. {
  519. $this->rowsToRepeatAtTop = [$start, $end];
  520. return $this;
  521. }
  522. /**
  523. * Get center page horizontally.
  524. *
  525. * @return bool
  526. */
  527. public function getHorizontalCentered()
  528. {
  529. return $this->horizontalCentered;
  530. }
  531. /**
  532. * Set center page horizontally.
  533. *
  534. * @param bool $value
  535. *
  536. * @return $this
  537. */
  538. public function setHorizontalCentered($value)
  539. {
  540. $this->horizontalCentered = $value;
  541. return $this;
  542. }
  543. /**
  544. * Get center page vertically.
  545. *
  546. * @return bool
  547. */
  548. public function getVerticalCentered()
  549. {
  550. return $this->verticalCentered;
  551. }
  552. /**
  553. * Set center page vertically.
  554. *
  555. * @param bool $value
  556. *
  557. * @return $this
  558. */
  559. public function setVerticalCentered($value)
  560. {
  561. $this->verticalCentered = $value;
  562. return $this;
  563. }
  564. /**
  565. * Get print area.
  566. *
  567. * @param int $index Identifier for a specific print area range if several ranges have been set
  568. * Default behaviour, or a index value of 0, will return all ranges as a comma-separated string
  569. * Otherwise, the specific range identified by the value of $index will be returned
  570. * Print areas are numbered from 1
  571. *
  572. * @return string
  573. */
  574. public function getPrintArea($index = 0)
  575. {
  576. if ($index == 0) {
  577. return (string) $this->printArea;
  578. }
  579. $printAreas = explode(',', (string) $this->printArea);
  580. if (isset($printAreas[$index - 1])) {
  581. return $printAreas[$index - 1];
  582. }
  583. throw new PhpSpreadsheetException('Requested Print Area does not exist');
  584. }
  585. /**
  586. * Is print area set?
  587. *
  588. * @param int $index Identifier for a specific print area range if several ranges have been set
  589. * Default behaviour, or an index value of 0, will identify whether any print range is set
  590. * Otherwise, existence of the range identified by the value of $index will be returned
  591. * Print areas are numbered from 1
  592. *
  593. * @return bool
  594. */
  595. public function isPrintAreaSet($index = 0)
  596. {
  597. if ($index == 0) {
  598. return $this->printArea !== null;
  599. }
  600. $printAreas = explode(',', (string) $this->printArea);
  601. return isset($printAreas[$index - 1]);
  602. }
  603. /**
  604. * Clear a print area.
  605. *
  606. * @param int $index Identifier for a specific print area range if several ranges have been set
  607. * Default behaviour, or an index value of 0, will clear all print ranges that are set
  608. * Otherwise, the range identified by the value of $index will be removed from the series
  609. * Print areas are numbered from 1
  610. *
  611. * @return $this
  612. */
  613. public function clearPrintArea($index = 0)
  614. {
  615. if ($index == 0) {
  616. $this->printArea = null;
  617. } else {
  618. $printAreas = explode(',', (string) $this->printArea);
  619. if (isset($printAreas[$index - 1])) {
  620. unset($printAreas[$index - 1]);
  621. $this->printArea = implode(',', $printAreas);
  622. }
  623. }
  624. return $this;
  625. }
  626. /**
  627. * Set print area. e.g. 'A1:D10' or 'A1:D10,G5:M20'.
  628. *
  629. * @param string $value
  630. * @param int $index Identifier for a specific print area range allowing several ranges to be set
  631. * When the method is "O"verwrite, then a positive integer index will overwrite that indexed
  632. * entry in the print areas list; a negative index value will identify which entry to
  633. * overwrite working bacward through the print area to the list, with the last entry as -1.
  634. * Specifying an index value of 0, will overwrite <b>all</b> existing print ranges.
  635. * When the method is "I"nsert, then a positive index will insert after that indexed entry in
  636. * the print areas list, while a negative index will insert before the indexed entry.
  637. * Specifying an index value of 0, will always append the new print range at the end of the
  638. * list.
  639. * Print areas are numbered from 1
  640. * @param string $method Determines the method used when setting multiple print areas
  641. * Default behaviour, or the "O" method, overwrites existing print area
  642. * The "I" method, inserts the new print area before any specified index, or at the end of the list
  643. *
  644. * @return $this
  645. */
  646. public function setPrintArea($value, $index = 0, $method = self::SETPRINTRANGE_OVERWRITE)
  647. {
  648. if (strpos($value, '!') !== false) {
  649. throw new PhpSpreadsheetException('Cell coordinate must not specify a worksheet.');
  650. } elseif (strpos($value, ':') === false) {
  651. throw new PhpSpreadsheetException('Cell coordinate must be a range of cells.');
  652. } elseif (strpos($value, '$') !== false) {
  653. throw new PhpSpreadsheetException('Cell coordinate must not be absolute.');
  654. }
  655. $value = strtoupper($value);
  656. if (!$this->printArea) {
  657. $index = 0;
  658. }
  659. if ($method == self::SETPRINTRANGE_OVERWRITE) {
  660. if ($index == 0) {
  661. $this->printArea = $value;
  662. } else {
  663. $printAreas = explode(',', (string) $this->printArea);
  664. if ($index < 0) {
  665. $index = count($printAreas) - abs($index) + 1;
  666. }
  667. if (($index <= 0) || ($index > count($printAreas))) {
  668. throw new PhpSpreadsheetException('Invalid index for setting print range.');
  669. }
  670. $printAreas[$index - 1] = $value;
  671. $this->printArea = implode(',', $printAreas);
  672. }
  673. } elseif ($method == self::SETPRINTRANGE_INSERT) {
  674. if ($index == 0) {
  675. $this->printArea = $this->printArea ? ($this->printArea . ',' . $value) : $value;
  676. } else {
  677. $printAreas = explode(',', (string) $this->printArea);
  678. if ($index < 0) {
  679. $index = (int) abs($index) - 1;
  680. }
  681. if ($index > count($printAreas)) {
  682. throw new PhpSpreadsheetException('Invalid index for setting print range.');
  683. }
  684. $printAreas = array_merge(array_slice($printAreas, 0, $index), [$value], array_slice($printAreas, $index));
  685. $this->printArea = implode(',', $printAreas);
  686. }
  687. } else {
  688. throw new PhpSpreadsheetException('Invalid method for setting print range.');
  689. }
  690. return $this;
  691. }
  692. /**
  693. * Add a new print area (e.g. 'A1:D10' or 'A1:D10,G5:M20') to the list of print areas.
  694. *
  695. * @param string $value
  696. * @param int $index Identifier for a specific print area range allowing several ranges to be set
  697. * A positive index will insert after that indexed entry in the print areas list, while a
  698. * negative index will insert before the indexed entry.
  699. * Specifying an index value of 0, will always append the new print range at the end of the
  700. * list.
  701. * Print areas are numbered from 1
  702. *
  703. * @return $this
  704. */
  705. public function addPrintArea($value, $index = -1)
  706. {
  707. return $this->setPrintArea($value, $index, self::SETPRINTRANGE_INSERT);
  708. }
  709. /**
  710. * Set print area.
  711. *
  712. * @param int $column1 Column 1
  713. * @param int $row1 Row 1
  714. * @param int $column2 Column 2
  715. * @param int $row2 Row 2
  716. * @param int $index Identifier for a specific print area range allowing several ranges to be set
  717. * When the method is "O"verwrite, then a positive integer index will overwrite that indexed
  718. * entry in the print areas list; a negative index value will identify which entry to
  719. * overwrite working backward through the print area to the list, with the last entry as -1.
  720. * Specifying an index value of 0, will overwrite <b>all</b> existing print ranges.
  721. * When the method is "I"nsert, then a positive index will insert after that indexed entry in
  722. * the print areas list, while a negative index will insert before the indexed entry.
  723. * Specifying an index value of 0, will always append the new print range at the end of the
  724. * list.
  725. * Print areas are numbered from 1
  726. * @param string $method Determines the method used when setting multiple print areas
  727. * Default behaviour, or the "O" method, overwrites existing print area
  728. * The "I" method, inserts the new print area before any specified index, or at the end of the list
  729. *
  730. * @return $this
  731. */
  732. public function setPrintAreaByColumnAndRow($column1, $row1, $column2, $row2, $index = 0, $method = self::SETPRINTRANGE_OVERWRITE)
  733. {
  734. return $this->setPrintArea(
  735. Coordinate::stringFromColumnIndex($column1) . $row1 . ':' . Coordinate::stringFromColumnIndex($column2) . $row2,
  736. $index,
  737. $method
  738. );
  739. }
  740. /**
  741. * Add a new print area to the list of print areas.
  742. *
  743. * @param int $column1 Start Column for the print area
  744. * @param int $row1 Start Row for the print area
  745. * @param int $column2 End Column for the print area
  746. * @param int $row2 End Row for the print area
  747. * @param int $index Identifier for a specific print area range allowing several ranges to be set
  748. * A positive index will insert after that indexed entry in the print areas list, while a
  749. * negative index will insert before the indexed entry.
  750. * Specifying an index value of 0, will always append the new print range at the end of the
  751. * list.
  752. * Print areas are numbered from 1
  753. *
  754. * @return $this
  755. */
  756. public function addPrintAreaByColumnAndRow($column1, $row1, $column2, $row2, $index = -1)
  757. {
  758. return $this->setPrintArea(
  759. Coordinate::stringFromColumnIndex($column1) . $row1 . ':' . Coordinate::stringFromColumnIndex($column2) . $row2,
  760. $index,
  761. self::SETPRINTRANGE_INSERT
  762. );
  763. }
  764. /**
  765. * Get first page number.
  766. *
  767. * @return ?int
  768. */
  769. public function getFirstPageNumber()
  770. {
  771. return $this->firstPageNumber;
  772. }
  773. /**
  774. * Set first page number.
  775. *
  776. * @param ?int $value
  777. *
  778. * @return $this
  779. */
  780. public function setFirstPageNumber($value)
  781. {
  782. $this->firstPageNumber = $value;
  783. return $this;
  784. }
  785. /**
  786. * Reset first page number.
  787. *
  788. * @return $this
  789. */
  790. public function resetFirstPageNumber()
  791. {
  792. return $this->setFirstPageNumber(null);
  793. }
  794. public function getPageOrder(): string
  795. {
  796. return $this->pageOrder;
  797. }
  798. public function setPageOrder(?string $pageOrder): self
  799. {
  800. if ($pageOrder === null || $pageOrder === self::PAGEORDER_DOWN_THEN_OVER || $pageOrder === self::PAGEORDER_OVER_THEN_DOWN) {
  801. $this->pageOrder = $pageOrder ?? self::PAGEORDER_DOWN_THEN_OVER;
  802. }
  803. return $this;
  804. }
  805. }