|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions\LookupRef; |
| 6 | + |
| 7 | +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; |
| 8 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 9 | + |
| 10 | +/** |
| 11 | + * Many of these tests are derived from |
| 12 | + * https://exceljet.net/functions/hstack-function. |
| 13 | + */ |
| 14 | +class HStackTest extends AllSetupTeardown |
| 15 | +{ |
| 16 | + public static function testHstack1(): void |
| 17 | + { |
| 18 | + $spreadsheet = new Spreadsheet(); |
| 19 | + Calculation::getInstance($spreadsheet) |
| 20 | + ->setInstanceArrayReturnType( |
| 21 | + Calculation::RETURN_ARRAY_AS_ARRAY |
| 22 | + ); |
| 23 | + $sheet = $spreadsheet->getActiveSheet(); |
| 24 | + $b4tof14 = [ |
| 25 | + ['A', null, 'B', null, 'C'], |
| 26 | + [12, null, 7, null, 12], |
| 27 | + [9, null, 13, null, 10], |
| 28 | + [10, null, 5, null, 11], |
| 29 | + [11, null, 13, null, 6], |
| 30 | + [8, null, 12, null, 7], |
| 31 | + [12, null, 11, null, 15], |
| 32 | + [9, null, 10, null, 6], |
| 33 | + [10, null, 15, null, 5], |
| 34 | + [11, null, 9, null, 14], |
| 35 | + [6, null, 13, null, 11], |
| 36 | + ]; |
| 37 | + $sheet->fromArray($b4tof14, null, 'B4', true); |
| 38 | + $sheet->setCellValue('H4', '=HSTACK(B4:B14,D4:D14,F4:F14)'); |
| 39 | + $expected = [ |
| 40 | + ['A', 'B', 'C'], |
| 41 | + [12, 7, 12], |
| 42 | + [9, 13, 10], |
| 43 | + [10, 5, 11], |
| 44 | + [11, 13, 6], |
| 45 | + [8, 12, 7], |
| 46 | + [12, 11, 15], |
| 47 | + [9, 10, 6], |
| 48 | + [10, 15, 5], |
| 49 | + [11, 9, 14], |
| 50 | + [6, 13, 11], |
| 51 | + ]; |
| 52 | + self::assertSame($expected, $sheet->getCell('H4')->getCalculatedValue()); |
| 53 | + |
| 54 | + $sheet->setCellValue('K4', '=HSTACK(B4:B14,D4:D12,F4:F14)'); |
| 55 | + $expected = [ |
| 56 | + ['A', 'B', 'C'], |
| 57 | + [12, 7, 12], |
| 58 | + [9, 13, 10], |
| 59 | + [10, 5, 11], |
| 60 | + [11, 13, 6], |
| 61 | + [8, 12, 7], |
| 62 | + [12, 11, 15], |
| 63 | + [9, 10, 6], |
| 64 | + [10, 15, 5], |
| 65 | + [11, '#N/A', 14], |
| 66 | + [6, '#N/A', 11], |
| 67 | + ]; |
| 68 | + self::assertSame($expected, $sheet->getCell('K4')->getCalculatedValue(), 'one short column'); |
| 69 | + |
| 70 | + $sheet->setCellValue('R4', '=IFERROR(HSTACK(B4:B14,D4:D12,F4:F14),"")'); |
| 71 | + $expected = [ |
| 72 | + ['A', 'B', 'C'], |
| 73 | + [12, 7, 12], |
| 74 | + [9, 13, 10], |
| 75 | + [10, 5, 11], |
| 76 | + [11, 13, 6], |
| 77 | + [8, 12, 7], |
| 78 | + [12, 11, 15], |
| 79 | + [9, 10, 6], |
| 80 | + [10, 15, 5], |
| 81 | + [11, '', 14], |
| 82 | + [6, '', 11], |
| 83 | + ]; |
| 84 | + self::assertSame($expected, $sheet->getCell('R4')->getCalculatedValue(), 'one short column with null string instead of N/A'); |
| 85 | + |
| 86 | + $sheet->setCellValue('N4', '=HSTACK(B5,H6:J6)'); |
| 87 | + $expected = [ |
| 88 | + [12, 9, 13, 10], |
| 89 | + ]; |
| 90 | + self::assertSame($expected, $sheet->getCell('N4')->getCalculatedValue(), 'one single-cell arg'); |
| 91 | + |
| 92 | + $spreadsheet->disconnectWorksheets(); |
| 93 | + } |
| 94 | +} |
0 commit comments