SheetView.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  4. class SheetView
  5. {
  6. // Sheet View types
  7. const SHEETVIEW_NORMAL = 'normal';
  8. const SHEETVIEW_PAGE_LAYOUT = 'pageLayout';
  9. const SHEETVIEW_PAGE_BREAK_PREVIEW = 'pageBreakPreview';
  10. private const SHEET_VIEW_TYPES = [
  11. self::SHEETVIEW_NORMAL,
  12. self::SHEETVIEW_PAGE_LAYOUT,
  13. self::SHEETVIEW_PAGE_BREAK_PREVIEW,
  14. ];
  15. /**
  16. * ZoomScale.
  17. *
  18. * Valid values range from 10 to 400.
  19. *
  20. * @var ?int
  21. */
  22. private $zoomScale = 100;
  23. /**
  24. * ZoomScaleNormal.
  25. *
  26. * Valid values range from 10 to 400.
  27. *
  28. * @var ?int
  29. */
  30. private $zoomScaleNormal = 100;
  31. /**
  32. * ShowZeros.
  33. *
  34. * If true, "null" values from a calculation will be shown as "0". This is the default Excel behaviour and can be changed
  35. * with the advanced worksheet option "Show a zero in cells that have zero value"
  36. *
  37. * @var bool
  38. */
  39. private $showZeros = true;
  40. /**
  41. * View.
  42. *
  43. * Valid values range from 10 to 400.
  44. *
  45. * @var string
  46. */
  47. private $sheetviewType = self::SHEETVIEW_NORMAL;
  48. /**
  49. * Create a new SheetView.
  50. */
  51. public function __construct()
  52. {
  53. }
  54. /**
  55. * Get ZoomScale.
  56. *
  57. * @return ?int
  58. */
  59. public function getZoomScale()
  60. {
  61. return $this->zoomScale;
  62. }
  63. /**
  64. * Set ZoomScale.
  65. * Valid values range from 10 to 400.
  66. *
  67. * @param ?int $zoomScale
  68. *
  69. * @return $this
  70. */
  71. public function setZoomScale($zoomScale)
  72. {
  73. // Microsoft Office Excel 2007 only allows setting a scale between 10 and 400 via the user interface,
  74. // but it is apparently still able to handle any scale >= 1
  75. if ($zoomScale === null || $zoomScale >= 1) {
  76. $this->zoomScale = $zoomScale;
  77. } else {
  78. throw new PhpSpreadsheetException('Scale must be greater than or equal to 1.');
  79. }
  80. return $this;
  81. }
  82. /**
  83. * Get ZoomScaleNormal.
  84. *
  85. * @return ?int
  86. */
  87. public function getZoomScaleNormal()
  88. {
  89. return $this->zoomScaleNormal;
  90. }
  91. /**
  92. * Set ZoomScale.
  93. * Valid values range from 10 to 400.
  94. *
  95. * @param ?int $zoomScaleNormal
  96. *
  97. * @return $this
  98. */
  99. public function setZoomScaleNormal($zoomScaleNormal)
  100. {
  101. if ($zoomScaleNormal === null || $zoomScaleNormal >= 1) {
  102. $this->zoomScaleNormal = $zoomScaleNormal;
  103. } else {
  104. throw new PhpSpreadsheetException('Scale must be greater than or equal to 1.');
  105. }
  106. return $this;
  107. }
  108. /**
  109. * Set ShowZeroes setting.
  110. *
  111. * @param bool $showZeros
  112. */
  113. public function setShowZeros($showZeros): void
  114. {
  115. $this->showZeros = $showZeros;
  116. }
  117. /**
  118. * @return bool
  119. */
  120. public function getShowZeros()
  121. {
  122. return $this->showZeros;
  123. }
  124. /**
  125. * Get View.
  126. *
  127. * @return string
  128. */
  129. public function getView()
  130. {
  131. return $this->sheetviewType;
  132. }
  133. /**
  134. * Set View.
  135. *
  136. * Valid values are
  137. * 'normal' self::SHEETVIEW_NORMAL
  138. * 'pageLayout' self::SHEETVIEW_PAGE_LAYOUT
  139. * 'pageBreakPreview' self::SHEETVIEW_PAGE_BREAK_PREVIEW
  140. *
  141. * @param ?string $sheetViewType
  142. *
  143. * @return $this
  144. */
  145. public function setView($sheetViewType)
  146. {
  147. // MS Excel 2007 allows setting the view to 'normal', 'pageLayout' or 'pageBreakPreview' via the user interface
  148. if ($sheetViewType === null) {
  149. $sheetViewType = self::SHEETVIEW_NORMAL;
  150. }
  151. if (in_array($sheetViewType, self::SHEET_VIEW_TYPES)) {
  152. $this->sheetviewType = $sheetViewType;
  153. } else {
  154. throw new PhpSpreadsheetException('Invalid sheetview layout type.');
  155. }
  156. return $this;
  157. }
  158. }