Skip to content

Commit 276c96a

Browse files
committed
added percentage formatting (still need to set column type in excel...)
1 parent 80e7317 commit 276c96a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Fastbolt\ExcelWriter\ColumnFormatter;
4+
5+
use Fastbolt\ExcelWriter\ColumnSetting;
6+
use PhpOffice\PhpSpreadsheet\Style\Alignment;
7+
8+
class PercentageFormatter extends BaseFormatter
9+
{
10+
private int $decimalLength;
11+
12+
public function __construct(ColumnSetting $column)
13+
{
14+
$this->decimalLength = $column->getDecimalLength();
15+
}
16+
17+
public function getAlignment(): array
18+
{
19+
return ['horizontal' => Alignment::HORIZONTAL_RIGHT];
20+
}
21+
22+
public function getNumberFormat(): array
23+
{
24+
$formatCode = '0.' . str_repeat('0', $this->decimalLength) . '%';
25+
26+
return ['formatCode' => $formatCode];
27+
}
28+
}

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)