Skip to content

Commit

Permalink
added percentage formatting (still need to set column type in excel...)
Browse files Browse the repository at this point in the history
  • Loading branch information
StvL97 committed Oct 2, 2024
1 parent 80e7317 commit 276c96a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/ColumnFormatter/PercentageFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Fastbolt\ExcelWriter\ColumnFormatter;

use Fastbolt\ExcelWriter\ColumnSetting;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class PercentageFormatter extends BaseFormatter
{
private int $decimalLength;

public function __construct(ColumnSetting $column)
{
$this->decimalLength = $column->getDecimalLength();
}

public function getAlignment(): array
{
return ['horizontal' => Alignment::HORIZONTAL_RIGHT];
}

public function getNumberFormat(): array
{
$formatCode = '0.' . str_repeat('0', $this->decimalLength) . '%';

return ['formatCode' => $formatCode];
}
}
4 changes: 4 additions & 0 deletions src/ColumnSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Fastbolt\ExcelWriter\ColumnFormatter\DateFormatter;
use Fastbolt\ExcelWriter\ColumnFormatter\FloatFormatter;
use Fastbolt\ExcelWriter\ColumnFormatter\IntegerFormatter;
use Fastbolt\ExcelWriter\ColumnFormatter\PercentageFormatter;
use Fastbolt\ExcelWriter\ColumnFormatter\StringFormatter;

class ColumnSetting
Expand All @@ -20,6 +21,7 @@ class ColumnSetting
public const FORMAT_FLOAT = 'float';
public const FORMAT_STRING = 'string';
public const FORMAT_DATE = 'datetime';
public const FOMRAT_PERCENTAGE = 'percentage';

private string $format;
private string $name = ''; //excel-name for the column
Expand Down Expand Up @@ -92,6 +94,8 @@ public function getFormatter(): ColumnFormatter
return new IntegerFormatter();
case self::FORMAT_FLOAT:
return new FloatFormatter($this);
case self::FOMRAT_PERCENTAGE:
return new PercentageFormatter($this);
}

return new StringFormatter();
Expand Down

0 comments on commit 276c96a

Please sign in to comment.