Skip to content

Commit

Permalink
https://github.com/aVadim483/fast-excel-templator/issues/10
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Jun 16, 2024
1 parent b45c7ac commit bf0222e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/FastExcelTemplator/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ public function getSheet(?string $name = null, ?string $areaRange = null, ?bool
return parent::getSheet($name, $areaRange, $firstRowKeys);
}

/**
* Array of all sheets
*
* @return Sheet[]
*/
public function sheets(): array
{
return $this->sheets;
}

/**
* @param array $params
*
Expand Down
9 changes: 4 additions & 5 deletions src/FastExcelTemplator/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,14 +477,13 @@ public function lastWrittenRowNum(): int
private function _writeWithStyle($cellAddress, $cellAddressIdx, $cellData)
{
$numberFormatType = null;
if ($cellData['t'] === 'date' && is_numeric($cellData['o'])) {
if (!empty($cellData['f'])) {
$this->sheetWriter->_writeToCellByIdx($cellAddressIdx, $cellData['f']);
}
elseif ($cellData['t'] === 'date' && is_numeric($cellData['o'])) {
$this->sheetWriter->_writeToCellByIdx($cellAddressIdx, $cellData['o']);
$numberFormatType = 'n_auto';
}
elseif (!empty($cellData['f'])) {
///$this->sheetWriter->writeTo($cellAddress, $cellData['f']);
$this->sheetWriter->_writeToCellByIdx($cellAddressIdx, $cellData['f']);
}
else {
if ($cellData['t'] === 'date') {
$pattern = $this->excel->getDateFormatPattern($cellData['s']);
Expand Down
47 changes: 47 additions & 0 deletions tests/FastExcelTemplatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,53 @@ public function test02()
$colAttributes = $sheet->getColAttributes();
$this->assertEquals(250, (int)$colAttributes['C']['width']);
$this->assertEquals(250, (int)$colAttributes['D']['width']);

unlink($out);
}

public function test03()
{
$tpl = __DIR__ . '/files/test-formulas.xlsx';
$out = __DIR__ . '/files/test-formulas-out.xlsx';

$excel = Excel::template($tpl, $out);

foreach ($excel->sheets() as $sheet) {
$sheet->transferRows();
}

$excel->save();
$this->assertTrue(is_file($out));

$excelReader = \avadim\FastExcelReader\Excel::open($out);
$excelReader->dateFormatter(true);
$cells = $excelReader->readCellsWithStyles();

$c1 = $cells['B2'];unset($c1['s']);
$c0 = ['v' => '23.01.1985', 'f' => null, 'o' => '31070', 't' => 'date'];
$this->assertEquals($c0, $c1);

$c1 = $cells['C2'];unset($c1['s']);
$c0 = ['v' => '=B2+1', 'f' => '=B2+1', 'o' => '=B2+1', 't' => 'date'];
$this->assertEquals($c0, $c1);

$c1 = $cells['C3'];unset($c1['s']);
$c0 = ['v' => '=Sheet2!B2', 'f' => '=Sheet2!B2', 'o' => '=Sheet2!B2', 't' => 'date'];
$this->assertEquals($c0, $c1);

$c1 = $cells['C6'];unset($c1['s']);
$c0 = ['v' => '=TestDate1+1', 'f' => '=TestDate1+1', 'o' => '=TestDate1+1', 't' => 'date'];
$this->assertEquals($c0, $c1);

$c1 = $cells['C9'];unset($c1['s']);
$c0 = ['v' => '="qwe" & TestDate1', 'f' => '="qwe" & TestDate1', 'o' => '="qwe" & TestDate1', 't' => ''];
$this->assertEquals($c0, $c1);

$c1 = $cells['C10'];unset($c1['s']);
$c0 = ['v' => '=SUM(C2:C9)', 'f' => '=SUM(C2:C9)', 'o' => '=SUM(C2:C9)', 't' => ''];
$this->assertEquals($c0, $c1);

unlink($out);
}
}

Binary file added tests/files/test-formulas.xlsx
Binary file not shown.

0 comments on commit bf0222e

Please sign in to comment.