forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SINGLE function can be used to return first value from a dynamic array result, or to return the value of the cell which matches the current row (VALUE error if not match) for a range. Excel allows you to specify an at-sign unary operator rather than SINGLE function; this PR does not permit that. Add support for reading CSE array functions for Gnumeric. Throw an exception if setValueExplicit Formula is invalid (not a string, or doesn't begin with equal sign. This is equivalent to what happens when setValueExplicit Numeric specifies a non-numeric value. Added a number of tests from PR PHPOffice#2787.
- Loading branch information
Showing
12 changed files
with
591 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
tests/PhpSpreadsheetTests/Calculation/InternalFunctionsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Calculation; | ||
|
||
use PhpOffice\PhpSpreadsheet\Calculation\Calculation; | ||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class InternalFunctionsTest extends TestCase | ||
{ | ||
private string $arrayReturnType; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->arrayReturnType = Calculation::getArrayReturnType(); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
Calculation::setArrayReturnType($this->arrayReturnType); | ||
} | ||
|
||
/** | ||
* @dataProvider anchorArrayDataProvider | ||
*/ | ||
public function testAnchorArrayFormula(string $reference, string $range, array $expectedResult): void | ||
{ | ||
Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_ARRAY); | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet1 = $spreadsheet->getActiveSheet(); | ||
$sheet1->setTitle('SheetOne'); // no space in sheet title | ||
$sheet2 = $spreadsheet->createSheet(); | ||
$sheet2->setTitle('Sheet Two'); // space in sheet title | ||
|
||
$sheet1->setCellValue('C3', '=SEQUENCE(3,3,-4)'); | ||
$sheet2->setCellValue('C3', '=SEQUENCE(3,3, 9, -1)'); | ||
$sheet1->calculateArrays(); | ||
$sheet2->calculateArrays(); | ||
$sheet1->setCellValue('A8', "=ANCHORARRAY({$reference})"); | ||
|
||
$result1 = $sheet1->getCell('A8')->getCalculatedValue(); | ||
self::assertSame($expectedResult, $result1); | ||
$attributes1 = $sheet1->getCell('A8')->getFormulaAttributes(); | ||
self::assertSame(['t' => 'array', 'ref' => $range], $attributes1); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public static function anchorArrayDataProvider(): array | ||
{ | ||
return [ | ||
[ | ||
'C3', | ||
'A8:C10', | ||
[[-4, -3, -2], [-1, 0, 1], [2, 3, 4]], | ||
], | ||
[ | ||
"'Sheet Two'!C3", | ||
'A8:C10', | ||
[[9, 8, 7], [6, 5, 4], [3, 2, 1]], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider singleDataProvider | ||
*/ | ||
public function testSingleArrayFormula(string $reference, mixed $expectedResult): void | ||
{ | ||
Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_ARRAY); | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet1 = $spreadsheet->getActiveSheet(); | ||
$sheet1->setTitle('SheetOne'); // no space in sheet title | ||
$sheet2 = $spreadsheet->createSheet(); | ||
$sheet2->setTitle('Sheet Two'); // space in sheet title | ||
|
||
$sheet1->setCellValue('C3', '=SEQUENCE(3,3,-4)'); | ||
$sheet2->setCellValue('C3', '=SEQUENCE(3,3, 9, -1)'); | ||
|
||
$sheet1->setCellValue('A8', "=SINGLE({$reference})"); | ||
$sheet1->setCellValue('G3', 'three'); | ||
$sheet1->setCellValue('G4', 'four'); | ||
$sheet1->setCellValue('G5', 'five'); | ||
$sheet1->setCellValue('G7', 'seven'); | ||
$sheet1->setCellValue('G8', 'eight'); | ||
$sheet1->setCellValue('G9', 'nine'); | ||
|
||
$sheet1->calculateArrays(); | ||
$sheet2->calculateArrays(); | ||
|
||
$result1 = $sheet1->getCell('A8')->getCalculatedValue(); | ||
self::assertSame($expectedResult, $result1); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public static function singleDataProvider(): array | ||
{ | ||
return [ | ||
'array cell on same sheet' => [ | ||
'C3', | ||
-4, | ||
], | ||
'array cell on different sheet' => [ | ||
"'Sheet Two'!C3", | ||
9, | ||
], | ||
'range which includes current row' => [ | ||
'G7:G9', | ||
'eight', | ||
], | ||
'range which does not include current row' => [ | ||
'G3:G5', | ||
'#VALUE!', | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.