Reader\Xlsx::setLoadSheetsOnly() has an impact on getPrintArea() #371
Closed
Description
This is:
- [X] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)
What is the expected behavior?
Both readers should have the same behaviour: getPrintArea() should report expected values regardless of previous call to setLoadSheetsOnly().
What is the current behavior?
Using setLoadSheetsOnly() for the Xlsx reader makes getPrintArea() return print areas of unloaded sheets.
What are the steps to reproduce?
Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:
<?php
require __DIR__ . '/vendor/autoload.php';
const FILENAME = 'printArea_bug';
const FORMATS = ['Xls', 'Xlsx'];
# Create new workbook with 3 sheets and different print areas
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$worksheet1 = $spreadsheet->getActiveSheet()->setTitle('W1');
$worksheet1->getPageSetup()->setPrintArea('A1:B1');
for ($i = 2; $i < 5; $i++) {
$worksheet = $spreadsheet->createSheet()->setTitle("W$i");
$worksheet->getPageSetup()->setPrintArea("A$i:B$i");
}
# save the file in various formats
foreach (FORMATS as $fileFormat) {
$writerClass = "\\PhpOffice\\PhpSpreadsheet\\Writer\\$fileFormat";
$writer = new $writerClass($spreadsheet);
$path = FILENAME . ".$fileFormat";
$writer->save($path); // the file is as expected with Excel.
}
# Read the same content in Xls and Xlsx formats, showing discrepancy with getPrintArea()
foreach (FORMATS as $fileFormat)
{
$path = FILENAME . ".$fileFormat";
$reader = PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($path);
# load only sheets that name meet our criteria
$reader->setLoadSheetsOnly(['W1', 'W3']);
$workbook = $reader->load($path);
# display print areas
foreach ($workbook->getWorksheetIterator() as $index => $sheet) {
echo get_class($reader)." > {$sheet->getTitle()} print area: {$sheet->getPageSetup()->getPrintArea()}\n";
}
}
Which versions of PhpSpreadsheet and PHP are affected?
branch develop, PHP 5.6
Metadata
Assignees
Labels
No labels