1 | <?php |
||
20 | class Worksheet implements WorksheetInterface |
||
21 | { |
||
22 | /** |
||
23 | * Maximum number of characters a cell can contain |
||
24 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007] |
||
25 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3 [Excel 2010] |
||
26 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-ca36e2dc-1f09-4620-b726-67c00b05040f [Excel 2013/2016] |
||
27 | */ |
||
28 | const MAX_CHARACTERS_PER_CELL = 32767; |
||
29 | |||
30 | const SHEET_XML_FILE_HEADER = <<<EOD |
||
31 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
||
32 | <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> |
||
33 | EOD; |
||
34 | |||
35 | /** @var \Box\Spout\Writer\Common\Sheet The "external" sheet */ |
||
36 | protected $externalSheet; |
||
37 | |||
38 | /** @var string Path to the XML file that will contain the sheet data */ |
||
39 | protected $worksheetFilePath; |
||
40 | |||
41 | /** @var \Box\Spout\Writer\XLSX\Helper\SharedStringsHelper Helper to write shared strings */ |
||
42 | protected $sharedStringsHelper; |
||
43 | |||
44 | /** @var \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to work with styles */ |
||
45 | protected $styleHelper; |
||
46 | |||
47 | /** @var bool Whether inline or shared strings should be used */ |
||
48 | protected $shouldUseInlineStrings; |
||
49 | |||
50 | /** @var bool Determine whether cell widths should be calculated */ |
||
51 | protected $shouldUseCellAutosizing; |
||
52 | |||
53 | /** @var \Box\Spout\Common\Escaper\XLSX Strings escaper */ |
||
54 | protected $stringsEscaper; |
||
55 | |||
56 | /** @var \Box\Spout\Common\Helper\StringHelper String helper */ |
||
57 | protected $stringHelper; |
||
58 | |||
59 | /** @var \Box\Spout\Writer\XLSX\Helper\SizeCalculator */ |
||
60 | protected $sizeCalculator; |
||
61 | |||
62 | /** @var Resource Pointer to the sheet data file (e.g. xl/worksheets/sheet1.xml) */ |
||
63 | protected $sheetFilePointer; |
||
64 | |||
65 | /** @var int Index of the last written row */ |
||
66 | protected $lastWrittenRowIndex = 0; |
||
67 | |||
68 | /** @var array Holds the column widths for cell sizing */ |
||
69 | protected $columnWidths = []; |
||
70 | |||
71 | /** |
||
72 | * @param \Box\Spout\Writer\Common\Sheet $externalSheet The associated "external" sheet |
||
73 | * @param string $worksheetFilesFolder Temporary folder where the files to create the XLSX will be stored |
||
74 | * @param \Box\Spout\Writer\XLSX\Helper\SharedStringsHelper $sharedStringsHelper Helper for shared strings |
||
75 | * @param \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to work with styles |
||
76 | * @param \Box\Spout\Writer\XLSX\Helper\SizeCalculator $sizeCalculator To calculate cell sizes |
||
77 | * @param bool $shouldUseInlineStrings Whether inline or shared strings should be used |
||
78 | * @param bool $shouldUseCellAutosizing Whether cell sizes should be calculated or not |
||
79 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
80 | */ |
||
81 | 132 | public function __construct( |
|
104 | |||
105 | /** |
||
106 | * Prepares the worksheet to accept data and preserves free space at the beginning |
||
107 | * of the sheet file to prepend header xml and optional column size data. |
||
108 | * |
||
109 | * @return void |
||
110 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
111 | */ |
||
112 | 132 | protected function startSheet() |
|
121 | |||
122 | /** |
||
123 | * Checks if the book has been created. Throws an exception if not created yet. |
||
124 | * |
||
125 | * @return void |
||
126 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
127 | */ |
||
128 | 132 | protected function throwIfSheetFilePointerIsNotAvailable() |
|
134 | |||
135 | /** |
||
136 | * @return \Box\Spout\Writer\Common\Sheet The "external" sheet |
||
137 | */ |
||
138 | 102 | public function getExternalSheet() |
|
142 | |||
143 | /** |
||
144 | * @return int The index of the last written row |
||
145 | */ |
||
146 | 93 | public function getLastWrittenRowIndex() |
|
150 | |||
151 | /** |
||
152 | * @return int The ID of the worksheet |
||
153 | */ |
||
154 | 99 | public function getId() |
|
159 | |||
160 | /** |
||
161 | * Adds data to the worksheet. |
||
162 | * |
||
163 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
164 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
165 | * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. NULL means use default style. |
||
166 | * @return void |
||
167 | * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written |
||
168 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
169 | */ |
||
170 | 93 | public function addRow($dataRow, $style) |
|
178 | |||
179 | /** |
||
180 | * Returns whether the given row is empty |
||
181 | * |
||
182 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
183 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
184 | * @return bool Whether the given row is empty |
||
185 | */ |
||
186 | 93 | private function isEmptyRow($dataRow) |
|
192 | |||
193 | /** |
||
194 | * Adds non empty row to the worksheet. |
||
195 | * |
||
196 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
197 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
198 | * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. NULL means use default style. |
||
199 | * @return void |
||
200 | * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written |
||
201 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
202 | */ |
||
203 | 93 | private function addNonEmptyRow($dataRow, $style) |
|
227 | |||
228 | /** |
||
229 | * Build and return xml for a single cell. |
||
230 | * |
||
231 | * @param int $rowIndex |
||
232 | * @param int $cellNumber |
||
233 | * @param mixed $cellValue |
||
234 | * @param Style $style Style to be applied to the row. NULL means use default style. |
||
235 | * |
||
236 | * @return string |
||
237 | * @throws InvalidArgumentException If the given value cannot be processed |
||
238 | */ |
||
239 | 93 | private function getCellXML($rowIndex, $cellNumber, $cellValue, Style $style) |
|
267 | |||
268 | /** |
||
269 | * Returns the XML fragment for a cell containing a non empty string |
||
270 | * |
||
271 | * @param string $cellValue The cell value |
||
272 | * @return string The XML fragment representing the cell |
||
273 | * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell |
||
274 | */ |
||
275 | 90 | private function getCellXMLFragmentForNonEmptyString($cellValue) |
|
290 | |||
291 | /** |
||
292 | * Update the width of the current cellNumber, if cell autosizing is enabled |
||
293 | * and the width of the current value exceeds a previously calculated one. |
||
294 | * |
||
295 | * @param int $cellNumber |
||
296 | * @param string $cellValue |
||
297 | * @param Style $style |
||
298 | */ |
||
299 | 87 | private function updateColumnWidth($cellNumber, $cellValue, $style) |
|
308 | |||
309 | /** |
||
310 | * Return writable <cols> xml string, if column widths have been |
||
311 | * calculated or custom widths have been set. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | 99 | private function getColsXML() |
|
332 | |||
333 | /** |
||
334 | * Closes the worksheet |
||
335 | * |
||
336 | * @return void |
||
337 | */ |
||
338 | 99 | public function close() |
|
352 | } |
||
353 |