Skip to content

Commit fcb5efc

Browse files
authored
Merge pull request #4480 from oleibman/issue4477
Xlsx Reader Do Not Process Printer Settings for Dataonly
2 parents 6b2767c + 0cbaf10 commit fcb5efc

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
3434
- Print Area and Row Break. [Issue #1275](https://github.com/PHPOffice/PhpSpreadsheet/issues/1275) [PR #4450](https://github.com/PHPOffice/PhpSpreadsheet/pull/4450)
3535
- Copy Styles after insertNewColumnBefore. [Issue #1425](https://github.com/PHPOffice/PhpSpreadsheet/issues/1425) [PR #4468](https://github.com/PHPOffice/PhpSpreadsheet/pull/4468)
3636
- Xls Writer Treat Hyperlink Starting with # as Internal. [Issue #56](https://github.com/PHPOffice/PhpSpreadsheet/issues/56) [PR #4453](https://github.com/PHPOffice/PhpSpreadsheet/pull/4453)
37-
- ODS Handling of Ceiling and Floor. [Issue #407](https://github.com/PHPOffice/PhpSpreadsheet/issues/407) [PR #4466](https://github.com/PHPOffice/PhpSpreadsheet/pull/4466)
37+
- ODS Handling of Ceiling and Floor. [Issue #477](https://github.com/PHPOffice/PhpSpreadsheet/issues/407) [PR #4466](https://github.com/PHPOffice/PhpSpreadsheet/pull/4466)
38+
- Xlsx Reader Do Not Process Printer Settings for Dataonly. [Issue #4477](https://github.com/PHPOffice/PhpSpreadsheet/issues/4477) [PR #4480](https://github.com/PHPOffice/PhpSpreadsheet/pull/4480)
3839

3940
## 2025-04-16 - 4.2.0
4041

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,6 +2231,9 @@ private function readFormControlProperties(Spreadsheet $excel, string $dir, stri
22312231
/** @param mixed[][][][] $unparsedLoadedData */
22322232
private function readPrinterSettings(Spreadsheet $excel, string $dir, string $fileWorksheet, Worksheet $docSheet, array &$unparsedLoadedData): void
22332233
{
2234+
if ($this->readDataOnly) {
2235+
return;
2236+
}
22342237
$zip = $this->zip;
22352238
if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') === false) {
22362239
return;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
8+
use PhpOffice\PhpSpreadsheet\Shared\File;
9+
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
10+
use PHPUnit\Framework\TestCase;
11+
use ZipArchive;
12+
13+
class Issue4477Test extends TestCase
14+
{
15+
private string $tempfile = '';
16+
17+
protected function tearDown(): void
18+
{
19+
if ($this->tempfile !== '') {
20+
unlink($this->tempfile);
21+
$this->tempfile = '';
22+
}
23+
}
24+
25+
public function testDataonlyNoPrinter(): void
26+
{
27+
// Need to ignore printer settings when Read Dataonly
28+
$infile = 'tests/data/Reader/XLSX/issue.4477.disclaimer.xlsx';
29+
$zip = new ZipArchive();
30+
if ($zip->open($infile) !== true) {
31+
self::fail("failed to open $infile");
32+
}
33+
$num = $zip->numFiles;
34+
$foundPrinter = $foundWorksheet = false;
35+
for ($i = 0; $i < $num; ++$i) {
36+
$filename = (string) $zip->getNameIndex($i);
37+
if (str_contains($filename, 'printer')) {
38+
$foundPrinter = true;
39+
} elseif ($filename === 'xl/worksheets/sheet1.xml') {
40+
$foundWorksheet = true;
41+
}
42+
}
43+
$zip->close();
44+
self::assertTrue($foundPrinter);
45+
self::assertTrue($foundWorksheet);
46+
47+
$reader = new XlsxReader();
48+
$reader->setReadDataOnly(true);
49+
$spreadsheet = $reader->load($infile);
50+
$writer = new XlsxWriter($spreadsheet);
51+
$this->tempfile = File::temporaryFileName();
52+
$writer->save($this->tempfile);
53+
$spreadsheet->disconnectWorksheets();
54+
55+
$zip = new ZipArchive();
56+
if ($zip->open($this->tempfile) !== true) {
57+
self::fail("failed to open {$infile}");
58+
}
59+
$num = $zip->numFiles;
60+
$foundPrinter = $foundWorksheet = false;
61+
for ($i = 0; $i < $num; ++$i) {
62+
$filename = (string) $zip->getNameIndex($i);
63+
if (str_contains($filename, 'printer')) {
64+
$foundPrinter = true;
65+
} elseif ($filename === 'xl/worksheets/sheet1.xml') {
66+
$foundWorksheet = true;
67+
}
68+
}
69+
$zip->close();
70+
self::assertFalse($foundPrinter);
71+
self::assertTrue($foundWorksheet);
72+
}
73+
}
Binary file not shown.

0 commit comments

Comments
 (0)