Skip to content

Commit 54d82cd

Browse files
authored
Merge pull request #2825 from PHPOffice/Column-Autofit-Indentation
Column autofit indentation
2 parents a352856 + ffd900b commit 54d82cd

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/PhpSpreadsheet/Shared/Font.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ public static function calculateColumnWidth(
229229
$cellText = '',
230230
$rotation = 0,
231231
?FontStyle $defaultFont = null,
232-
bool $filterAdjustment = false
232+
bool $filterAdjustment = false,
233+
int $indentAdjustment = 0
233234
): int {
234235
// If it is rich text, use plain text
235236
if ($cellText instanceof RichText) {
@@ -248,12 +249,12 @@ public static function calculateColumnWidth(
248249
}
249250

250251
// Try to get the exact text width in pixels
251-
$approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX;
252+
$approximate = self::$autoSizeMethod === self::AUTOSIZE_METHOD_APPROX;
252253
$columnWidth = 0;
253254
if (!$approximate) {
254255
$columnWidthAdjust = ceil(
255256
self::getTextWidthPixelsExact(
256-
str_repeat('n', 1 * ($filterAdjustment ? 3 : 1)),
257+
str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))),
257258
$font,
258259
0
259260
) * 1.07
@@ -270,7 +271,7 @@ public static function calculateColumnWidth(
270271

271272
if ($approximate) {
272273
$columnWidthAdjust = self::getTextWidthPixelsApprox(
273-
str_repeat('n', 1 * ($filterAdjustment ? 3 : 1)),
274+
str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))),
274275
$font,
275276
0
276277
);

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,11 @@ public function calculateColumnWidths()
756756
//By default merged cells should be ignored
757757
$isMergedButProceed = false;
758758

759-
//The only exception is if it's a merge range value cell of a 'vertical' randge (1 column wide)
759+
//The only exception is if it's a merge range value cell of a 'vertical' range (1 column wide)
760760
if ($isMerged && $cell->isMergeRangeValueCell()) {
761761
$range = $cell->getMergeRange();
762762
$rangeBoundaries = Coordinate::rangeDimension($range);
763-
if ($rangeBoundaries[0] == 1) {
763+
if ($rangeBoundaries[0] === 1) {
764764
$isMergedButProceed = true;
765765
}
766766
}
@@ -774,6 +774,8 @@ public function calculateColumnWidths()
774774
$filterAdjustment = true;
775775
}
776776

777+
$indentAdjustment = $cell->getStyle()->getAlignment()->getIndent();
778+
777779
// Calculated value
778780
// To formatted string
779781
$cellValue = NumberFormat::toFormattedString(
@@ -791,7 +793,8 @@ public function calculateColumnWidths()
791793
$this->getParent()->getCellXfByIndex($cell->getXfIndex())
792794
->getAlignment()->getTextRotation(),
793795
$this->getParent()->getDefaultStyle()->getFont(),
794-
$filterAdjustment
796+
$filterAdjustment,
797+
$indentAdjustment
795798
)
796799
);
797800
}

tests/PhpSpreadsheetTests/Shared/FontTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,35 @@ public function testVerdanaRotation(): void
9999
$width = Font::getTextWidthPixelsApprox('n', $font, -165);
100100
self::assertEquals(4, $width);
101101
}
102+
103+
/**
104+
* @dataProvider providerCalculateApproximateColumnWidth
105+
*/
106+
public function testCalculateApproximateColumnWidth(
107+
int $expectedWidth,
108+
StyleFont $font,
109+
string $text,
110+
int $rotation,
111+
StyleFont $defaultFont,
112+
bool $filter,
113+
int $indent
114+
): void {
115+
$columnWidth = Font::calculateColumnWidth($font, $text, $rotation, $defaultFont, $filter, $indent);
116+
self::assertEquals($expectedWidth, $columnWidth);
117+
}
118+
119+
public function providerCalculateApproximateColumnWidth(): array
120+
{
121+
return [
122+
[13, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 0],
123+
[16, new StyleFont(), 'Hello World', 0, new StyleFont(), true, 0],
124+
[16, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 1],
125+
[18, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 2],
126+
[20, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 3],
127+
[6, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), false, 0],
128+
[9, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), true, 0],
129+
[17, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 0],
130+
[19, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 1],
131+
];
132+
}
102133
}

0 commit comments

Comments
 (0)