Skip to content

Commit

Permalink
Fix for Xls BIFF Window2 record structure where selected and active s…
Browse files Browse the repository at this point in the history
…heets are stored.

Added some tests for active sheet and selected cells in Xls.
  • Loading branch information
jimmy4o4 committed Jan 21, 2020
1 parent b711923 commit 1991cd6
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/PhpSpreadsheet/Writer/Xls/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,6 @@ private function writeWindow2()
$fFrozenNoSplit = 0; // 0 - bit
// 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;
$fPaged = 1; // 2
$fPageBreakPreview = $this->phpSheet->getSheetView()->getView() === SheetView::SHEETVIEW_PAGE_BREAK_PREVIEW;

$grbit = $fDspFmla;
Expand All @@ -1272,8 +1271,8 @@ private function writeWindow2()
$grbit |= $fArabic << 6;
$grbit |= $fDspGuts << 7;
$grbit |= $fFrozenNoSplit << 8;
$grbit |= $fSelected << 9;
$grbit |= $fPaged << 10;
$grbit |= $fSelected << 9; // Selected sheets.
$grbit |= $fSelected << 10; // Active sheet.
$grbit |= $fPageBreakPreview << 11;

$header = pack('vv', $record, $length);
Expand Down
68 changes: 68 additions & 0 deletions tests/PhpSpreadsheetTests/Functional/ActiveSheetTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
57 changes: 57 additions & 0 deletions tests/PhpSpreadsheetTests/Functional/SelectedCellsTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 1991cd6

Please sign in to comment.