|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx; |
| 6 | + |
| 7 | +use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
| 8 | +use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 9 | +use PhpOffice\PhpSpreadsheet\Worksheet\Table; |
| 10 | +use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; |
| 11 | + |
| 12 | +/** |
| 13 | + * Excel reports a workbook whose table covers its header row alone as unreadable, and repairs it |
| 14 | + * by dropping the table. Excel itself never writes one: asked to make a table over a single row of |
| 15 | + * headings it writes the table over the row below as well, leaving that row without a cell. The |
| 16 | + * writer does the same — unless that row already holds something. |
| 17 | + */ |
| 18 | +class TableHeaderRowTest extends AbstractFunctional |
| 19 | +{ |
| 20 | + private ?Spreadsheet $spreadsheet = null; |
| 21 | + |
| 22 | + protected function tearDown(): void |
| 23 | + { |
| 24 | + if ($this->spreadsheet !== null) { |
| 25 | + $this->spreadsheet->disconnectWorksheets(); |
| 26 | + $this->spreadsheet = null; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public function testHeaderRowTakesTheEmptyRowBelowIt(): void |
| 31 | + { |
| 32 | + $this->spreadsheet = new Spreadsheet(); |
| 33 | + $sheet = $this->spreadsheet->getActiveSheet(); |
| 34 | + $sheet->fromArray([['Year', 'Country']], null, 'A1'); |
| 35 | + $sheet->addTable(new Table('A1:B1', 'SalesData')); |
| 36 | + |
| 37 | + $reloaded = $this->writeAndReload($this->spreadsheet, 'Xlsx'); |
| 38 | + $worksheet = $reloaded->getActiveSheet(); |
| 39 | + $table = $worksheet->getTableByName('SalesData'); |
| 40 | + |
| 41 | + // exactly what Excel writes for a table made over a single row of headings |
| 42 | + self::assertInstanceOf(Table::class, $table); |
| 43 | + self::assertSame('A1:B2', $table->getRange()); |
| 44 | + self::assertSame(1, $worksheet->getHighestDataRow(), 'The row taken is left without a cell'); |
| 45 | + |
| 46 | + $reloaded->disconnectWorksheets(); |
| 47 | + } |
| 48 | + |
| 49 | + public function testHeaderRowWillNotSwallowARowThatHoldsSomething(): void |
| 50 | + { |
| 51 | + $this->spreadsheet = new Spreadsheet(); |
| 52 | + $sheet = $this->spreadsheet->getActiveSheet(); |
| 53 | + $sheet->fromArray([['Year', 'Country']], null, 'A1'); |
| 54 | + $sheet->getCell('A2')->setValue('Total'); |
| 55 | + $sheet->addTable(new Table('A1:B1', 'SalesData')); |
| 56 | + |
| 57 | + $this->expectException(PhpSpreadsheetException::class); |
| 58 | + $this->expectExceptionMessage('needs at least 2 rows'); |
| 59 | + |
| 60 | + $this->writeAndReload($this->spreadsheet, 'Xlsx')->disconnectWorksheets(); |
| 61 | + } |
| 62 | + |
| 63 | + public function testHeaderRowWithOneRowOfDataIsWritten(): void |
| 64 | + { |
| 65 | + $this->spreadsheet = new Spreadsheet(); |
| 66 | + $sheet = $this->spreadsheet->getActiveSheet(); |
| 67 | + $sheet->fromArray([['Year', 'Country'], [2010, 'Belgium']], null, 'A1'); |
| 68 | + $sheet->addTable(new Table('A1:B2', 'SalesData')); |
| 69 | + |
| 70 | + $reloaded = $this->writeAndReload($this->spreadsheet, 'Xlsx'); |
| 71 | + $table = $reloaded->getActiveSheet()->getTableByName('SalesData'); |
| 72 | + |
| 73 | + self::assertInstanceOf(Table::class, $table); |
| 74 | + self::assertSame('A1:B2', $table->getRange()); |
| 75 | + self::assertTrue($table->getShowHeaderRow()); |
| 76 | + |
| 77 | + $reloaded->disconnectWorksheets(); |
| 78 | + } |
| 79 | + |
| 80 | + public function testASingleRowTableWithoutAHeaderRowIsWritten(): void |
| 81 | + { |
| 82 | + $this->spreadsheet = new Spreadsheet(); |
| 83 | + $sheet = $this->spreadsheet->getActiveSheet(); |
| 84 | + $sheet->fromArray([[2010, 'Belgium']], null, 'A1'); |
| 85 | + $sheet->addTable((new Table('A1:B1', 'SalesData'))->setShowHeaderRow(false)); |
| 86 | + |
| 87 | + $reloaded = $this->writeAndReload($this->spreadsheet, 'Xlsx'); |
| 88 | + $table = $reloaded->getActiveSheet()->getTableByName('SalesData'); |
| 89 | + |
| 90 | + self::assertInstanceOf(Table::class, $table); |
| 91 | + self::assertSame('A1:B1', $table->getRange()); |
| 92 | + self::assertFalse($table->getShowHeaderRow()); |
| 93 | + |
| 94 | + $reloaded->disconnectWorksheets(); |
| 95 | + } |
| 96 | +} |
0 commit comments