Read printing area correctly when skipping some sheets

Fixes #371
This commit is contained in:
Adrien Crivelli 2018-02-25 19:38:53 +01:00
parent f58724ae0b
commit 1adc3a6688
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
4 changed files with 51 additions and 2 deletions

View File

@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Avoid potentially unsupported PSR-16 cache keys - [#354](https://github.com/PHPOffice/PhpSpreadsheet/issues/354) - Avoid potentially unsupported PSR-16 cache keys - [#354](https://github.com/PHPOffice/PhpSpreadsheet/issues/354)
- Check for MIME type to know if CSV reader can read a file - [#167](https://github.com/PHPOffice/PhpSpreadsheet/issues/167) - Check for MIME type to know if CSV reader can read a file - [#167](https://github.com/PHPOffice/PhpSpreadsheet/issues/167)
- Use proper € symbol for currency format - [#379](https://github.com/PHPOffice/PhpSpreadsheet/pull/379) - Use proper € symbol for currency format - [#379](https://github.com/PHPOffice/PhpSpreadsheet/pull/379)
- Read printing area correctly when skipping some sheets - [#371](https://github.com/PHPOffice/PhpSpreadsheet/issues/371)
## [1.1.0] - 2018-01-28 ## [1.1.0] - 2018-01-28

View File

@ -1700,7 +1700,7 @@ class Xlsx extends BaseReader
} }
// Some definedNames are only applicable if we are on the same sheet... // Some definedNames are only applicable if we are on the same sheet...
if ((string) $definedName['localSheetId'] != '' && (string) $definedName['localSheetId'] == $sheetId) { if ((string) $definedName['localSheetId'] != '' && (string) $definedName['localSheetId'] == $oldSheetId) {
// Switch on type // Switch on type
switch ((string) $definedName['name']) { switch ((string) $definedName['name']) {
case '_xlnm._FilterDatabase': case '_xlnm._FilterDatabase':

View File

@ -17,16 +17,20 @@ abstract class AbstractFunctional extends TestCase
* *
* @param Spreadsheet $spreadsheet * @param Spreadsheet $spreadsheet
* @param string $format * @param string $format
* @param null|callable $readerCustomizer
* *
* @return Spreadsheet * @return Spreadsheet
*/ */
protected function writeAndReload(Spreadsheet $spreadsheet, $format) protected function writeAndReload(Spreadsheet $spreadsheet, $format, callable $readerCustomizer = null)
{ {
$filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test'); $filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test');
$writer = IOFactory::createWriter($spreadsheet, $format); $writer = IOFactory::createWriter($spreadsheet, $format);
$writer->save($filename); $writer->save($filename);
$reader = IOFactory::createReader($format); $reader = IOFactory::createReader($format);
if ($readerCustomizer) {
$readerCustomizer($reader);
}
$reloadedSpreadsheet = $reader->load($filename); $reloadedSpreadsheet = $reader->load($filename);
unlink($filename); unlink($filename);

View File

@ -0,0 +1,44 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\Reader\BaseReader;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class PrintAreaTest extends AbstractFunctional
{
public function providerFormats()
{
return [
['Xls'],
['Xlsx'],
];
}
/**
* @dataProvider providerFormats
*
* @param string $format
*/
public function testPageSetup($format)
{
// Create new workbook with 3 sheets and different print areas
$spreadsheet = new Spreadsheet();
$worksheet1 = $spreadsheet->getActiveSheet()->setTitle('Sheet 1');
$worksheet1->getPageSetup()->setPrintArea('A1:B1');
for ($i = 2; $i < 4; ++$i) {
$sheet = $spreadsheet->createSheet()->setTitle("Sheet $i");
$sheet->getPageSetup()->setPrintArea("A$i:B$i");
}
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format, function (BaseReader $reader) {
$reader->setLoadSheetsOnly(['Sheet 1', 'Sheet 3']);
});
$actual1 = $reloadedSpreadsheet->getSheetByName('Sheet 1')->getPageSetup()->getPrintArea();
$actual3 = $reloadedSpreadsheet->getSheetByName('Sheet 3')->getPageSetup()->getPrintArea();
self::assertSame('A1:B1', $actual1, 'should be able to write and read normal page setup');
self::assertSame('A3:B3', $actual3, 'should be able to write and read page setup even when skipping sheets');
}
}