|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOffice\PhpSpreadsheetTests\Calculation; |
| 6 | + |
| 7 | +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
| 8 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use ReflectionMethod; |
| 11 | + |
| 12 | +class Issue4451Test extends TestCase |
| 13 | +{ |
| 14 | + public static function testReflect(): void |
| 15 | + { |
| 16 | + // Sample matrices to test with |
| 17 | + $matrix1 = [[1], [3]]; |
| 18 | + $matrix2 = [[5], [8], [11]]; |
| 19 | + |
| 20 | + // Use reflection to make the protected method accessible |
| 21 | + $calculation = new Calculation(); |
| 22 | + $reflectionMethod = new ReflectionMethod(Calculation::class, 'resizeMatricesExtend'); |
| 23 | + $reflectionMethod->setAccessible(true); |
| 24 | + |
| 25 | + // Call the method using reflection |
| 26 | + $reflectionMethod->invokeArgs($calculation, [&$matrix1, &$matrix2, count($matrix1), 1, count($matrix2), 1]); |
| 27 | + |
| 28 | + self::assertSame([[1], [3], [null]], $matrix1); //* @phpstan-ignore-line |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * These 2 tests are contrived. They prove that method |
| 33 | + * works as desired, but Excel will actually return |
| 34 | + * a CALC error, a result I don't know how to duplicate. |
| 35 | + */ |
| 36 | + public static function testExtendFirstColumn(): void |
| 37 | + { |
| 38 | + $spreadsheet = new Spreadsheet(); |
| 39 | + $sheet = $spreadsheet->getActiveSheet(); |
| 40 | + $sheet->setTitle('Products'); |
| 41 | + $calculationEngine = Calculation::getInstance($spreadsheet); |
| 42 | + $calculationEngine->setInstanceArrayReturnType( |
| 43 | + Calculation::RETURN_ARRAY_AS_ARRAY |
| 44 | + ); |
| 45 | + |
| 46 | + $sheet->getCell('D5')->setValue(5); |
| 47 | + $sheet->getCell('E5')->setValue(20); |
| 48 | + $sheet->fromArray( |
| 49 | + [ |
| 50 | + [5, 20, 'Apples'], |
| 51 | + [10, 20, 'Bananas'], |
| 52 | + [5, 20, 'Cherries'], |
| 53 | + [5, 40, 'Grapes'], |
| 54 | + [25, 50, 'Peaches'], |
| 55 | + [30, 60, 'Pears'], |
| 56 | + [35, 70, 'Papayas'], |
| 57 | + [40, 80, 'Mangos'], |
| 58 | + [null, 20, 'Unknown'], |
| 59 | + ], |
| 60 | + null, |
| 61 | + 'K1', |
| 62 | + true |
| 63 | + ); |
| 64 | + $kRows = $sheet->getHighestDataRow('K'); |
| 65 | + self::assertSame(8, $kRows); |
| 66 | + $lRows = $sheet->getHighestDataRow('L'); |
| 67 | + self::assertSame(9, $lRows); |
| 68 | + $mRows = $sheet->getHighestDataRow('M'); |
| 69 | + self::assertSame(9, $mRows); |
| 70 | + $sheet->getCell('A1') |
| 71 | + ->setValue( |
| 72 | + "=FILTER(Products!M1:M$mRows," |
| 73 | + . "(Products!K1:K$kRows=D5)" |
| 74 | + . "*(Products!L1:L$lRows=E5))" |
| 75 | + ); |
| 76 | + |
| 77 | + $result = $sheet->getCell('A1')->getCalculatedValue(); |
| 78 | + self::assertSame([['Apples'], ['Cherries']], $result); |
| 79 | + $spreadsheet->disconnectWorksheets(); |
| 80 | + } |
| 81 | + |
| 82 | + public static function testExtendSecondColumn(): void |
| 83 | + { |
| 84 | + $spreadsheet = new Spreadsheet(); |
| 85 | + $sheet = $spreadsheet->getActiveSheet(); |
| 86 | + $sheet->setTitle('Products'); |
| 87 | + $calculationEngine = Calculation::getInstance($spreadsheet); |
| 88 | + $calculationEngine->setInstanceArrayReturnType( |
| 89 | + Calculation::RETURN_ARRAY_AS_ARRAY |
| 90 | + ); |
| 91 | + |
| 92 | + $sheet->getCell('D5')->setValue(5); |
| 93 | + $sheet->getCell('E5')->setValue(20); |
| 94 | + $sheet->fromArray( |
| 95 | + [ |
| 96 | + [5, 20, 'Apples'], |
| 97 | + [10, 20, 'Bananas'], |
| 98 | + [5, 20, 'Cherries'], |
| 99 | + [5, 40, 'Grapes'], |
| 100 | + [25, 50, 'Peaches'], |
| 101 | + [30, 60, 'Pears'], |
| 102 | + [35, 70, 'Papayas'], |
| 103 | + [40, 80, 'Mangos'], |
| 104 | + [null, 20, 'Unknown'], |
| 105 | + ], |
| 106 | + null, |
| 107 | + 'K1', |
| 108 | + true |
| 109 | + ); |
| 110 | + $kRows = $sheet->getHighestDataRow('K'); |
| 111 | + self::assertSame(8, $kRows); |
| 112 | + //$lRows = $sheet->getHighestDataRow('L'); |
| 113 | + //self::assertSame(9, $lRows); |
| 114 | + $lRows = 2; |
| 115 | + $mRows = $sheet->getHighestDataRow('M'); |
| 116 | + self::assertSame(9, $mRows); |
| 117 | + $sheet->getCell('A1') |
| 118 | + ->setValue( |
| 119 | + "=FILTER(Products!M1:M$mRows," |
| 120 | + . "(Products!K1:K$kRows=D5)" |
| 121 | + . "*(Products!L1:L$lRows=E5))" |
| 122 | + ); |
| 123 | + |
| 124 | + $result = $sheet->getCell('A1')->getCalculatedValue(); |
| 125 | + self::assertSame([['Apples']], $result); |
| 126 | + $spreadsheet->disconnectWorksheets(); |
| 127 | + } |
| 128 | +} |
0 commit comments