Fix for Xls writer wrong selected cells and active sheet

This commit is contained in:
Jimmy4o4 2019-11-25 08:41:55 +01:00 committed by Adrien Crivelli
parent cb18163a1d
commit 06d9dc03e9
4 changed files with 143 additions and 3 deletions

View File

@ -7,13 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org).
## [Unreleased] ## [Unreleased]
### Added
- Added support for the BASE function - Added support for the BASE function
- Added support for the ARABIC function - Added support for the ARABIC function
- Conditionals - Extend Support for (NOT)CONTAINSBLANKS [#1278](https://github.com/PHPOffice/PhpSpreadsheet/pull/1278) - Conditionals - Extend Support for (NOT)CONTAINSBLANKS [#1278](https://github.com/PHPOffice/PhpSpreadsheet/pull/1278)
### Fixed
- Handle Error in Formula Processing Better for Xls [#1267](https://github.com/PHPOffice/PhpSpreadsheet/pull/1267) - Handle Error in Formula Processing Better for Xls [#1267](https://github.com/PHPOffice/PhpSpreadsheet/pull/1267)
- Handle ConditionalStyle NumberFormat When Reading Xlsx File [#1296](https://github.com/PHPOffice/PhpSpreadsheet/pull/1296) - Handle ConditionalStyle NumberFormat When Reading Xlsx File [#1296](https://github.com/PHPOffice/PhpSpreadsheet/pull/1296)
- Fix Xlsx Writer's handling of decimal commas [#1282](https://github.com/PHPOffice/PhpSpreadsheet/pull/1282) - Fix Xlsx Writer's handling of decimal commas [#1282](https://github.com/PHPOffice/PhpSpreadsheet/pull/1282)
- Fix for issue by removing test code mistakenly left in [#1328](https://github.com/PHPOffice/PhpSpreadsheet/pull/1328) - Fix for issue by removing test code mistakenly left in [#1328](https://github.com/PHPOffice/PhpSpreadsheet/pull/1328)
- Fix for Xls writer wrong selected cells and active sheet [#1256](https://github.com/PHPOffice/PhpSpreadsheet/pull/1256)
## [1.10.1] - 2019-12-02 ## [1.10.1] - 2019-12-02

View File

@ -281,6 +281,10 @@ class Worksheet extends BIFFwriter
{ {
$phpSheet = $this->phpSheet; $phpSheet = $this->phpSheet;
// Storing selected cells and active sheet because it changes while parsing cells with formulas.
$selectedCells = $this->phpSheet->getSelectedCells();
$activeSheetIndex = $this->phpSheet->getParent()->getActiveSheetIndex();
// Write BOF record // Write BOF record
$this->storeBof(0x0010); $this->storeBof(0x0010);
@ -481,6 +485,9 @@ class Worksheet extends BIFFwriter
// Append // Append
$this->writeMsoDrawing(); $this->writeMsoDrawing();
// Restoring active sheet.
$this->phpSheet->getParent()->setActiveSheetIndex($activeSheetIndex);
// Write WINDOW2 record // Write WINDOW2 record
$this->writeWindow2(); $this->writeWindow2();
@ -493,6 +500,9 @@ class Worksheet extends BIFFwriter
$this->writePanes(); $this->writePanes();
} }
// Restoring selected cells.
$this->phpSheet->setSelectedCells($selectedCells);
// Write SELECTION record // Write SELECTION record
$this->writeSelection(); $this->writeSelection();
@ -1250,7 +1260,6 @@ class Worksheet extends BIFFwriter
$fFrozenNoSplit = 0; // 0 - bit $fFrozenNoSplit = 0; // 0 - bit
// no support in PhpSpreadsheet for selected sheet, therefore sheet is only selected if it is the active sheet // no support in PhpSpreadsheet for selected sheet, therefore sheet is only selected if it is the active sheet
$fSelected = ($this->phpSheet === $this->phpSheet->getParent()->getActiveSheet()) ? 1 : 0; $fSelected = ($this->phpSheet === $this->phpSheet->getParent()->getActiveSheet()) ? 1 : 0;
$fPaged = 1; // 2
$fPageBreakPreview = $this->phpSheet->getSheetView()->getView() === SheetView::SHEETVIEW_PAGE_BREAK_PREVIEW; $fPageBreakPreview = $this->phpSheet->getSheetView()->getView() === SheetView::SHEETVIEW_PAGE_BREAK_PREVIEW;
$grbit = $fDspFmla; $grbit = $fDspFmla;
@ -1262,8 +1271,8 @@ class Worksheet extends BIFFwriter
$grbit |= $fArabic << 6; $grbit |= $fArabic << 6;
$grbit |= $fDspGuts << 7; $grbit |= $fDspGuts << 7;
$grbit |= $fFrozenNoSplit << 8; $grbit |= $fFrozenNoSplit << 8;
$grbit |= $fSelected << 9; $grbit |= $fSelected << 9; // Selected sheets.
$grbit |= $fPaged << 10; $grbit |= $fSelected << 10; // Active sheet.
$grbit |= $fPageBreakPreview << 11; $grbit |= $fPageBreakPreview << 11;
$header = pack('vv', $record, $length); $header = pack('vv', $record, $length);

View File

@ -0,0 +1,68 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class ActiveSheetTest extends AbstractFunctional
{
public function providerFormats()
{
return [
['Xls'],
];
}
/**
* Test load file with correct active sheet.
*
* @dataProvider providerFormats
*
* @param string $format
*/
public function testActiveSheet($format)
{
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0)
->setTitle('Test1')
->setCellValue('D1', 1)
->setCellValue('D2', 2)
->setCellValue('D3', 3)
->setCellValue('D4', 4)
->setCellValue('D5', '=SUM(D1:D4)')
->setSelectedCell('B2');
$spreadsheet->createSheet(1);
$spreadsheet->setActiveSheetIndex(1)
->setTitle('Test2')
->setCellValue('D1', 4)
->setCellValue('E1', 3)
->setCellValue('F1', 2)
->setCellValue('G1', 1)
->setCellValue('H1', '=SUM(D1:G4)')
->setSelectedCells('A1:B2');
$spreadsheet->createSheet(2);
$spreadsheet->setActiveSheetIndex(2)
->setTitle('Test3')
->setCellValue('A1', 4)
->setCellValue('B1', 3)
->setCellValue('C1', 2)
->setCellValue('D1', 1)
->setCellValue('E1', '=SUM(A1:D4)')
->setSelectedCells('A1:D1');
$spreadsheet->setActiveSheetIndex(1);
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
// Original object.
self::assertSame(1, $spreadsheet->getActiveSheetIndex());
// Saved and reloaded file.
self::assertSame(1, $reloadedSpreadsheet->getActiveSheetIndex());
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Functional;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class SelectedCellsTest extends AbstractFunctional
{
public function providerFormats()
{
return [
['Xls'],
];
}
/**
* Test load file with correct selected cells.
*
* @dataProvider providerFormats
*
* @param string $format
*/
public function testSelectedCells($format)
{
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0)
->setTitle('Test1')
->setCellValue('D1', 1)
->setCellValue('D2', 2)
->setCellValue('D3', 3)
->setCellValue('D4', 4)
->setCellValue('D5', '=SUM(D1:D4)')
->setSelectedCell('B2');
$spreadsheet->createSheet(1);
$spreadsheet->setActiveSheetIndex(1)
->setTitle('Test2')
->setCellValue('D1', 4)
->setCellValue('E1', 3)
->setCellValue('F1', 2)
->setCellValue('G1', 1)
->setCellValue('H1', '=SUM(D1:G4)')
->setSelectedCells('A1:B2');
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
// Original object.
self::assertSame('B2', $spreadsheet->setActiveSheetIndex(0)->getSelectedCells());
self::assertSame('A1:B2', $spreadsheet->setActiveSheetIndex(1)->getSelectedCells());
// Saved and reloaded file.
self::assertSame('B2', $reloadedSpreadsheet->setActiveSheetIndex(0)->getSelectedCells());
self::assertSame('A1:B2', $reloadedSpreadsheet->setActiveSheetIndex(1)->getSelectedCells());
}
}