1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation; |
6
|
|
|
|
7
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class DefinedNamesCalculationTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('namedRangeCalculationTest1')] |
13
|
|
|
public function testNamedRangeCalculations1(string $cellAddress, float $expectedValue): void |
14
|
|
|
{ |
15
|
|
|
$inputFileType = 'Xlsx'; |
16
|
|
|
$inputFileName = __DIR__ . '/../../data/Calculation/DefinedNames/NamedRanges.xlsx'; |
17
|
|
|
|
18
|
|
|
$reader = IOFactory::createReader($inputFileType); |
19
|
|
|
$spreadsheet = $reader->load($inputFileName); |
20
|
|
|
|
21
|
|
|
$calculatedCellValue = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue(); |
22
|
|
|
self::assertSame($expectedValue, $calculatedCellValue, "Failed calculation for cell {$cellAddress}"); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('namedRangeCalculationTest2')] |
26
|
|
|
public function testNamedRangeCalculationsWithAdjustedRateValue(string $cellAddress, float $expectedValue): void |
27
|
|
|
{ |
28
|
|
|
$inputFileType = 'Xlsx'; |
29
|
|
|
$inputFileName = __DIR__ . '/../../data/Calculation/DefinedNames/NamedRanges.xlsx'; |
30
|
|
|
|
31
|
|
|
$reader = IOFactory::createReader($inputFileType); |
32
|
|
|
$spreadsheet = $reader->load($inputFileName); |
33
|
|
|
|
34
|
|
|
$spreadsheet->getActiveSheet()->getCell('B1')->setValue(12.5); |
35
|
|
|
|
36
|
|
|
$calculatedCellValue = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue(); |
37
|
|
|
self::assertSame($expectedValue, $calculatedCellValue, "Failed calculation for cell {$cellAddress}"); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('namedRangeCalculationTest1')] |
41
|
|
|
public function testNamedFormulaCalculations1(string $cellAddress, float $expectedValue): void |
42
|
|
|
{ |
43
|
|
|
$inputFileType = 'Xlsx'; |
44
|
|
|
$inputFileName = __DIR__ . '/../../data/Calculation/DefinedNames/NamedFormulae.xlsx'; |
45
|
|
|
|
46
|
|
|
$reader = IOFactory::createReader($inputFileType); |
47
|
|
|
$spreadsheet = $reader->load($inputFileName); |
48
|
|
|
|
49
|
|
|
$calculatedCellValue = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue(); |
50
|
|
|
self::assertSame($expectedValue, $calculatedCellValue, "Failed calculation for cell {$cellAddress}"); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('namedRangeCalculationTest2')] |
54
|
|
|
public function testNamedFormulaeCalculationsWithAdjustedRateValue(string $cellAddress, float $expectedValue): void |
55
|
|
|
{ |
56
|
|
|
$inputFileType = 'Xlsx'; |
57
|
|
|
$inputFileName = __DIR__ . '/../../data/Calculation/DefinedNames/NamedFormulae.xlsx'; |
58
|
|
|
|
59
|
|
|
$reader = IOFactory::createReader($inputFileType); |
60
|
|
|
$spreadsheet = $reader->load($inputFileName); |
61
|
|
|
|
62
|
|
|
$spreadsheet->getActiveSheet()->getCell('B1')->setValue(12.5); |
63
|
|
|
|
64
|
|
|
$calculatedCellValue = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue(); |
65
|
|
|
self::assertSame($expectedValue, $calculatedCellValue, "Failed calculation for cell {$cellAddress}"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function namedRangeCalculationTest1(): array |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
|
|
['C4', 56.25], |
72
|
|
|
['C5', 54.375], |
73
|
|
|
['C6', 48.75], |
74
|
|
|
['C7', 52.5], |
75
|
|
|
['C8', 41.25], |
76
|
|
|
['B10', 33.75], |
77
|
|
|
['C10', 253.125], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public static function namedRangeCalculationTest2(): array |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
|
|
['C4', 93.75], |
85
|
|
|
['C5', 90.625], |
86
|
|
|
['C6', 81.25], |
87
|
|
|
['C7', 87.5], |
88
|
|
|
['C8', 68.75], |
89
|
|
|
['C10', 421.875], |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|