diff --git a/src/Drivers/PhpSpreadsheetDriver.php b/src/Drivers/PhpSpreadsheetDriver.php index cb1999f..94b6600 100644 --- a/src/Drivers/PhpSpreadsheetDriver.php +++ b/src/Drivers/PhpSpreadsheetDriver.php @@ -52,6 +52,7 @@ public function load(string $file, \AnourValar\Office\Format $format): self public function save(string $file, \AnourValar\Office\Format $format): void { $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($this->spreadsheet, $this->getFormat($format)); + $this->sheet->setSelectedCells('A1'); if (method_exists($writer, 'writeAllSheets')) { $writer->writeAllSheets(); @@ -65,37 +66,30 @@ public function save(string $file, \AnourValar\Office\Format $format): void * * @param string $cell * @param mixed $value + * @param string $format * @return self */ - public function setValue(string $cell, $value): self + public function setValue(string $cell, $value, string $format = null): self { if ($value instanceof \DateTimeInterface) { $this->sheet->setCellValue($cell, \PhpOffice\PhpSpreadsheet\Shared\Date::PHPToExcel($value)); - $this->sheet->getStyle($cell)->getNumberFormat()->setFormatCode(static::DATE_FORMAT); - - } elseif (is_string($value) && preg_match('#^\=[A-Z][A-Z\.\d]#', $value)) { - - $this->sheet->setCellValue($cell, $value); + $this->setCellFormat($cell, ($format ?? static::DATE_FORMAT)); } elseif (is_string($value) || is_null($value)) { - $this->sheet->getCell($cell)->setValueExplicit((string) $value, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING); + if (is_numeric($value)) { + $this->sheet->getCell($cell)->setValueExplicit($value, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING); + } else { + $this->sheet->setCellValue($cell, $value); + } } else { if (is_double($value)) { - if (abs($value) >= 1000) { - $this->setCellFormat($cell, '# ##0.00'); - } else { - $this->setCellFormat($cell, '0.00'); - } + $this->setCellFormat($cell, ($format ?? '#,##0.00')); } elseif (is_integer($value)) { - if (abs($value) >= 1000) { - $this->setCellFormat($cell, '# ##0'); - } else { - $this->setCellFormat($cell, '0'); - } + $this->setCellFormat($cell, ($format ?? '#,##0')); } $this->sheet->getCell($cell)->setValueExplicit($value, \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_NUMERIC); @@ -105,6 +99,20 @@ public function setValue(string $cell, $value): self return $this; } + /** + * Set data format of cell (column, range) + * + * @param string $range + * @param string $format + * @return self + */ + public function setCellFormat(string $range, string $format): self + { + $this->sheet->getStyle($range)->getNumberFormat()->setFormatCode($format); + + return $this; + } + /** * {@inheritDoc} * @see \AnourValar\Office\Drivers\TemplateInterface::setValues() @@ -462,20 +470,4 @@ protected function getFormat(\AnourValar\Office\Format $format): string \AnourValar\Office\Format::Ods => 'Ods', }; } - - /** - * Установка формата ячейки - * - * @param string $cell - * @param string $format - * @return void - */ - private function setCellFormat(string $cell, string $format): void - { - $this - ->sheet - ->getStyle($cell) - ->getNumberFormat() - ->setFormatCode($format); - } } diff --git a/src/GridService.php b/src/GridService.php index 67968df..0293443 100644 --- a/src/GridService.php +++ b/src/GridService.php @@ -264,8 +264,15 @@ protected function getGenerator( $columns = []; if ($totalRange) { + $keys = array_keys($headers); + while ($firstColumn <= $lastColumn) { - $columns[] = $firstColumn; + if (! $keys) { + $columns[] = $firstColumn; + } else { + $columns[array_shift($keys)] = $firstColumn; + } + $firstColumn++; } } diff --git a/tests/GridServiceTest.php b/tests/GridServiceTest.php index a2ddb75..3ea9299 100644 --- a/tests/GridServiceTest.php +++ b/tests/GridServiceTest.php @@ -190,10 +190,10 @@ public function test_generate_statistic_with_headers_with_shift() $this->assertSame('C5:C5', $headersRange); $this->assertSame(null, $dataRange); $this->assertSame('C5:C5', $totalRange); - $this->assertSame(['C'], $columns); + $this->assertSame(['one' => 'C'], $columns); }) ->generate( - ['foo'], + ['one' => 'foo'], [ ], 'C5' ); @@ -355,10 +355,10 @@ public function test_generate_statistic_with_headers_with_shift() $this->assertSame('C5:E5', $headersRange); $this->assertSame('C6:E8', $dataRange); $this->assertSame('C5:E8', $totalRange); - $this->assertSame(['C', 'D', 'E'], $columns); + $this->assertSame(['one' => 'C', 'two' => 'D', 'three' => 'E'], $columns); }) ->generate( - ['foo', 'bar', 'baz'], + ['one' => 'foo', 'two' => 'bar', 'three' => 'baz'], [ ['foo-1', 'bar-1', 'baz-1'], ['foo-2', 'bar-2', 'baz-2'], ['foo-3', 'bar-3', 'baz-3'] ], 'C5' );