1 | <?php |
||
19 | class Worksheet implements WorksheetInterface |
||
20 | { |
||
21 | /** |
||
22 | * Maximum number of characters a cell can contain |
||
23 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007] |
||
24 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3 [Excel 2010] |
||
25 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-ca36e2dc-1f09-4620-b726-67c00b05040f [Excel 2013/2016] |
||
26 | */ |
||
27 | const MAX_CHARACTERS_PER_CELL = 32767; |
||
28 | |||
29 | const SHEET_XML_FILE_HEADER = <<<EOD |
||
30 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
||
31 | <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> |
||
32 | EOD; |
||
33 | |||
34 | /** @var \Box\Spout\Writer\Common\Sheet The "external" sheet */ |
||
35 | protected $externalSheet; |
||
36 | |||
37 | /** @var string Path to the XML file that will contain the sheet data */ |
||
38 | protected $worksheetFilePath; |
||
39 | |||
40 | /** @var \Box\Spout\Writer\XLSX\Helper\SharedStringsHelper Helper to write shared strings */ |
||
41 | protected $sharedStringsHelper; |
||
42 | |||
43 | /** @var \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to work with styles */ |
||
44 | protected $styleHelper; |
||
45 | |||
46 | /** @var bool Whether inline or shared strings should be used */ |
||
47 | protected $shouldUseInlineStrings; |
||
48 | |||
49 | /** @var \Box\Spout\Common\Escaper\XLSX Strings escaper */ |
||
50 | protected $stringsEscaper; |
||
51 | |||
52 | /** @var \Box\Spout\Common\Helper\StringHelper String helper */ |
||
53 | protected $stringHelper; |
||
54 | |||
55 | /** @var Resource Pointer to the sheet data file (e.g. xl/worksheets/sheet1.xml) */ |
||
56 | protected $sheetFilePointer; |
||
57 | |||
58 | /** @var int Index of the last written row */ |
||
59 | protected $lastWrittenRowIndex = 0; |
||
60 | |||
61 | /** |
||
62 | * @param \Box\Spout\Writer\Common\Sheet $externalSheet The associated "external" sheet |
||
63 | * @param string $worksheetFilesFolder Temporary folder where the files to create the XLSX will be stored |
||
64 | * @param \Box\Spout\Writer\XLSX\Helper\SharedStringsHelper $sharedStringsHelper Helper for shared strings |
||
65 | * @param \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to work with styles |
||
66 | * @param bool $shouldUseInlineStrings Whether inline or shared strings should be used |
||
67 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
68 | */ |
||
69 | 135 | public function __construct($externalSheet, $worksheetFilesFolder, $sharedStringsHelper, $styleHelper, $shouldUseInlineStrings) |
|
83 | |||
84 | /** |
||
85 | * Prepares the worksheet to accept data |
||
86 | * |
||
87 | * @return void |
||
88 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
89 | */ |
||
90 | 135 | protected function startSheet() |
|
98 | |||
99 | /** |
||
100 | * Checks if the book has been created. Throws an exception if not created yet. |
||
101 | * |
||
102 | * @return void |
||
103 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
104 | */ |
||
105 | 135 | protected function throwIfSheetFilePointerIsNotAvailable() |
|
111 | |||
112 | /** |
||
113 | * @return \Box\Spout\Writer\Common\Sheet The "external" sheet |
||
114 | */ |
||
115 | 108 | public function getExternalSheet() |
|
119 | |||
120 | /** |
||
121 | * @return int The index of the last written row |
||
122 | */ |
||
123 | 99 | public function getLastWrittenRowIndex() |
|
127 | |||
128 | /** |
||
129 | * @return int The ID of the worksheet |
||
130 | */ |
||
131 | 105 | public function getId() |
|
136 | |||
137 | /** |
||
138 | * Adds data to the worksheet. |
||
139 | * |
||
140 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
141 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
142 | * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. NULL means use default style. |
||
143 | * @return void |
||
144 | * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written |
||
145 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
146 | */ |
||
147 | 99 | public function addRow($dataRow, $style) |
|
155 | |||
156 | /** |
||
157 | * Returns whether the given row is empty |
||
158 | * |
||
159 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
160 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
161 | * @return bool Whether the given row is empty |
||
162 | */ |
||
163 | 99 | private function isEmptyRow($dataRow) |
|
169 | |||
170 | /** |
||
171 | * Adds non empty row to the worksheet. |
||
172 | * |
||
173 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
174 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
175 | * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. NULL means use default style. |
||
176 | * @return void |
||
177 | * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written |
||
178 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
179 | */ |
||
180 | 99 | private function addNonEmptyRow($dataRow, $style) |
|
200 | |||
201 | /** |
||
202 | * Build and return xml for a single cell. |
||
203 | * |
||
204 | * @param int $rowIndex |
||
205 | * @param int $cellNumber |
||
206 | * @param mixed $cellValue |
||
207 | * @param int $styleId |
||
208 | * @return string |
||
209 | * @throws InvalidArgumentException If the given value cannot be processed |
||
210 | */ |
||
211 | 99 | private function getCellXML($rowIndex, $cellNumber, $cellValue, $styleId) |
|
243 | |||
244 | /** |
||
245 | * Returns the XML fragment for a cell containing a non empty string |
||
246 | * |
||
247 | * @param string $cellValue The cell value |
||
248 | * @return string The XML fragment representing the cell |
||
249 | * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell |
||
250 | */ |
||
251 | 96 | private function getCellXMLFragmentForNonEmptyString($cellValue) |
|
266 | |||
267 | /** |
||
268 | * Closes the worksheet |
||
269 | * |
||
270 | * @return void |
||
271 | */ |
||
272 | 105 | public function close() |
|
282 | } |
||
283 |