Skip to content

Commit 11bae5c

Browse files
authored
Merge pull request #3352 from PHPOffice/PR3329-Additional-Exception-Handling
Additional exception handling to prevent an invalid formula from breaking `isDateTime()`
2 parents 5fb76c8 + 8c91bd6 commit 11bae5c

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/PhpSpreadsheet/Shared/Date.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,18 @@ public static function isDateTime(Cell $cell, $value = null, bool $dateWithoutTi
375375
if ($worksheet !== null && $spreadsheet !== null) {
376376
$index = $spreadsheet->getActiveSheetIndex();
377377
$selected = $worksheet->getSelectedCells();
378-
$result = is_numeric($value ?? $cell->getCalculatedValue()) &&
379-
self::isDateTimeFormat(
380-
$worksheet->getStyle(
381-
$cell->getCoordinate()
382-
)->getNumberFormat(),
383-
$dateWithoutTimeOkay
384-
);
378+
379+
try {
380+
$result = is_numeric($value ?? $cell->getCalculatedValue()) &&
381+
self::isDateTimeFormat(
382+
$worksheet->getStyle(
383+
$cell->getCoordinate()
384+
)->getNumberFormat(),
385+
$dateWithoutTimeOkay
386+
);
387+
} catch (Exception $e) {
388+
// Result is already false, so no need to actually do anything here
389+
}
385390
$worksheet->setSelectedCells($selected);
386391
$spreadsheet->setActiveSheetIndex($index);
387392
}

tests/PhpSpreadsheetTests/Shared/DateTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,5 +262,14 @@ public function testVarious(): void
262262
->getNumberFormat()
263263
->setFormatCode('0.00E+00');
264264
self::assertFalse(null !== $cella3 && Date::isDateTime($cella3));
265+
266+
$cella4 = $sheet->getCell('A4');
267+
self::assertNotNull($cella4);
268+
269+
$cella4->setValue('= 44 7510557347');
270+
$sheet->getStyle('A4')
271+
->getNumberFormat()
272+
->setFormatCode('yyyy-mm-dd');
273+
self::assertFalse(Date::isDateTime($cella4));
265274
}
266275
}

0 commit comments

Comments
 (0)