Perubahan NIP petik kolom NIP, NPWP, dan Nomor Rekening semua export excel
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exports\Concerns;
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Cell;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
|
||||
|
||||
/**
|
||||
* Memaksa kolom tertentu (NIP, NPWP, Nomor Rekening, dll) disimpan sebagai
|
||||
* string murni di level cell, sebagai pengganti trik prefix tanda petik (')
|
||||
* yang dulu dipakai supaya Excel tidak mengubah nilainya jadi angka.
|
||||
*
|
||||
* Trik prefix ('123...) membuat tanda petik ikut tampil di beberapa
|
||||
* aplikasi/versi Excel. Sedangkan hanya mengandalkan columnFormats('@') saja
|
||||
* tidak cukup untuk NIP 18 digit, karena PhpSpreadsheet sudah membulatkannya
|
||||
* jadi notasi ilmiah (presisi double ~15-17 digit) sebelum format diterapkan.
|
||||
*/
|
||||
trait ForcesTextColumns
|
||||
{
|
||||
/**
|
||||
* Kolom yang HARUS selalu disimpan sebagai teks (mis. NPWP, Nomor Rekening).
|
||||
*/
|
||||
protected function alwaysTextColumns(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Kolom berisi NIP. Hanya dipaksa jadi teks kalau nilainya memang angka
|
||||
* panjang (NIP asli), supaya baris Total/ringkasan yang kadang memakai
|
||||
* kolom yang sama tetap dianggap angka oleh Excel.
|
||||
*/
|
||||
protected function nipColumns(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function bindValue(Cell $cell, $value)
|
||||
{
|
||||
$column = $cell->getColumn();
|
||||
|
||||
$isNipValue = in_array($column, $this->nipColumns(), true)
|
||||
&& is_string($value) && strlen($value) >= 15 && ctype_digit($value);
|
||||
|
||||
if (($isNipValue || in_array($column, $this->alwaysTextColumns(), true)) && $value !== '') {
|
||||
$cell->setValueExplicit((string) $value, DataType::TYPE_STRING);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return (new DefaultValueBinder)->bindValue($cell, $value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user