Skip to content

Commit

Permalink
Bugfix #1858; Additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkBaker committed Feb 19, 2021
1 parent c14110a commit 5878ecf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/PhpSpreadsheet/Worksheet/Worksheet.php
Expand Up @@ -1197,7 +1197,8 @@ public function getCell($pCoordinate, $createIfNotExists = true)
) {
$namedRange = $this->validateNamedRange($pCoordinate, true);
if ($namedRange !== null) {
$cellCoordinate = str_replace('$', '', $namedRange->getValue());
$cellCoordinate = ltrim(substr($namedRange->getValue(), strrpos($namedRange->getValue(), '!')), '!');
$cellCoordinate = str_replace('$', '', $cellCoordinate);

return $namedRange->getWorksheet()->getCell($cellCoordinate, $createIfNotExists);
}
Expand Down Expand Up @@ -1296,8 +1297,12 @@ public function cellExists($pCoordinate)
(preg_match('/^' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '$/i', $pCoordinate, $matches))
) {
$namedRange = $this->validateNamedRange($pCoordinate, true);
if ($namedRange !== null) {
$cellCoordinate = ltrim(substr($namedRange->getValue(), strrpos($namedRange->getValue(), '!')), '!');
$cellCoordinate = str_replace('$', '', $cellCoordinate);

return ($namedRange !== null) ? $namedRange->getWorksheet()->cellExists($pCoordinate) : false;
return $namedRange->getWorksheet()->cellExists($cellCoordinate);
}
}

// Uppercase coordinate
Expand Down Expand Up @@ -2589,7 +2594,8 @@ public function namedRangeToArray(string $definedName, $nullValue = null, $calcu
{
$namedRange = $this->validateNamedRange($definedName);
$workSheet = $namedRange->getWorksheet();
$cellRange = str_replace('$', '', $namedRange->getValue());
$cellRange = ltrim(substr($namedRange->getValue(), strrpos($namedRange->getValue(), '!')), '!');
$cellRange = str_replace('$', '', $cellRange);

return $workSheet->rangeToArray($cellRange, $nullValue, $calculateFormulas, $formatData, $returnCellRef);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/PhpSpreadsheetTests/Worksheet/WorksheetNamedRangesTest.php
@@ -0,0 +1,38 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Worksheet;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PHPUnit\Framework\TestCase;

class WorksheetNamedRangesTest extends TestCase
{
protected $spreadsheet;

public function setUp(): void
{
$reader = new Xlsx();
$this->spreadsheet = $reader->load('tests/data/Worksheet/namedRangeTest.xlsx');
}

public function testCellExists()
{
$worksheet = $this->spreadsheet->getActiveSheet();
$cellExists = $worksheet->cellExists('GREETING');
self::assertTrue($cellExists);
}

public function testGetCell()
{
$worksheet = $this->spreadsheet->getActiveSheet();
$cell = $worksheet->getCell('GREETING');
self::assertSame('Hello', $cell->getValue());
}

public function testNamedRangeToArray()
{
$worksheet = $this->spreadsheet->getActiveSheet();
$rangeData = $worksheet->namedRangeToArray('Range1');
self::assertSame([[1, 2, 3]], $rangeData);
}
}

0 comments on commit 5878ecf

Please sign in to comment.