Skip to content

Commit 3ae6eb1

Browse files
authored
Merge pull request #20 from fastbolt/19-adding-column-formatting-percent-type
added percentage formatting and column type in excel
2 parents 80e7317 + 9d22c0e commit 3ae6eb1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Fastbolt Schraubengroßhandels GmbH.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
namespace Fastbolt\ExcelWriter\ColumnFormatter;
10+
11+
use Fastbolt\ExcelWriter\ColumnSetting;
12+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
13+
14+
class PercentageFormatter extends BaseFormatter
15+
{
16+
private int $decimalLength;
17+
18+
public function __construct(ColumnSetting $column)
19+
{
20+
$this->decimalLength = $column->getDecimalLength();
21+
}
22+
23+
public function getAlignment(): array
24+
{
25+
return ['horizontal' => Alignment::HORIZONTAL_RIGHT];
26+
}
27+
28+
public function getNumberFormat(): array
29+
{
30+
$formatCode = '0.' . str_repeat('0', $this->decimalLength) . '%';
31+
32+
return ['formatCode' => $formatCode];
33+
}
34+
}

src/ColumnSetting.php

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Fastbolt\ExcelWriter\ColumnFormatter\DateFormatter;
1313
use Fastbolt\ExcelWriter\ColumnFormatter\FloatFormatter;
1414
use Fastbolt\ExcelWriter\ColumnFormatter\IntegerFormatter;
15+
use Fastbolt\ExcelWriter\ColumnFormatter\PercentageFormatter;
1516
use Fastbolt\ExcelWriter\ColumnFormatter\StringFormatter;
1617

1718
class ColumnSetting
@@ -20,6 +21,7 @@ class ColumnSetting
2021
public const FORMAT_FLOAT = 'float';
2122
public const FORMAT_STRING = 'string';
2223
public const FORMAT_DATE = 'datetime';
24+
public const FOMRAT_PERCENTAGE = 'percentage';
2325

2426
private string $format;
2527
private string $name = ''; //excel-name for the column
@@ -92,6 +94,8 @@ public function getFormatter(): ColumnFormatter
9294
return new IntegerFormatter();
9395
case self::FORMAT_FLOAT:
9496
return new FloatFormatter($this);
97+
case self::FOMRAT_PERCENTAGE:
98+
return new PercentageFormatter($this);
9599
}
96100

97101
return new StringFormatter();

0 commit comments

Comments
 (0)