Skip to content

Commit d445646

Browse files
committed
Theme File Missing But Referenced in Spreadsheet
Fix PHPOffice#3770. A rels file points to a non-existent theme file in the spreadsheet. In other similar cases (e.g. PR PHPOffice#3771), Excel opens such a spreadsheet, but with an error pop-up. Not so with this file; it just opens the spreadsheet without the pop-up. PhpSpreadsheet will now account for this unusual situation as well.
1 parent db35a41 commit d445646

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/PhpSpreadsheet/Reader/Xlsx.php

+3
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
427427
}
428428
switch ($rel['Type']) {
429429
case "$xmlNamespaceBase/theme":
430+
if (!$this->fileExistsInArchive($zip, "xl/{$relTarget}")) {
431+
break; // issue3770
432+
}
430433
$themeOrderArray = ['lt1', 'dk1', 'lt2', 'dk2'];
431434
$themeOrderAdditional = count($themeOrderArray);
432435

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
8+
9+
class Issue3770Test extends \PHPUnit\Framework\TestCase
10+
{
11+
private static string $testbook = 'tests/data/Reader/XLSX/issue.3770.xlsx';
12+
13+
public function testPreliminaries(): void
14+
{
15+
$file = 'zip://';
16+
$file .= self::$testbook;
17+
$file .= '#xl/_rels/workbook.xml.rels';
18+
$data = file_get_contents($file);
19+
// rels file points to non-existent theme file
20+
if ($data === false) {
21+
self::fail('Unable to read file');
22+
} else {
23+
self::assertStringContainsString('Target="theme/theme1.xml"', $data);
24+
self::assertStringContainsString('Target="worksheets/sheet1.xml"', $data);
25+
}
26+
$file = 'zip://';
27+
$file .= self::$testbook;
28+
$file .= '#xl/theme/theme1.xml';
29+
$data = @file_get_contents($file);
30+
self::assertFalse($data);
31+
$file = 'zip://';
32+
$file .= self::$testbook;
33+
$file .= '#xl/worksheets/sheet1.xml';
34+
$data = file_get_contents($file);
35+
self::assertNotFalse($data);
36+
}
37+
38+
public function testLoadable(): void
39+
{
40+
$reader = new Xlsx();
41+
$spreadsheet = $reader->load(self::$testbook);
42+
$sheet = $spreadsheet->getActiveSheet();
43+
// Assert anything to confirm read succeeded
44+
self::assertSame('Универсальный передаточный документ', $sheet->getCell('A1')->getValue());
45+
$spreadsheet->disconnectWorksheets();
46+
}
47+
}
11.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)