Merge pull request #2652 from PHPOffice/Gnumeric-Selected-Cells-in-Worksheet

Set Selected Cells for Worksheets in Gnumeric Loader
This commit is contained in:
Mark Baker 2022-03-05 19:41:54 +01:00 committed by GitHub
commit ac33bd9010
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 4 deletions

View File

@ -19,11 +19,11 @@ and this project adheres to [Semantic Versioning](https://semver.org).
### Changed ### Changed
- Gnumeric Reader now loads number formatting for cells. - Gnumeric Reader now loads number formatting for cells.
- Gnumeric Reader now correctly identifies selected worksheet. - Gnumeric Reader now correctly identifies selected worksheet and selected cells in a worksheet.
- Some Refactoring of the Ods Reader, moving all formula and address translation from Ods to Excel into a separate class to eliminate code duplication and ensure consistency. - Some Refactoring of the Ods Reader, moving all formula and address translation from Ods to Excel into a separate class to eliminate code duplication and ensure consistency.
- Make Boolean Conversion in Csv Reader locale-aware when using the String Value Binder. - Make Boolean Conversion in Csv Reader locale-aware when using the String Value Binder.
This is determined b the Calculation Engine locale setting. This is determined by the Calculation Engine locale setting.
(i.e. `"Vrai"` wil be converted to a boolean `true` if the Locale is set to `fr`.) (i.e. `"Vrai"` wil be converted to a boolean `true` if the Locale is set to `fr`.)

View File

@ -309,6 +309,7 @@ class Gnumeric extends BaseReader
$this->processMergedCells($sheet); $this->processMergedCells($sheet);
$this->processAutofilter($sheet); $this->processAutofilter($sheet);
$this->setSelectedCells($sheet);
++$worksheetID; ++$worksheetID;
} }
@ -329,6 +330,28 @@ class Gnumeric extends BaseReader
} }
} }
private function setSelectedCells(?SimpleXMLElement $sheet): void
{
if ($sheet !== null && isset($sheet->Selections)) {
foreach ($sheet->Selections as $selection) {
$startCol = (int) ($selection->StartCol ?? 0);
$startRow = (int) ($selection->StartRow ?? 0) + 1;
$endCol = (int) ($selection->EndCol ?? $startCol);
$endRow = (int) ($selection->endRow ?? 0) + 1;
$startColumn = Coordinate::stringFromColumnIndex($startCol + 1);
$endColumn = Coordinate::stringFromColumnIndex($endCol + 1);
$startCell = "{$startColumn}{$startRow}";
$endCell = "{$endColumn}{$endRow}";
$selectedRange = $startCell . (($endCell !== $startCell) ? ':' . $endCell : '');
$this->spreadsheet->getActiveSheet()->setSelectedCell($selectedRange);
break;
}
}
}
private function processMergedCells(?SimpleXMLElement $sheet): void private function processMergedCells(?SimpleXMLElement $sheet): void
{ {
// Handle Merged Cells in this worksheet // Handle Merged Cells in this worksheet
@ -357,7 +380,8 @@ class Gnumeric extends BaseReader
private function setColumnWidth(int $whichColumn, float $defaultWidth): void private function setColumnWidth(int $whichColumn, float $defaultWidth): void
{ {
$columnDimension = $this->spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($whichColumn + 1)); $columnDimension = $this->spreadsheet->getActiveSheet()
->getColumnDimension(Coordinate::stringFromColumnIndex($whichColumn + 1));
if ($columnDimension !== null) { if ($columnDimension !== null) {
$columnDimension->setWidth($defaultWidth); $columnDimension->setWidth($defaultWidth);
} }
@ -365,7 +389,8 @@ class Gnumeric extends BaseReader
private function setColumnInvisible(int $whichColumn): void private function setColumnInvisible(int $whichColumn): void
{ {
$columnDimension = $this->spreadsheet->getActiveSheet()->getColumnDimension(Coordinate::stringFromColumnIndex($whichColumn + 1)); $columnDimension = $this->spreadsheet->getActiveSheet()
->getColumnDimension(Coordinate::stringFromColumnIndex($whichColumn + 1));
if ($columnDimension !== null) { if ($columnDimension !== null) {
$columnDimension->setVisible(false); $columnDimension->setVisible(false);
} }

View File

@ -125,6 +125,8 @@ class GnumericLoadTest extends TestCase
self::assertTrue($sheet->getCell('B24')->getStyle()->getFont()->getSuperScript()); self::assertTrue($sheet->getCell('B24')->getStyle()->getFont()->getSuperScript());
$rowDimension = $sheet->getRowDimension(30); $rowDimension = $sheet->getRowDimension(30);
self::assertFalse($rowDimension->getVisible()); self::assertFalse($rowDimension->getVisible());
self::assertSame('B24', $sheet->getSelectedCells());
} }
public function testLoadFilter(): void public function testLoadFilter(): void
@ -167,5 +169,7 @@ class GnumericLoadTest extends TestCase
$sheet = $spreadsheet->getSheet(0); $sheet = $spreadsheet->getSheet(0);
self::assertEquals('Report Data', $sheet->getTitle()); self::assertEquals('Report Data', $sheet->getTitle());
self::assertEquals('Third Heading', $sheet->getCell('C2')->getValue()); self::assertEquals('Third Heading', $sheet->getCell('C2')->getValue());
self::assertSame('A1', $sheet->getSelectedCells());
} }
} }