Merge pull request #2825 from PHPOffice/Column-Autofit-Indentation

Column autofit indentation
This commit is contained in:
Mark Baker 2022-05-11 20:13:04 +02:00 committed by GitHub
commit 54d82cd023
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 7 deletions

View File

@ -229,7 +229,8 @@ class Font
$cellText = '',
$rotation = 0,
?FontStyle $defaultFont = null,
bool $filterAdjustment = false
bool $filterAdjustment = false,
int $indentAdjustment = 0
): int {
// If it is rich text, use plain text
if ($cellText instanceof RichText) {
@ -248,12 +249,12 @@ class Font
}
// Try to get the exact text width in pixels
$approximate = self::$autoSizeMethod == self::AUTOSIZE_METHOD_APPROX;
$approximate = self::$autoSizeMethod === self::AUTOSIZE_METHOD_APPROX;
$columnWidth = 0;
if (!$approximate) {
$columnWidthAdjust = ceil(
self::getTextWidthPixelsExact(
str_repeat('n', 1 * ($filterAdjustment ? 3 : 1)),
str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))),
$font,
0
) * 1.07
@ -270,7 +271,7 @@ class Font
if ($approximate) {
$columnWidthAdjust = self::getTextWidthPixelsApprox(
str_repeat('n', 1 * ($filterAdjustment ? 3 : 1)),
str_repeat('n', 1 * (($filterAdjustment ? 3 : 1) + ($indentAdjustment * 2))),
$font,
0
);

View File

@ -756,11 +756,11 @@ class Worksheet implements IComparable
//By default merged cells should be ignored
$isMergedButProceed = false;
//The only exception is if it's a merge range value cell of a 'vertical' randge (1 column wide)
//The only exception is if it's a merge range value cell of a 'vertical' range (1 column wide)
if ($isMerged && $cell->isMergeRangeValueCell()) {
$range = $cell->getMergeRange();
$rangeBoundaries = Coordinate::rangeDimension($range);
if ($rangeBoundaries[0] == 1) {
if ($rangeBoundaries[0] === 1) {
$isMergedButProceed = true;
}
}
@ -774,6 +774,8 @@ class Worksheet implements IComparable
$filterAdjustment = true;
}
$indentAdjustment = $cell->getStyle()->getAlignment()->getIndent();
// Calculated value
// To formatted string
$cellValue = NumberFormat::toFormattedString(
@ -791,7 +793,8 @@ class Worksheet implements IComparable
$this->getParent()->getCellXfByIndex($cell->getXfIndex())
->getAlignment()->getTextRotation(),
$this->getParent()->getDefaultStyle()->getFont(),
$filterAdjustment
$filterAdjustment,
$indentAdjustment
)
);
}

View File

@ -99,4 +99,35 @@ class FontTest extends TestCase
$width = Font::getTextWidthPixelsApprox('n', $font, -165);
self::assertEquals(4, $width);
}
/**
* @dataProvider providerCalculateApproximateColumnWidth
*/
public function testCalculateApproximateColumnWidth(
int $expectedWidth,
StyleFont $font,
string $text,
int $rotation,
StyleFont $defaultFont,
bool $filter,
int $indent
): void {
$columnWidth = Font::calculateColumnWidth($font, $text, $rotation, $defaultFont, $filter, $indent);
self::assertEquals($expectedWidth, $columnWidth);
}
public function providerCalculateApproximateColumnWidth(): array
{
return [
[13, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 0],
[16, new StyleFont(), 'Hello World', 0, new StyleFont(), true, 0],
[16, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 1],
[18, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 2],
[20, new StyleFont(), 'Hello World', 0, new StyleFont(), false, 3],
[6, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), false, 0],
[9, new StyleFont(), "Hello\nWorld", 0, new StyleFont(), true, 0],
[17, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 0],
[19, new StyleFont(), 'PhpSpreadsheet', 0, new StyleFont(), false, 1],
];
}
}