Merge remote-tracking branch 'remotes/origin/master' into custom-column-widths

# Conflicts:
#	src/Spout/Writer/XLSX/Manager/WorksheetManager.php
This commit is contained in:
Alexander Hofstede committed 2019-12-19 23:02:47 +01:00
commit 49e621b822
102 files changed
+1051 -381

No files matched your search

@@ -116,12 +116,12 @@ EOD;
*/
public function startSheet(Worksheet $worksheet)
{
$sheetFilePointer = fopen($worksheet->getFilePath(), 'w');
$sheetFilePointer = \fopen($worksheet->getFilePath(), 'w');
$this->throwIfSheetFilePointerIsNotAvailable($sheetFilePointer);
$worksheet->setFilePointer($sheetFilePointer);
fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER);
\fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER);
}
/**
@@ -167,22 +167,21 @@ EOD;
fwrite($sheetFilePointer, $this->getXMLFragmentForColumnWidths());
fwrite($sheetFilePointer, '<sheetData>');
}
$cellIndex = 0;
$rowStyle = $row->getStyle();
$rowIndex = $worksheet->getLastWrittenRowIndex() + 1;
$rowIndexOneBased = $worksheet->getLastWrittenRowIndex() + 1;
$numCells = $row->getNumCells();
$hasCustomHeight = $this->defaultRowHeight > 0 ? '1' : '0';
$rowXML = "<row r=\"{$rowIndex}\" spans=\"1:{$numCells}\" customHeight=\"{$hasCustomHeight}\">";
$rowXML = "<row r=\"{$rowIndexOneBased}\" spans=\"1:{$numCells}\" customHeight=\"{$hasCustomHeight}\">";
foreach ($row->getCells() as $cell) {
$rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndex, $cellIndex);
$cellIndex++;
foreach ($row->getCells() as $columnIndexZeroBased => $cell) {
$rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndexOneBased, $columnIndexZeroBased);
}
$rowXML .= '</row>';
$wasWriteSuccessful = fwrite($sheetFilePointer, $rowXML);
$wasWriteSuccessful = \fwrite($sheetFilePointer, $rowXML);
if ($wasWriteSuccessful === false) {
throw new IOException("Unable to write data in {$worksheet->getFilePath()}");
}
@@ -193,14 +192,15 @@ EOD;
* Applies styles to the given style, merging the cell's style with its row's style
* Then builds and returns xml for the cell.
*
* @param Cell $cell
* @param Cell $cell
* @param Style $rowStyle
* @param int $rowIndex
* @param int $cellIndex
* @param int $rowIndexOneBased
* @param int $columnIndexZeroBased
*
* @return string
* @throws InvalidArgumentException If the given value cannot be processed
*/
private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $rowIndex, $cellIndex)
private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $rowIndexOneBased, $columnIndexZeroBased)
{
// Apply row and extra styles
$mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle);
@@ -209,23 +209,24 @@ EOD;
$registeredStyle = $this->styleManager->registerStyle($newCellStyle);
return $this->getCellXML($rowIndex, $cellIndex, $cell, $registeredStyle->getId());
return $this->getCellXML($rowIndexOneBased, $columnIndexZeroBased, $cell, $registeredStyle->getId());
}
/**
* Builds and returns xml for a single cell.
*
* @param int $rowIndex
* @param int $cellNumber
* @param int $rowIndexOneBased
* @param int $columnIndexZeroBased
* @param Cell $cell
* @param int $styleId
* @param int $styleId
*
* @return string
* @throws InvalidArgumentException If the given value cannot be processed
*/
private function getCellXML($rowIndex, $cellNumber, Cell $cell, $styleId)
private function getCellXML($rowIndexOneBased, $columnIndexZeroBased, Cell $cell, $styleId)
{
$columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber);
$cellXML = '<c r="' . $columnIndex . $rowIndex . '"';
$columnLetters = CellHelper::getColumnLettersFromColumnIndex($columnIndexZeroBased);
$cellXML = '<c r="' . $columnLetters . $rowIndexOneBased . '"';
$cellXML .= ' s="' . $styleId . '"';
if ($cell->isString()) {
@@ -234,6 +235,9 @@ EOD;
$cellXML .= ' t="b"><v>' . (int)($cell->getValue()) . '</v></c>';
} elseif ($cell->isNumeric()) {
$cellXML .= '><v>' . $cell->getValue() . '</v></c>';
} elseif ($cell->isError() && is_string($cell->getValueEvenIfError())) {
// only writes the error value if it's a string
$cellXML .= ' t="e"><v>' . $cell->getValueEvenIfError() . '</v></c>';
} elseif ($cell->isEmpty()) {
if ($this->styleManager->shouldApplyStyleOnEmptyCell($styleId)) {
$cellXML .= '/>';
@@ -243,7 +247,7 @@ EOD;
$cellXML = '';
}
} else {
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cell->getValue()));
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . \gettype($cell->getValue()));
}
return $cellXML;
@@ -314,14 +318,14 @@ EOD;
{
$worksheetFilePointer = $worksheet->getFilePointer();
if (!is_resource($worksheetFilePointer)) {
if (!\is_resource($worksheetFilePointer)) {
return;
}
if ($this->hasWrittenRows) {
fwrite($worksheetFilePointer, '</sheetData>');
\fwrite($worksheetFilePointer, '</sheetData>');
}
fwrite($worksheetFilePointer, '</worksheet>');
fclose($worksheetFilePointer);
\fwrite($worksheetFilePointer, '</worksheet>');
\fclose($worksheetFilePointer);
}
}