105 lines
3.5 KiB
PHP
105 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Zoomyboy\Event\Classes;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet as BaseSpreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Style\Alignment as AlignmentStyle;
|
|
use PhpOffice\PhpSpreadsheet\Style\Fill as FillStyle;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
class Spreadsheet
|
|
{
|
|
private array $headers;
|
|
private $sheet;
|
|
private $warningStyle = [
|
|
'font' => [
|
|
'size' => 10,
|
|
'bold' => true,
|
|
'color' => [
|
|
'rgb' => 'ff0000',
|
|
],
|
|
],
|
|
'alignment' => [
|
|
'vertical' => AlignmentStyle::VERTICAL_CENTER,
|
|
],
|
|
];
|
|
private $headerStyle = [
|
|
'font' => [
|
|
'size' => 12,
|
|
'bold' => true,
|
|
'color' => [
|
|
'rgb' => 'e9fcfc',
|
|
],
|
|
],
|
|
'alignment' => [
|
|
'vertical' => AlignmentStyle::VERTICAL_CENTER,
|
|
],
|
|
'fill' => [
|
|
'fillType' => FillStyle::FILL_SOLID,
|
|
'color' => ['rgb' => '117878'],
|
|
],
|
|
];
|
|
|
|
public function __construct(string $title)
|
|
{
|
|
$this->spreadsheet = new BaseSpreadsheet();
|
|
$this->spreadsheet->getProperties()
|
|
->setTitle($title)
|
|
->setSubject($title)
|
|
->setDescription($title);
|
|
$this->spreadsheet->getDefaultStyle()->getFont()->setSize(12);
|
|
$this->spreadsheet->getActiveSheet()->setTitle('unfilled');
|
|
}
|
|
|
|
public function headers(array $headers): self
|
|
{
|
|
$this->headers = $headers;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function generate()
|
|
{
|
|
$this->spreadsheet->setActiveSheetIndex(0);
|
|
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->spreadsheet);
|
|
$writer->save('/home/pille/spreadsheet.xlsx');
|
|
// $writer->save(sys_get_temp_dir().'/spreadsheet.xlsx');
|
|
$this->spreadsheet->disconnectWorksheets();
|
|
unset($this->spreadsheet);
|
|
|
|
return sys_get_temp_dir().'/spreadsheet.xlsx';
|
|
}
|
|
|
|
public function sheet(string $name, array $data): self
|
|
{
|
|
if ('unfilled' === $this->spreadsheet->getActiveSheet()->getTitle()) {
|
|
$this->spreadsheet->getActiveSheet()->setTitle($name);
|
|
|
|
return $this->fill($data);
|
|
}
|
|
|
|
$this->spreadsheet->addSheet(new Worksheet($this->spreadsheet, $name));
|
|
$this->spreadsheet->setActiveSheetIndexByName($name);
|
|
|
|
return $this->fill($data);
|
|
}
|
|
|
|
private function fill(array $data): self
|
|
{
|
|
$headers = collect($this->headers)->map(fn ($header) => $header->name)->toArray();
|
|
$this->spreadsheet->getActiveSheet()->fromArray(['Achtung: Dieses Dokument wird bei neuen Teilnehmern automatisch aktualisiert. Gespeicherte Änderungen können daher jederzeit verloren gehen. Wenn du dieses Dokument bearbeiten willst, speichere es bitte an einem anderen Ort ab und editiere es dann.'], null, 'A1');
|
|
$this->spreadsheet->getActiveSheet()->fromArray($headers, null, 'A2');
|
|
$this->spreadsheet->getActiveSheet()->fromArray($data, null, 'A3');
|
|
$this->spreadsheet->getActiveSheet()->getStyle('A2:Z2')->applyFromArray($this->headerStyle);
|
|
$this->spreadsheet->getActiveSheet()->getStyle('A1')->applyFromArray($this->warningStyle);
|
|
|
|
foreach (range('A', 'Z') as $col) {
|
|
$this->spreadsheet->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
|
|
}
|
|
$this->spreadsheet->getActiveSheet()->getRowDimension(2)->setRowHeight(18);
|
|
$this->spreadsheet->getActiveSheet()->mergeCells('A1:Z1');
|
|
|
|
return $this;
|
|
}
|
|
}
|