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/StringTable.php b/src/PhpSpreadsheet/Writer/Xlsx/StringTable.php index f9a2e711..a64e0d68 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/StringTable.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/StringTable.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx; +use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\RichText\RichText; use PhpOffice\PhpSpreadsheet\RichText\Run; @@ -35,8 +36,9 @@ class StringTable extends WriterPart $aFlippedStringTable = $this->flipStringTable($aStringTable); // Loop through cells - foreach ($worksheet->getCoordinates() as $coordinate) { - $cell = $worksheet->getCell($coordinate); + foreach ($worksheet->getCellCollection()->getCoordinates() as $coordinate) { + /** @var Cell $cell */ + $cell = $worksheet->getCellCollection()->get($coordinate); $cellValue = $cell->getValue(); if ( !is_object($cellValue) && 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); } }