Skip to content

Commit 771b3f1

Browse files
committed
Update OFFSET
Fix #4376.
1 parent 8e50403 commit 771b3f1

File tree

2 files changed

+18
-4
lines changed
  • src/PhpSpreadsheet/Calculation/LookupRef
  • tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef

2 files changed

+18
-4
lines changed

src/PhpSpreadsheet/Calculation/LookupRef/Offset.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
88
use PhpOffice\PhpSpreadsheet\Cell\Cell;
99
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
10+
use PhpOffice\PhpSpreadsheet\Worksheet\Validations;
1011
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
1112

1213
class Offset
@@ -55,19 +56,22 @@ public static function OFFSET(?string $cellAddress = null, mixed $rows = 0, mixe
5556
if (!is_object($cell)) {
5657
return ExcelError::REF();
5758
}
59+
$sheet = $cell->getParent()?->getParent(); // worksheet
60+
if ($sheet !== null) {
61+
$cellAddress = Validations::definedNameToCoordinate($cellAddress, $sheet);
62+
}
5863

5964
[$cellAddress, $worksheet] = self::extractWorksheet($cellAddress, $cell);
6065

6166
$startCell = $endCell = $cellAddress;
6267
if (strpos($cellAddress, ':')) {
6368
[$startCell, $endCell] = explode(':', $cellAddress);
6469
}
65-
[$startCellColumn, $startCellRow] = Coordinate::coordinateFromString($startCell);
66-
[$endCellColumn, $endCellRow] = Coordinate::coordinateFromString($endCell);
70+
[$startCellColumn, $startCellRow] = Coordinate::indexesFromString($startCell);
71+
[, $endCellRow, $endCellColumn] = Coordinate::indexesFromString($endCell);
6772

6873
$startCellRow += $rows;
69-
$startCellColumn = Coordinate::columnIndexFromString($startCellColumn) - 1;
70-
$startCellColumn += $columns;
74+
$startCellColumn += $columns - 1;
7175

7276
if (($startCellRow <= 0) || ($startCellColumn < 0)) {
7377
return ExcelError::REF();

tests/PhpSpreadsheetTests/Calculation/Functions/LookupRef/OffsetTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,14 @@ public function testOffsetNamedRangeApostropheSheet(): void
7474

7575
self::assertSame(2, $workSheet->getCell('B2')->getCalculatedValue());
7676
}
77+
78+
public function testOffsetMultiCellNamedRange(): void
79+
{
80+
$sheet = $this->getSheet();
81+
$sheet->setCellValue('D13', 'Hello');
82+
$this->getSpreadsheet()
83+
->addNamedRange(new NamedRange('CELLAREA', $sheet, '$B$6:$F$22'));
84+
$sheet->setCellValue('D1', '=OFFSET(CELLAREA,7,2,1,1)');
85+
self::assertSame('Hello', $sheet->getCell('D1')->getCalculatedValue());
86+
}
7787
}

0 commit comments

Comments
 (0)