123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- declare(strict_types=1);
- namespace catcher\library\excel;
- trait MacroExcel
- {
-
- protected $start = 'A';
-
- protected $row = 1;
-
- protected $columns = [];
-
- protected function getStartSheet(): string
- {
- if (method_exists($this->excel, 'start')) {
- $this->start = $this->excel->start();
- }
- return $this->start;
- }
-
- protected function setSheetWidth()
- {
- if (method_exists($this->excel, 'setWidth')) {
- $width = $this->excel->setWidth();
- foreach ($width as $sheet => $w) {
- $this->getWorksheet()->getColumnDimension($sheet)->setWidth($w);
- }
- }
- }
-
- protected function before()
- {
- if (method_exists($this->excel, 'before')) {
- $this->excel->before();
- }
- }
-
- protected function getSheetColumns()
- {
- if (empty($this->columns)) {
- $start = $this->getStartSheet();
- $columns = [];
-
- foreach ($this->excel->headers() as $k => $header) {
- $columns[] = chr(ord($start) + $k);
- }
- return $columns;
- }
- return $this->columns;
- }
-
- protected function getKeys()
- {
- if (method_exists($this->excel, 'keys')) {
- return $this->excel->keys();
- }
- return [];
- }
-
- protected function getStartRow()
- {
- if (method_exists($this->excel, 'setRow')) {
- $this->row = $this->excel->setRow();
- }
- return $this->row;
- }
-
- protected function setTitle()
- {
- if (method_exists($this->excel, 'setTitle')) {
- [$cells, $title, $style] = $this->excel->setTitle();
- $this->getWorksheet()
- ->mergeCells($cells)
- ->setCellValue(explode(':', $cells)[0], $title)
- ->getStyle($cells)
- ->getAlignment()
- ->setHorizontal($style);
- }
- }
-
- protected function registerWorksheet()
- {
- if (method_exists($this->excel, 'getWorksheet')) {
- $this->excel->getWorksheet($this->getWorksheet());
- }
- }
-
- protected function incRow()
- {
- ++$this->row;
- }
-
- public function setMemoryLimit()
- {
- if (property_exists($this->excel, 'memory')) {
- ini_set('memory_limit', $this->excel->memory);
- }
- }
- }
|