Skip to content

Commit 09eb05f

Browse files
committed
OFFSET should allow omitted height and width
Commit 8dddf56 inadvertently removed the ability to omit the width and height arguments to the OFFSET function. And #REF! is returned because the function is validating that the new $pCell argument is present. It is present, but it has been passed in the $height position. We fixed this by always passing $pCell at the last position and filling missing arguments with NULL values. Fixes #561 Fixes #565
1 parent 7b362bd commit 09eb05f

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- Sheet title can contain exclamation mark - [#325](https://github.com/PHPOffice/PhpSpreadsheet/issues/325)
1717
- Xls file cause the exception during open by Xls reader - [#402](https://github.com/PHPOffice/PhpSpreadsheet/issues/402)
1818
- Skip non numeric value in SUMIF - [#618](https://github.com/PHPOffice/PhpSpreadsheet/pull/618)
19+
- OFFSET should allow omitted height and width - [#561](https://github.com/PHPOffice/PhpSpreadsheet/issues/561)
1920

2021
## [1.4.1] - 2018-09-30
2122

src/PhpSpreadsheet/Calculation/Calculation.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3942,9 +3942,7 @@ private function processTokenStack($tokens, $cellID = null, Cell $pCell = null)
39423942
}
39433943

39443944
// Process the argument with the appropriate function call
3945-
if ($passCellReference) {
3946-
$args[] = $pCell;
3947-
}
3945+
$args = $this->addCellReference($args, $passCellReference, $pCell, $functionCall);
39483946

39493947
if (!is_array($functionCall)) {
39503948
foreach ($args as &$arg) {
@@ -4436,4 +4434,32 @@ public function getImplementedFunctionNames()
44364434

44374435
return $returnValue;
44384436
}
4437+
4438+
/**
4439+
* Add cell reference if needed while making sure that it is the last argument.
4440+
*
4441+
* @param array $args
4442+
* @param bool $passCellReference
4443+
* @param Cell $pCell
4444+
* @param array $functionCall
4445+
*
4446+
* @return array
4447+
*/
4448+
private function addCellReference(array $args, $passCellReference, Cell $pCell, array $functionCall)
4449+
{
4450+
if ($passCellReference) {
4451+
$className = $functionCall[0];
4452+
$methodName = $functionCall[1];
4453+
4454+
$reflectionMethod = new \ReflectionMethod($className, $methodName);
4455+
$argumentCount = count($reflectionMethod->getParameters());
4456+
while (count($args) < $argumentCount - 1) {
4457+
$args[] = null;
4458+
}
4459+
4460+
$args[] = $pCell;
4461+
}
4462+
4463+
return $args;
4464+
}
44394465
}

tests/PhpSpreadsheetTests/Calculation/CalculationTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
66
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
7+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
78
use PHPUnit\Framework\TestCase;
89

910
class CalculationTest extends TestCase
@@ -117,4 +118,26 @@ public function testDoesHandleXlfnFunctions()
117118
$function = $tree[4];
118119
self::assertEquals('Function', $function['type']);
119120
}
121+
122+
public function testFormulaWithOptionalArgumentsAndRequiredCellReferenceShouldPassNullForMissingArguments()
123+
{
124+
$spreadsheet = new Spreadsheet();
125+
$sheet = $spreadsheet->getActiveSheet();
126+
127+
$sheet->fromArray(
128+
[
129+
[1, 2, 3],
130+
[4, 5, 6],
131+
[7, 8, 9],
132+
]
133+
);
134+
135+
$cell = $sheet->getCell('E5');
136+
$cell->setValue('=OFFSET(D3, -1, -2, 1, 1)');
137+
self::assertEquals(5, $cell->getCalculatedValue(), 'with all arguments');
138+
139+
$cell = $sheet->getCell('F6');
140+
$cell->setValue('=OFFSET(D3, -1, -2)');
141+
self::assertEquals(5, $cell->getCalculatedValue(), 'missing arguments should be filled with null');
142+
}
120143
}

0 commit comments

Comments
 (0)