wi-events/classes/Spreadsheet.php

104 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2023-03-22 21:13:12 +01:00
<?php
namespace Zoomyboy\Event\Classes;
use PhpOffice\PhpSpreadsheet\Spreadsheet as BaseSpreadsheet;
2023-03-27 23:10:51 +02:00
use PhpOffice\PhpSpreadsheet\Style\Alignment as AlignmentStyle;
use PhpOffice\PhpSpreadsheet\Style\Fill as FillStyle;
2023-03-22 21:13:12 +01:00
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class Spreadsheet
{
private array $headers;
private $sheet;
2023-03-27 23:10:51 +02:00
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'],
],
];
2023-03-22 21:13:12 +01:00
public function __construct(string $title)
{
$this->spreadsheet = new BaseSpreadsheet();
$this->spreadsheet->getProperties()
->setTitle($title)
->setSubject($title)
->setDescription($title);
2023-03-27 23:10:51 +02:00
$this->spreadsheet->getDefaultStyle()->getFont()->setSize(12);
2023-03-22 21:13:12 +01:00
$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);
2023-03-27 23:24:12 +02:00
$writer->save(sys_get_temp_dir().'/spreadsheet.xlsx');
2023-03-22 21:13:12 +01:00
$this->spreadsheet->disconnectWorksheets();
unset($this->spreadsheet);
2023-03-27 23:10:51 +02:00
return sys_get_temp_dir().'/spreadsheet.xlsx';
2023-03-22 21:13:12 +01:00
}
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();
2023-03-27 23:10:51 +02:00
$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');
2023-03-22 21:13:12 +01:00
return $this;
}
}