Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
AnourValar committed Apr 11, 2022
1 parent 2af22d9 commit 45843c0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
58 changes: 25 additions & 33 deletions src/Drivers/PhpSpreadsheetDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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()
Expand Down Expand Up @@ -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);
}
}
9 changes: 8 additions & 1 deletion src/GridService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/GridServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
Expand Down Expand Up @@ -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'
);
Expand Down

0 comments on commit 45843c0

Please sign in to comment.