Skip to content

Split Screens #3622

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 3 commits into from
Jun 28, 2023
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
54 changes: 43 additions & 11 deletions src/PhpSpreadsheet/Reader/Xlsx/SheetViews.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Worksheet\Pane;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use SimpleXMLElement;

Expand All @@ -18,6 +19,8 @@ class SheetViews extends BaseParserClass
/** @var Worksheet */
private $worksheet;

private string $activePane = '';

public function __construct(SimpleXMLElement $sheetViewXml, Worksheet $workSheet)
{
$this->sheetViewXml = $sheetViewXml;
Expand All @@ -35,11 +38,15 @@ public function load(): void
$this->direction();
$this->showZeros();

$usesPanes = false;
if (isset($this->sheetViewXml->pane)) {
$this->pane();
$usesPanes = true;
}
if (isset($this->sheetViewXml->selection, $this->sheetViewXml->selection->attributes()->sqref)) {
$this->selection();
if (isset($this->sheetViewXml->selection)) {
foreach ($this->sheetViewXml->selection as $selection) {
$this->selection($selection, $usesPanes);
}
}
}

Expand Down Expand Up @@ -127,30 +134,55 @@ private function pane(): void

if (isset($paneAttributes->xSplit)) {
$xSplit = (int) ($paneAttributes->xSplit);
$this->worksheet->setXSplit($xSplit);
}

if (isset($paneAttributes->ySplit)) {
$ySplit = (int) ($paneAttributes->ySplit);
$this->worksheet->setYSplit($ySplit);
}

$paneState = isset($paneAttributes->state) ? ((string) $paneAttributes->state) : '';
$this->worksheet->setPaneState($paneState);
if (isset($paneAttributes->topLeftCell)) {
$topLeftCell = (string) $paneAttributes->topLeftCell;
$this->worksheet->setPaneTopLeftCell($topLeftCell);
if ($paneState === Worksheet::PANE_FROZEN) {
$this->worksheet->setTopLeftCell($topLeftCell);
}
}
$activePane = isset($paneAttributes->activePane) ? ((string) $paneAttributes->activePane) : 'topLeft';
$this->worksheet->setActivePane($activePane);
$this->activePane = $activePane;
if ($paneState === Worksheet::PANE_FROZEN || $paneState === Worksheet::PANE_FROZENSPLIT) {
$this->worksheet->freezePane(
Coordinate::stringFromColumnIndex($xSplit + 1) . ($ySplit + 1),
$topLeftCell,
$paneState === Worksheet::PANE_FROZENSPLIT
);
}

$this->worksheet->freezePane(
Coordinate::stringFromColumnIndex($xSplit + 1) . ($ySplit + 1),
$topLeftCell
);
}

private function selection(): void
private function selection(?SimpleXMLElement $selection, bool $usesPanes): void
{
$attributes = $this->sheetViewXml->selection->attributes();
$attributes = ($selection === null) ? null : $selection->attributes();
if ($attributes !== null) {
$position = (string) $attributes->pane;
if ($usesPanes && $position === '') {
$position = 'topLeft';
}
$activeCell = (string) $attributes->activeCell;
$sqref = (string) $attributes->sqref;
$sqref = explode(' ', $sqref);
$sqref = $sqref[0];
$this->worksheet->setSelectedCells($sqref);
if ($position === '') {
$this->worksheet->setSelectedCells($sqref);
} else {
$pane = new Pane($position, $sqref, $activeCell);
$this->worksheet->setPane($position, $pane);
if ($position === $this->activePane && $sqref !== '') {
$this->worksheet->setSelectedCells($sqref);
}
}
}
}
}
39 changes: 38 additions & 1 deletion src/PhpSpreadsheet/Reader/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,44 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet, boo
if (isset($xmlX->WorksheetOptions->SplitVertical)) {
$freezeColumn = (int) $xmlX->WorksheetOptions->SplitVertical + 1;
}
$spreadsheet->getActiveSheet()->freezePane(Coordinate::stringFromColumnIndex($freezeColumn) . (string) $freezeRow);
$leftTopRow = (string) $xmlX->WorksheetOptions->TopRowBottomPane;
$leftTopColumn = (string) $xmlX->WorksheetOptions->LeftColumnRightPane;
if (is_numeric($leftTopRow) && is_numeric($leftTopColumn)) {
$leftTopCoordinate = Coordinate::stringFromColumnIndex((int) $leftTopColumn + 1) . (string) ($leftTopRow + 1);
$spreadsheet->getActiveSheet()->freezePane(Coordinate::stringFromColumnIndex($freezeColumn) . (string) $freezeRow, $leftTopCoordinate, !isset($xmlX->WorksheetOptions->FrozenNoSplit));
} else {
$spreadsheet->getActiveSheet()->freezePane(Coordinate::stringFromColumnIndex($freezeColumn) . (string) $freezeRow, null, !isset($xmlX->WorksheetOptions->FrozenNoSplit));
}
} elseif (isset($xmlX->WorksheetOptions->SplitVertical) || isset($xmlX->WorksheetOptions->SplitHorizontal)) {
if (isset($xmlX->WorksheetOptions->SplitHorizontal)) {
$ySplit = (int) $xmlX->WorksheetOptions->SplitHorizontal;
$spreadsheet->getActiveSheet()->setYSplit($ySplit);
}
if (isset($xmlX->WorksheetOptions->SplitVertical)) {
$xSplit = (int) $xmlX->WorksheetOptions->SplitVertical;
$spreadsheet->getActiveSheet()->setXSplit($xSplit);
}
if (isset($xmlX->WorksheetOptions->LeftColumnVisible) || isset($xmlX->WorksheetOptions->TopRowVisible)) {
$leftTopColumn = $leftTopRow = 1;
if (isset($xmlX->WorksheetOptions->LeftColumnVisible)) {
$leftTopColumn = 1 + (int) $xmlX->WorksheetOptions->LeftColumnVisible;
}
if (isset($xmlX->WorksheetOptions->TopRowVisible)) {
$leftTopRow = 1 + (int) $xmlX->WorksheetOptions->TopRowVisible;
}
$leftTopCoordinate = Coordinate::stringFromColumnIndex($leftTopColumn) . "$leftTopRow";
$spreadsheet->getActiveSheet()->setTopLeftCell($leftTopCoordinate);
}

$leftTopColumn = $leftTopRow = 1;
if (isset($xmlX->WorksheetOptions->LeftColumnRightPane)) {
$leftTopColumn = 1 + (int) $xmlX->WorksheetOptions->LeftColumnRightPane;
}
if (isset($xmlX->WorksheetOptions->TopRowBottomPane)) {
$leftTopRow = 1 + (int) $xmlX->WorksheetOptions->TopRowBottomPane;
}
$leftTopCoordinate = Coordinate::stringFromColumnIndex($leftTopColumn) . "$leftTopRow";
$spreadsheet->getActiveSheet()->setPaneTopLeftCell($leftTopCoordinate);
}
(new PageSettings($xmlX))->loadPageSettings($spreadsheet);
if (isset($xmlX->WorksheetOptions->TopRowVisible, $xmlX->WorksheetOptions->LeftColumnVisible)) {
Expand Down
48 changes: 48 additions & 0 deletions src/PhpSpreadsheet/Worksheet/Pane.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace PhpOffice\PhpSpreadsheet\Worksheet;

class Pane
{
private string $sqref = '';

private string $activeCell = '';

private string $position;

public function __construct(string $position, string $sqref = '', string $activeCell = '')
{
$this->sqref = $sqref;
$this->activeCell = $activeCell;
$this->position = $position;
}

public function getPosition(): string
{
return $this->position;
}

public function getSqref(): string
{
return $this->sqref;
}

public function setSqref(string $sqref): self
{
$this->sqref = $sqref;

return $this;
}

public function getActiveCell(): string
{
return $this->activeCell;
}

public function setActiveCell(string $activeCell): self
{
$this->activeCell = $activeCell;

return $this;
}
}
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Worksheet/Validations.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function validateCellRange($cellRange): string
$addressRange = (string) preg_replace(
['/^([A-Z]+):([A-Z]+)$/i', '/^(\\d+):(\\d+)$/'],
[self::SETMAXROW, self::SETMAXCOL],
$addressRange
$addressRange ?? ''
);

return empty($worksheet) ? strtoupper($addressRange) : $worksheet . '!' . strtoupper($addressRange);
Expand Down
Loading