Skip to content

Xlsx Reader Do Not Process Printer Settings for Dataonly #4480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Print Area and Row Break. [Issue #1275](https://github.com/PHPOffice/PhpSpreadsheet/issues/1275) [PR #4450](https://github.com/PHPOffice/PhpSpreadsheet/pull/4450)
- Copy Styles after insertNewColumnBefore. [Issue #1425](https://github.com/PHPOffice/PhpSpreadsheet/issues/1425) [PR #4468](https://github.com/PHPOffice/PhpSpreadsheet/pull/4468)
- 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)
- ODS Handling of Ceiling and Floor. [Issue #407](https://github.com/PHPOffice/PhpSpreadsheet/issues/407) [PR #4466](https://github.com/PHPOffice/PhpSpreadsheet/pull/4466)
- ODS Handling of Ceiling and Floor. [Issue #477](https://github.com/PHPOffice/PhpSpreadsheet/issues/407) [PR #4466](https://github.com/PHPOffice/PhpSpreadsheet/pull/4466)
- 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)

## 2025-04-16 - 4.2.0

Expand Down
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -2231,6 +2231,9 @@ private function readFormControlProperties(Spreadsheet $excel, string $dir, stri
/** @param mixed[][][][] $unparsedLoadedData */
private function readPrinterSettings(Spreadsheet $excel, string $dir, string $fileWorksheet, Worksheet $docSheet, array &$unparsedLoadedData): void
{
if ($this->readDataOnly) {
return;
}
$zip = $this->zip;
if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') === false) {
return;
Expand Down
73 changes: 73 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/Issue4477Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
use PHPUnit\Framework\TestCase;
use ZipArchive;

class Issue4477Test extends TestCase
{
private string $tempfile = '';

protected function tearDown(): void
{
if ($this->tempfile !== '') {
unlink($this->tempfile);
$this->tempfile = '';
}
}

public function testDataonlyNoPrinter(): void
{
// Need to ignore printer settings when Read Dataonly
$infile = 'tests/data/Reader/XLSX/issue.4477.disclaimer.xlsx';
$zip = new ZipArchive();
if ($zip->open($infile) !== true) {
self::fail("failed to open $infile");
}
$num = $zip->numFiles;
$foundPrinter = $foundWorksheet = false;
for ($i = 0; $i < $num; ++$i) {
$filename = (string) $zip->getNameIndex($i);
if (str_contains($filename, 'printer')) {
$foundPrinter = true;
} elseif ($filename === 'xl/worksheets/sheet1.xml') {
$foundWorksheet = true;
}
}
$zip->close();
self::assertTrue($foundPrinter);
self::assertTrue($foundWorksheet);

$reader = new XlsxReader();
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($infile);
$writer = new XlsxWriter($spreadsheet);
$this->tempfile = File::temporaryFileName();
$writer->save($this->tempfile);
$spreadsheet->disconnectWorksheets();

$zip = new ZipArchive();
if ($zip->open($this->tempfile) !== true) {
self::fail("failed to open {$infile}");
}
$num = $zip->numFiles;
$foundPrinter = $foundWorksheet = false;
for ($i = 0; $i < $num; ++$i) {
$filename = (string) $zip->getNameIndex($i);
if (str_contains($filename, 'printer')) {
$foundPrinter = true;
} elseif ($filename === 'xl/worksheets/sheet1.xml') {
$foundWorksheet = true;
}
}
$zip->close();
self::assertFalse($foundPrinter);
self::assertTrue($foundWorksheet);
}
}
Binary file added tests/data/Reader/XLSX/issue.4477.disclaimer.xlsx
Binary file not shown.