Skip to content

Commit 2ce7a2b

Browse files
committed
Test for calendar when reading Excel Files.
Set Excel calendar to be used for calculations and formatting for the calendar stored against the spreadsheet.
1 parent 95cf51b commit 2ce7a2b

File tree

9 files changed

+97
-14
lines changed

9 files changed

+97
-14
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -990,11 +990,6 @@ parameters:
990990
count: 6
991991
path: src/PhpSpreadsheet/Cell/Cell.php
992992

993-
-
994-
message: "#^Unreachable statement \\- code above always terminates\\.$#"
995-
count: 1
996-
path: src/PhpSpreadsheet/Cell/Cell.php
997-
998993
-
999994
message: "#^Call to an undefined method object\\:\\:getHashCode\\(\\)\\.$#"
1000995
count: 1
@@ -2580,11 +2575,6 @@ parameters:
25802575
count: 1
25812576
path: src/PhpSpreadsheet/Shared/OLERead.php
25822577

2583-
-
2584-
message: "#^Call to an undefined method PhpOffice\\\\PhpSpreadsheet\\\\Calculation\\\\Calculation\\:\\:_calculateFormulaValue\\(\\)\\.$#"
2585-
count: 1
2586-
path: src/PhpSpreadsheet/Shared/StringHelper.php
2587-
25882578
-
25892579
message: "#^Static method PhpOffice\\\\PhpSpreadsheet\\\\Shared\\\\TimeZone\\:\\:validateTimeZone\\(\\) is unused\\.$#"
25902580
count: 1

src/PhpSpreadsheet/Cell/Cell.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ public function getValue()
181181
*/
182182
public function getFormattedValue()
183183
{
184+
SharedDate::setExcelCalendar($this->getWorksheet()->getParent()->getExcelCalendar());
185+
184186
return (string) NumberFormat::toFormattedString(
185187
$this->getCalculatedValue(),
186188
$this->getStyle()
@@ -342,8 +344,6 @@ public function setValueExplicit($value, $dataType, bool $isArrayFormula = false
342344
break;
343345
default:
344346
throw new Exception('Invalid datatype: ' . $dataType);
345-
346-
break;
347347
}
348348

349349
// set the datatype
@@ -403,6 +403,8 @@ public function getCalculatedValue(bool $asArray = false, bool $resetLog = true)
403403
{
404404
if ($this->dataType === DataType::TYPE_FORMULA) {
405405
try {
406+
// SharedDate::setExcelCalendar($this->getWorksheet()->getParent()->getExcelCalendar());
407+
//
406408
$coordinate = $this->getCoordinate();
407409
$worksheet = $this->getWorksheet();
408410
$value = $this->value;

src/PhpSpreadsheet/Shared/Date.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Date
6767
protected static $defaultTimeZone;
6868

6969
/**
70-
* Set the Excel Default Calendar (Windows 1900 or Mac 1904).
70+
* Set the Excel Date Calendar (Windows 1900 or Mac 1904) used for calculations and formatting.
7171
*
7272
* @param int $baseYear Excel base date (1900 or 1904)
7373
*
@@ -85,7 +85,8 @@ public static function setExcelCalendar($baseYear)
8585
}
8686

8787
/**
88-
* Return the Excel Default Calendar (Windows 1900 or Mac 1904).
88+
* Return the Excel Date Calendar (Windows 1900 or Mac 1904)
89+
* to be used for current calculations and formatting.
8990
*
9091
* @return int Excel base date (1900 or 1904)
9192
*/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xls;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xls;
6+
use PhpOffice\PhpSpreadsheet\Shared\Date;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class DateReaderTest extends TestCase
10+
{
11+
protected function tearDown(): void
12+
{
13+
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
14+
}
15+
16+
public function testReadExcel1900Spreadsheet(): void
17+
{
18+
$filename = 'tests/data/Reader/XLS/1900_Calendar.xls';
19+
$reader = new Xls();
20+
$spreadsheet = $reader->load($filename);
21+
22+
self::assertSame(Date::CALENDAR_WINDOWS_1900, $spreadsheet->getExcelCalendar());
23+
24+
$worksheet = $spreadsheet->getActiveSheet();
25+
self::assertSame(44562, $worksheet->getCell('A1')->getValue());
26+
self::assertSame('2022-01-01', $worksheet->getCell('A1')->getFormattedValue());
27+
self::assertSame(44926, $worksheet->getCell('A2')->getValue());
28+
self::assertSame('2022-12-31', $worksheet->getCell('A2')->getFormattedValue());
29+
}
30+
31+
public function testReadExcel1904Spreadsheet(): void
32+
{
33+
$filename = 'tests/data/Reader/XLS/1904_Calendar.xls';
34+
$reader = new Xls();
35+
$spreadsheet = $reader->load($filename);
36+
37+
self::assertSame(Date::CALENDAR_MAC_1904, $spreadsheet->getExcelCalendar());
38+
39+
$worksheet = $spreadsheet->getActiveSheet();
40+
self::assertSame(43100, $worksheet->getCell('A1')->getValue());
41+
self::assertSame('2022-01-01', $worksheet->getCell('A1')->getFormattedValue());
42+
self::assertSame(43464, $worksheet->getCell('A2')->getValue());
43+
self::assertSame('2022-12-31', $worksheet->getCell('A2')->getFormattedValue());
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
6+
use PhpOffice\PhpSpreadsheet\Shared\Date;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class DateReaderTest extends TestCase
10+
{
11+
protected function tearDown(): void
12+
{
13+
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
14+
}
15+
16+
public function testReadExcel1900Spreadsheet(): void
17+
{
18+
$filename = 'tests/data/Reader/XLSX/1900_Calendar.xlsx';
19+
$reader = new Xlsx();
20+
$spreadsheet = $reader->load($filename);
21+
22+
self::assertSame(Date::CALENDAR_WINDOWS_1900, $spreadsheet->getExcelCalendar());
23+
24+
$worksheet = $spreadsheet->getActiveSheet();
25+
self::assertSame(44562, $worksheet->getCell('A1')->getValue());
26+
self::assertSame('2022-01-01', $worksheet->getCell('A1')->getFormattedValue());
27+
self::assertSame(44926, $worksheet->getCell('A2')->getValue());
28+
self::assertSame('2022-12-31', $worksheet->getCell('A2')->getFormattedValue());
29+
}
30+
31+
public function testReadExcel1904Spreadsheet(): void
32+
{
33+
$filename = 'tests/data/Reader/XLSX/1904_Calendar.xlsx';
34+
$reader = new Xlsx();
35+
$spreadsheet = $reader->load($filename);
36+
37+
self::assertSame(Date::CALENDAR_MAC_1904, $spreadsheet->getExcelCalendar());
38+
39+
$worksheet = $spreadsheet->getActiveSheet();
40+
self::assertSame(43100, $worksheet->getCell('A1')->getValue());
41+
self::assertSame('2022-01-01', $worksheet->getCell('A1')->getFormattedValue());
42+
self::assertSame(43464, $worksheet->getCell('A2')->getValue());
43+
self::assertSame('2022-12-31', $worksheet->getCell('A2')->getFormattedValue());
44+
}
45+
}
27.5 KB
Binary file not shown.
27.5 KB
Binary file not shown.
9.55 KB
Binary file not shown.
9.56 KB
Binary file not shown.

0 commit comments

Comments
 (0)