diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 2f679a84..0b0d79dc 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -5250,11 +5250,6 @@ parameters: count: 1 path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php - - - message: "#^Offset int\\<1, max\\> on array\\\\> in isset\\(\\) does not exist\\.$#" - count: 2 - path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php - - message: "#^Parameter \\#2 \\$content of method XMLWriter\\:\\:writeElement\\(\\) expects string\\|null, int\\|string given\\.$#" count: 1 diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index eba4c927..cdc7c941 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -1145,11 +1145,15 @@ class Worksheet extends WriterPart // Highest row number $highestRow = $worksheet->getHighestRow(); - // Loop through cells + // Loop through cells building a comma-separated list of the columns in each row + // This is a trade-off between the memory usage that is required for a full array of columns, + // and execution speed + /** @var array $cellsByRow */ $cellsByRow = []; foreach ($worksheet->getCoordinates() as $coordinate) { - $cellAddress = Coordinate::coordinateFromString($coordinate); - $cellsByRow[$cellAddress[1]][] = $coordinate; + [$column, $row] = Coordinate::coordinateFromString($coordinate); + $cellsByRow[$row] = $cellsByRow[$row] ?? ''; + $cellsByRow[$row] .= "{$column},"; } $currentRow = 0; @@ -1195,9 +1199,12 @@ class Worksheet extends WriterPart // Write cells if (isset($cellsByRow[$currentRow])) { - foreach ($cellsByRow[$currentRow] as $cellAddress) { + // We have a comma-separated list of column names (with a trailing entry); split to an array + $columnsInRow = explode(',', $cellsByRow[$currentRow]); + array_pop($columnsInRow); + foreach ($columnsInRow as $column) { // Write cell - $this->writeCell($objWriter, $worksheet, $cellAddress, $aFlippedStringTable); + $this->writeCell($objWriter, $worksheet, "{$column}{$currentRow}", $aFlippedStringTable); } }