[Operator] Perbaikan Halaman Pegawai dan Export Excel
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports;
|
||||
|
||||
use Maatwebsite\Excel\Concerns\FromArray;
|
||||
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
|
||||
use Maatwebsite\Excel\Concerns\WithFreezePane;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||
use Maatwebsite\Excel\DefaultValueBinder;
|
||||
use Override;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
/**
|
||||
* Hasil laporan operator (kolom + baris ReportResult) sebagai satu lembar Excel: baris kepala tebal dan dibekukan,
|
||||
* lebar kolom mengikuti isi. Teks ditulis apa adanya agar NIP/NIDN panjang dan nol di depan tidak diubah Excel.
|
||||
*/
|
||||
final class ReportExport extends DefaultValueBinder implements FromArray, ShouldAutoSize, WithCustomValueBinder, WithFreezePane, WithHeadings, WithStrictNullComparison, WithStyles, WithTitle
|
||||
{
|
||||
/** Batas Excel untuk nama lembar. */
|
||||
private const int TITLE_MAX = 31;
|
||||
|
||||
/**
|
||||
* @param list<array{key: string, label: string, num?: bool}> $columns
|
||||
* @param list<array<string, mixed>> $rows
|
||||
*/
|
||||
public function __construct(private readonly string $title, private readonly array $columns, private readonly array $rows) {}
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return array_column($this->columns, 'label');
|
||||
}
|
||||
|
||||
/** Nilai tiap baris menurut urutan kolom; kosong dan nilai bukan skalar menjadi sel kosong (0 tetap ditulis). */
|
||||
public function array(): array
|
||||
{
|
||||
return array_map(fn (array $row): array => array_map(function (array $column) use ($row): mixed {
|
||||
$value = $row[$column['key']] ?? null;
|
||||
|
||||
return is_scalar($value) && $value !== '' ? $value : null;
|
||||
}, $this->columns), $this->rows);
|
||||
}
|
||||
|
||||
/** Nama lembar yang diterima Excel: tanpa [ ] : * ? / \ dan paling banyak 31 karakter. */
|
||||
public function title(): string
|
||||
{
|
||||
$title = trim(mb_substr(preg_replace('/[\[\]:*?\/\\\\]+/u', ' ', $this->title) ?? '', 0, self::TITLE_MAX), " '");
|
||||
|
||||
return $title === '' ? 'Data' : $title;
|
||||
}
|
||||
|
||||
public function freezePane(): string
|
||||
{
|
||||
return 'A2';
|
||||
}
|
||||
|
||||
public function styles(Worksheet $sheet): array
|
||||
{
|
||||
return [1 => ['font' => ['bold' => true]]];
|
||||
}
|
||||
|
||||
/** Teks tetap teks: tidak ditebak sebagai angka (NIP 18 digit, nol di depan) maupun rumus (awalan "="). */
|
||||
#[Override]
|
||||
public function bindValue(Cell $cell, mixed $value): bool
|
||||
{
|
||||
if (is_string($value)) {
|
||||
$cell->setValueExplicit(StringHelper::sanitizeUTF8($value), DataType::TYPE_STRING);
|
||||
// Tanpa segitiga peringatan "angka disimpan sebagai teks" di Excel
|
||||
$cell->getIgnoredErrors()->setNumberStoredAsText(is_numeric($value));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return parent::bindValue($cell, $value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user