PageSetup.php 25 KB

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