1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Table; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class StructuredReferenceFormulaTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @dataProvider structuredReferenceProvider |
14
|
|
|
*/ |
15
|
|
|
public function testStructuredReferences(float $expectedValue, string $cellAddress): void |
16
|
|
|
{ |
17
|
|
|
$inputFileType = 'Xlsx'; |
18
|
|
|
$inputFileName = __DIR__ . '/../../data/Calculation/TableFormulae.xlsx'; |
19
|
|
|
|
20
|
|
|
$reader = IOFactory::createReader($inputFileType); |
21
|
|
|
$spreadsheet = $reader->load($inputFileName); |
22
|
|
|
|
23
|
|
|
$calculatedCellValue = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue(); |
24
|
|
|
self::assertEqualsWithDelta($expectedValue, $calculatedCellValue, 1.0e-14, "Failed calculation for cell {$cellAddress}"); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testStructuredReferenceHiddenHeaders(): void |
28
|
|
|
{ |
29
|
|
|
$inputFileType = 'Xlsx'; |
30
|
|
|
$inputFileName = __DIR__ . '/../../data/Calculation/TableFormulae.xlsx'; |
31
|
|
|
|
32
|
|
|
$reader = IOFactory::createReader($inputFileType); |
33
|
|
|
$spreadsheet = $reader->load($inputFileName); |
34
|
|
|
/** @var Table $table */ |
35
|
|
|
$table = $spreadsheet->getActiveSheet()->getTableByName('DeptSales'); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$cellAddress = 'G8'; |
38
|
|
|
$spreadsheet->getActiveSheet()->getCell($cellAddress)->setValue('=DeptSales[[#Headers][Region]]'); |
39
|
|
|
$result = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue(); |
40
|
|
|
self::assertSame('Region', $result); |
41
|
|
|
|
42
|
|
|
$spreadsheet->getCalculationEngine()->flushInstance(); // @phpstan-ignore-line |
43
|
|
|
$table->setShowHeaderRow(false); |
44
|
|
|
|
45
|
|
|
$result = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue(); |
46
|
|
|
self::assertSame(ExcelError::REF(), $result); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function structuredReferenceProvider(): array |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
|
|
[26.0, 'E2'], |
53
|
|
|
[99.0, 'E3'], |
54
|
|
|
[141.0, 'E4'], |
55
|
|
|
[49.2, 'E5'], |
56
|
|
|
[120.0, 'E6'], |
57
|
|
|
[135.0, 'E7'], |
58
|
|
|
[570.2, 'E8'], |
59
|
|
|
[3970.0, 'C8'], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.