Skip to content

Commit 4058c8c

Browse files
Export only data cells/columns in toArray
Worksheet::toArray now calls these methods: $maxCol = $this->getHighestDataColumn(); $maxRow = $this->getHighestDataRow(); instead of getHighestColumn and getHighestRow. This is to make sure that rows/columns that only have some formatting are not included. Otherwise the export would contain 16384 columns if e.g. column width setting is applied.
1 parent 109b2b2 commit 4058c8c

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,9 +3286,9 @@ public function toArray(
32863286
$this->garbageCollect();
32873287
$this->calculateArrays($calculateFormulas);
32883288

3289-
// Identify the range that we need to extract from the worksheet
3290-
$maxCol = $this->getHighestColumn();
3291-
$maxRow = $this->getHighestRow();
3289+
// Identify the range that we need to extract from the worksheet
3290+
$maxCol = $this->getHighestDataColumn();
3291+
$maxRow = $this->getHighestDataRow();
32923292

32933293
// Return
32943294
return $this->rangeToArray("A1:{$maxCol}{$maxRow}", $nullValue, $calculateFormulas, $formatData, $returnCellRef, $ignoreHidden, $reduceArrays);

tests/PhpSpreadsheetTests/Worksheet/ToArrayTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,21 @@ public static function testMaxCol(): void
7676
self::assertSame('start', $array[0][0]);
7777
self::assertSame('end', $array[0][16383]);
7878
}
79+
80+
81+
public static function testToArrayConsidersValuesButNotFormatting(): void
82+
{
83+
$spreadsheet = new Spreadsheet();
84+
$sheet = $spreadsheet->getActiveSheet();
85+
86+
$sheet->getCell('A1')->setValue("hello");
87+
$array = $sheet->toArray(null, false, false, false, false);
88+
self::assertSame([['hello']], $array);
89+
90+
// Create column dimension object that didn't exist yet
91+
$sheet->getColumnDimension('C');
92+
$array = $sheet->toArray(null, false, false, false, false);
93+
// Yet there should be no column C in the export
94+
self::assertSame([['hello']], $array);
95+
}
7996
}

0 commit comments

Comments
 (0)