1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace OpenSpout\Writer\XLSX\Manager;
|
|
|
6 |
|
|
|
7 |
use OpenSpout\Common\Entity\Cell;
|
|
|
8 |
use OpenSpout\Common\Entity\Row;
|
|
|
9 |
use OpenSpout\Common\Entity\Style\Style;
|
|
|
10 |
use OpenSpout\Common\Exception\InvalidArgumentException;
|
|
|
11 |
use OpenSpout\Common\Exception\IOException;
|
|
|
12 |
use OpenSpout\Common\Helper\Escaper\XLSX as XLSXEscaper;
|
|
|
13 |
use OpenSpout\Common\Helper\StringHelper;
|
|
|
14 |
use OpenSpout\Writer\Common\Entity\Worksheet;
|
|
|
15 |
use OpenSpout\Writer\Common\Helper\CellHelper;
|
|
|
16 |
use OpenSpout\Writer\Common\Manager\RegisteredStyle;
|
|
|
17 |
use OpenSpout\Writer\Common\Manager\Style\StyleMerger;
|
|
|
18 |
use OpenSpout\Writer\Common\Manager\WorksheetManagerInterface;
|
|
|
19 |
use OpenSpout\Writer\XLSX\Helper\DateHelper;
|
|
|
20 |
use OpenSpout\Writer\XLSX\Helper\DateIntervalHelper;
|
|
|
21 |
use OpenSpout\Writer\XLSX\Manager\Style\StyleManager;
|
|
|
22 |
use OpenSpout\Writer\XLSX\Options;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* @internal
|
|
|
26 |
*/
|
|
|
27 |
final class WorksheetManager implements WorksheetManagerInterface
|
|
|
28 |
{
|
|
|
29 |
/**
|
|
|
30 |
* Maximum number of characters a cell can contain.
|
|
|
31 |
*
|
|
|
32 |
* @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007]
|
|
|
33 |
* @see https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3 [Excel 2010]
|
|
|
34 |
* @see https://support.office.com/en-us/article/Excel-specifications-and-limits-ca36e2dc-1f09-4620-b726-67c00b05040f [Excel 2013/2016]
|
|
|
35 |
*/
|
|
|
36 |
public const MAX_CHARACTERS_PER_CELL = 32767;
|
|
|
37 |
|
|
|
38 |
/** @var CommentsManager Manages comments */
|
|
|
39 |
private readonly CommentsManager $commentsManager;
|
|
|
40 |
|
|
|
41 |
private readonly Options $options;
|
|
|
42 |
|
|
|
43 |
/** @var StyleManager Manages styles */
|
|
|
44 |
private readonly StyleManager $styleManager;
|
|
|
45 |
|
|
|
46 |
/** @var StyleMerger Helper to merge styles together */
|
|
|
47 |
private readonly StyleMerger $styleMerger;
|
|
|
48 |
|
|
|
49 |
/** @var SharedStringsManager Helper to write shared strings */
|
|
|
50 |
private readonly SharedStringsManager $sharedStringsManager;
|
|
|
51 |
|
|
|
52 |
/** @var XLSXEscaper Strings escaper */
|
|
|
53 |
private readonly XLSXEscaper $stringsEscaper;
|
|
|
54 |
|
|
|
55 |
/** @var StringHelper String helper */
|
|
|
56 |
private readonly StringHelper $stringHelper;
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* WorksheetManager constructor.
|
|
|
60 |
*/
|
|
|
61 |
public function __construct(
|
|
|
62 |
Options $options,
|
|
|
63 |
StyleManager $styleManager,
|
|
|
64 |
StyleMerger $styleMerger,
|
|
|
65 |
CommentsManager $commentsManager,
|
|
|
66 |
SharedStringsManager $sharedStringsManager,
|
|
|
67 |
XLSXEscaper $stringsEscaper,
|
|
|
68 |
StringHelper $stringHelper
|
|
|
69 |
) {
|
|
|
70 |
$this->options = $options;
|
|
|
71 |
$this->styleManager = $styleManager;
|
|
|
72 |
$this->styleMerger = $styleMerger;
|
|
|
73 |
$this->commentsManager = $commentsManager;
|
|
|
74 |
$this->sharedStringsManager = $sharedStringsManager;
|
|
|
75 |
$this->stringsEscaper = $stringsEscaper;
|
|
|
76 |
$this->stringHelper = $stringHelper;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public function getSharedStringsManager(): SharedStringsManager
|
|
|
80 |
{
|
|
|
81 |
return $this->sharedStringsManager;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public function startSheet(Worksheet $worksheet): void
|
|
|
85 |
{
|
|
|
86 |
$sheetFilePointer = fopen($worksheet->getFilePath(), 'w');
|
|
|
87 |
\assert(false !== $sheetFilePointer);
|
|
|
88 |
|
|
|
89 |
$worksheet->setFilePointer($sheetFilePointer);
|
|
|
90 |
$this->commentsManager->createWorksheetCommentFiles($worksheet);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public function addRow(Worksheet $worksheet, Row $row): void
|
|
|
94 |
{
|
|
|
95 |
if (!$row->isEmpty()) {
|
|
|
96 |
$this->addNonEmptyRow($worksheet, $row);
|
|
|
97 |
$this->commentsManager->addComments($worksheet, $row);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
$worksheet->setLastWrittenRowIndex($worksheet->getLastWrittenRowIndex() + 1);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public function close(Worksheet $worksheet): void
|
|
|
104 |
{
|
|
|
105 |
$this->commentsManager->closeWorksheetCommentFiles($worksheet);
|
|
|
106 |
fclose($worksheet->getFilePointer());
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Adds non empty row to the worksheet.
|
|
|
111 |
*
|
|
|
112 |
* @param Worksheet $worksheet The worksheet to add the row to
|
|
|
113 |
* @param Row $row The row to be written
|
|
|
114 |
*
|
|
|
115 |
* @throws InvalidArgumentException If a cell value's type is not supported
|
|
|
116 |
* @throws IOException If the data cannot be written
|
|
|
117 |
*/
|
|
|
118 |
private function addNonEmptyRow(Worksheet $worksheet, Row $row): void
|
|
|
119 |
{
|
|
|
120 |
$sheetFilePointer = $worksheet->getFilePointer();
|
|
|
121 |
$rowStyle = $row->getStyle();
|
|
|
122 |
$rowIndexOneBased = $worksheet->getLastWrittenRowIndex() + 1;
|
|
|
123 |
$numCells = $row->getNumCells();
|
|
|
124 |
|
|
|
125 |
$rowHeight = $row->getHeight();
|
|
|
126 |
$hasCustomHeight = ($this->options->DEFAULT_ROW_HEIGHT > 0 || $rowHeight > 0) ? '1' : '0';
|
|
|
127 |
$rowXML = "<row r=\"{$rowIndexOneBased}\" spans=\"1:{$numCells}\" ".($rowHeight > 0 ? "ht=\"{$rowHeight}\" " : '')."customHeight=\"{$hasCustomHeight}\">";
|
|
|
128 |
|
|
|
129 |
foreach ($row->getCells() as $columnIndexZeroBased => $cell) {
|
|
|
130 |
$registeredStyle = $this->applyStyleAndRegister($cell, $rowStyle);
|
|
|
131 |
$cellStyle = $registeredStyle->getStyle();
|
|
|
132 |
if ($registeredStyle->isMatchingRowStyle()) {
|
|
|
133 |
$rowStyle = $cellStyle; // Replace actual rowStyle (possibly with null id) by registered style (with id)
|
|
|
134 |
}
|
|
|
135 |
$rowXML .= $this->getCellXML($rowIndexOneBased, $columnIndexZeroBased, $cell, $cellStyle->getId());
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
$rowXML .= '</row>';
|
|
|
139 |
|
|
|
140 |
$wasWriteSuccessful = fwrite($sheetFilePointer, $rowXML);
|
|
|
141 |
if (false === $wasWriteSuccessful) {
|
|
|
142 |
throw new IOException("Unable to write data in {$worksheet->getFilePath()}");
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Applies styles to the given style, merging the cell's style with its row's style.
|
|
|
148 |
*
|
|
|
149 |
* @throws InvalidArgumentException If the given value cannot be processed
|
|
|
150 |
*/
|
|
|
151 |
private function applyStyleAndRegister(Cell $cell, Style $rowStyle): RegisteredStyle
|
|
|
152 |
{
|
|
|
153 |
$isMatchingRowStyle = false;
|
|
|
154 |
if ($cell->getStyle()->isEmpty()) {
|
|
|
155 |
$cell->setStyle($rowStyle);
|
|
|
156 |
|
|
|
157 |
$possiblyUpdatedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell);
|
|
|
158 |
|
|
|
159 |
if ($possiblyUpdatedStyle->isUpdated()) {
|
|
|
160 |
$registeredStyle = $this->styleManager->registerStyle($possiblyUpdatedStyle->getStyle());
|
|
|
161 |
} else {
|
|
|
162 |
$registeredStyle = $this->styleManager->registerStyle($rowStyle);
|
|
|
163 |
$isMatchingRowStyle = true;
|
|
|
164 |
}
|
|
|
165 |
} else {
|
|
|
166 |
$mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle);
|
|
|
167 |
$cell->setStyle($mergedCellAndRowStyle);
|
|
|
168 |
|
|
|
169 |
$possiblyUpdatedStyle = $this->styleManager->applyExtraStylesIfNeeded($cell);
|
|
|
170 |
|
|
|
171 |
if ($possiblyUpdatedStyle->isUpdated()) {
|
|
|
172 |
$newCellStyle = $possiblyUpdatedStyle->getStyle();
|
|
|
173 |
} else {
|
|
|
174 |
$newCellStyle = $mergedCellAndRowStyle;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
$registeredStyle = $this->styleManager->registerStyle($newCellStyle);
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
return new RegisteredStyle($registeredStyle, $isMatchingRowStyle);
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Builds and returns xml for a single cell.
|
|
|
185 |
*
|
|
|
186 |
* @throws InvalidArgumentException If the given value cannot be processed
|
|
|
187 |
*/
|
|
|
188 |
private function getCellXML(int $rowIndexOneBased, int $columnIndexZeroBased, Cell $cell, ?int $styleId): string
|
|
|
189 |
{
|
|
|
190 |
$columnLetters = CellHelper::getColumnLettersFromColumnIndex($columnIndexZeroBased);
|
|
|
191 |
$cellXML = '<c r="'.$columnLetters.$rowIndexOneBased.'"';
|
|
|
192 |
$cellXML .= ' s="'.$styleId.'"';
|
|
|
193 |
|
|
|
194 |
if ($cell instanceof Cell\StringCell) {
|
|
|
195 |
$cellXML .= $this->getCellXMLFragmentForNonEmptyString($cell->getValue());
|
|
|
196 |
} elseif ($cell instanceof Cell\BooleanCell) {
|
|
|
197 |
$cellXML .= ' t="b"><v>'.(int) $cell->getValue().'</v></c>';
|
|
|
198 |
} elseif ($cell instanceof Cell\NumericCell) {
|
|
|
199 |
$cellXML .= '><v>'.$cell->getValue().'</v></c>';
|
|
|
200 |
} elseif ($cell instanceof Cell\FormulaCell) {
|
|
|
201 |
$cellXML .= '><f>'.substr($cell->getValue(), 1).'</f></c>';
|
|
|
202 |
} elseif ($cell instanceof Cell\DateTimeCell) {
|
|
|
203 |
$cellXML .= '><v>'.DateHelper::toExcel($cell->getValue()).'</v></c>';
|
|
|
204 |
} elseif ($cell instanceof Cell\DateIntervalCell) {
|
|
|
205 |
$cellXML .= '><v>'.DateIntervalHelper::toExcel($cell->getValue()).'</v></c>';
|
|
|
206 |
} elseif ($cell instanceof Cell\ErrorCell) {
|
|
|
207 |
// only writes the error value if it's a string
|
|
|
208 |
$cellXML .= ' t="e"><v>'.$cell->getRawValue().'</v></c>';
|
|
|
209 |
} elseif ($cell instanceof Cell\EmptyCell) {
|
|
|
210 |
if ($this->styleManager->shouldApplyStyleOnEmptyCell($styleId)) {
|
|
|
211 |
$cellXML .= '/>';
|
|
|
212 |
} else {
|
|
|
213 |
// don't write empty cells that do no need styling
|
|
|
214 |
// NOTE: not appending to $cellXML is the right behavior!!
|
|
|
215 |
$cellXML = '';
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
return $cellXML;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Returns the XML fragment for a cell containing a non empty string.
|
|
|
224 |
*
|
|
|
225 |
* @param string $cellValue The cell value
|
|
|
226 |
*
|
|
|
227 |
* @return string The XML fragment representing the cell
|
|
|
228 |
*
|
|
|
229 |
* @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell
|
|
|
230 |
*/
|
|
|
231 |
private function getCellXMLFragmentForNonEmptyString(string $cellValue): string
|
|
|
232 |
{
|
|
|
233 |
if ($this->stringHelper->getStringLength($cellValue) > self::MAX_CHARACTERS_PER_CELL) {
|
|
|
234 |
throw new InvalidArgumentException('Trying to add a value that exceeds the maximum number of characters allowed in a cell (32,767)');
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
if ($this->options->SHOULD_USE_INLINE_STRINGS) {
|
|
|
238 |
$cellXMLFragment = ' t="inlineStr"><is><t>'.$this->stringsEscaper->escape($cellValue).'</t></is></c>';
|
|
|
239 |
} else {
|
|
|
240 |
$sharedStringId = $this->sharedStringsManager->writeString($cellValue);
|
|
|
241 |
$cellXMLFragment = ' t="s"><v>'.$sharedStringId.'</v></c>';
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
return $cellXMLFragment;
|
|
|
245 |
}
|
|
|
246 |
}
|