Skip to content

Commit d686a99

Browse files
author
MarkBaker
committed
Memory Experiment in Xlsx Writer
1 parent 48b5d83 commit d686a99

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5250,11 +5250,6 @@ parameters:
52505250
count: 1
52515251
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
52525252

5253-
-
5254-
message: "#^Offset int\\<1, max\\> on array\\<string, non\\-empty\\-array\\<int, string\\>\\> in isset\\(\\) does not exist\\.$#"
5255-
count: 2
5256-
path: src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php
5257-
52585253
-
52595254
message: "#^Parameter \\#2 \\$content of method XMLWriter\\:\\:writeElement\\(\\) expects string\\|null, int\\|string given\\.$#"
52605255
count: 1

src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,11 +1145,15 @@ private function writeSheetData(XMLWriter $objWriter, PhpspreadsheetWorksheet $w
11451145
// Highest row number
11461146
$highestRow = $worksheet->getHighestRow();
11471147

1148-
// Loop through cells
1148+
// Loop through cells building a comma-separated list of the columns in each row
1149+
// This is a trade-off between the memory usage that is required for a full array of columns,
1150+
// and execution speed
1151+
/** @var array<int, string> $cellsByRow */
11491152
$cellsByRow = [];
11501153
foreach ($worksheet->getCoordinates() as $coordinate) {
1151-
$cellAddress = Coordinate::coordinateFromString($coordinate);
1152-
$cellsByRow[$cellAddress[1]][] = $coordinate;
1154+
[$column, $row] = Coordinate::coordinateFromString($coordinate);
1155+
$cellsByRow[$row] = $cellsByRow[$row] ?? '';
1156+
$cellsByRow[$row] .= "{$column},";
11531157
}
11541158

11551159
$currentRow = 0;
@@ -1195,9 +1199,12 @@ private function writeSheetData(XMLWriter $objWriter, PhpspreadsheetWorksheet $w
11951199

11961200
// Write cells
11971201
if (isset($cellsByRow[$currentRow])) {
1198-
foreach ($cellsByRow[$currentRow] as $cellAddress) {
1202+
// We have a comma-separated list of column names (with a trailing entry); split to an array
1203+
$columnsInRow = explode(',', $cellsByRow[$currentRow]);
1204+
array_pop($columnsInRow);
1205+
foreach ($columnsInRow as $column) {
11991206
// Write cell
1200-
$this->writeCell($objWriter, $worksheet, $cellAddress, $aFlippedStringTable);
1207+
$this->writeCell($objWriter, $worksheet, "{$column}{$currentRow}", $aFlippedStringTable);
12011208
}
12021209
}
12031210

0 commit comments

Comments
 (0)