BaseWriter.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Writer;
  3. abstract class BaseWriter implements IWriter
  4. {
  5. /**
  6. * Write charts that are defined in the workbook?
  7. * Identifies whether the Writer should write definitions for any charts that exist in the PhpSpreadsheet object;.
  8. *
  9. * @var bool
  10. */
  11. protected $includeCharts = false;
  12. /**
  13. * Pre-calculate formulas
  14. * Forces PhpSpreadsheet to recalculate all formulae in a workbook when saving, so that the pre-calculated values are
  15. * immediately available to MS Excel or other office spreadsheet viewer when opening the file.
  16. *
  17. * @var bool
  18. */
  19. protected $preCalculateFormulas = true;
  20. /**
  21. * Use disk caching where possible?
  22. *
  23. * @var bool
  24. */
  25. private $useDiskCaching = false;
  26. /**
  27. * Disk caching directory.
  28. *
  29. * @var string
  30. */
  31. private $diskCachingDirectory = './';
  32. /**
  33. * Write charts in workbook?
  34. * If this is true, then the Writer will write definitions for any charts that exist in the PhpSpreadsheet object.
  35. * If false (the default) it will ignore any charts defined in the PhpSpreadsheet object.
  36. *
  37. * @return bool
  38. */
  39. public function getIncludeCharts()
  40. {
  41. return $this->includeCharts;
  42. }
  43. /**
  44. * Set write charts in workbook
  45. * Set to true, to advise the Writer to include any charts that exist in the PhpSpreadsheet object.
  46. * Set to false (the default) to ignore charts.
  47. *
  48. * @param bool $pValue
  49. *
  50. * @return IWriter
  51. */
  52. public function setIncludeCharts($pValue)
  53. {
  54. $this->includeCharts = (bool) $pValue;
  55. return $this;
  56. }
  57. /**
  58. * Get Pre-Calculate Formulas flag
  59. * If this is true (the default), then the writer will recalculate all formulae in a workbook when saving,
  60. * so that the pre-calculated values are immediately available to MS Excel or other office spreadsheet
  61. * viewer when opening the file
  62. * If false, then formulae are not calculated on save. This is faster for saving in PhpSpreadsheet, but slower
  63. * when opening the resulting file in MS Excel, because Excel has to recalculate the formulae itself.
  64. *
  65. * @return bool
  66. */
  67. public function getPreCalculateFormulas()
  68. {
  69. return $this->preCalculateFormulas;
  70. }
  71. /**
  72. * Set Pre-Calculate Formulas
  73. * Set to true (the default) to advise the Writer to calculate all formulae on save
  74. * Set to false to prevent precalculation of formulae on save.
  75. *
  76. * @param bool $pValue Pre-Calculate Formulas?
  77. *
  78. * @return IWriter
  79. */
  80. public function setPreCalculateFormulas($pValue)
  81. {
  82. $this->preCalculateFormulas = (bool) $pValue;
  83. return $this;
  84. }
  85. /**
  86. * Get use disk caching where possible?
  87. *
  88. * @return bool
  89. */
  90. public function getUseDiskCaching()
  91. {
  92. return $this->useDiskCaching;
  93. }
  94. /**
  95. * Set use disk caching where possible?
  96. *
  97. * @param bool $pValue
  98. * @param string $pDirectory Disk caching directory
  99. *
  100. * @throws Exception when directory does not exist
  101. *
  102. * @return IWriter
  103. */
  104. public function setUseDiskCaching($pValue, $pDirectory = null)
  105. {
  106. $this->useDiskCaching = $pValue;
  107. if ($pDirectory !== null) {
  108. if (is_dir($pDirectory)) {
  109. $this->diskCachingDirectory = $pDirectory;
  110. } else {
  111. throw new Exception("Directory does not exist: $pDirectory");
  112. }
  113. }
  114. return $this;
  115. }
  116. /**
  117. * Get disk caching directory.
  118. *
  119. * @return string
  120. */
  121. public function getDiskCachingDirectory()
  122. {
  123. return $this->diskCachingDirectory;
  124. }
  125. }