PhpSpreadsheet/tests/PhpSpreadsheetTests/Writer/Html/ExtendForChartsAndImagesTest.php
Adrien Crivelli f1fb8dcf1f
Don't ouput row and columns without any cells in HTML writer
If row or column dimensions are accessed, then HTML writer would
still generate lots of empty cells, to show nothing at all. This
now ignore row and column dimensions to only output cell that
actually exists (even if those cells are empty).

Fixes #1235
Close #1537
2020-06-28 22:03:37 +09:00

95 lines
2.8 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use PhpOffice\PhpSpreadsheetTests\Functional;
class ExtendForChartsAndImagesTest extends Functional\AbstractFunctional
{
public function testEmptySheet(): void
{
$spreadsheet = new Spreadsheet();
$this->assertMaxColumnAndMaxRow($spreadsheet, 1, 1);
}
public function testSimpleSheet(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('B3', 'foo');
$this->assertMaxColumnAndMaxRow($spreadsheet, 2, 3);
}
public function testSheetWithExtraColumnDimensions(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('B3', 'foo');
// Artificially expend the sheet column count without any real cells
$sheet->getColumnDimension('E');
$this->assertMaxColumnAndMaxRow($spreadsheet, 2, 3);
}
public function testSheetWithExtraRowDimensions(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('B3', 'foo');
// Artificially expend the sheet row count without any real cells
$sheet->getRowDimension(5);
$this->assertMaxColumnAndMaxRow($spreadsheet, 2, 3);
}
public function testSheetWithImageBelowData(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('B3', 'foo');
// Add a drawing to the worksheet
$drawing = new Drawing();
$drawing->setPath('foo.png', false);
$drawing->setCoordinates('A5');
$drawing->setWorksheet($sheet);
$this->assertMaxColumnAndMaxRow($spreadsheet, 2, 5);
}
public function testSheetWithImageRightOfData(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('B3', 'foo');
// Add a drawing to the worksheet
$drawing = new Drawing();
$drawing->setPath('foo.png', false);
$drawing->setCoordinates('E1');
$drawing->setWorksheet($sheet);
$this->assertMaxColumnAndMaxRow($spreadsheet, 5, 3);
}
private function assertMaxColumnAndMaxRow(Spreadsheet $spreadsheet, int $expectedColumnCount, int $expectedRowCount): void
{
$writer = new Html($spreadsheet);
$html = $writer->generateHtmlAll();
$rowCount = substr_count($html, '<tr ');
self::assertSame($expectedRowCount, $rowCount);
$columnCount = substr_count($html, '<td ') / $rowCount;
self::assertSame($expectedColumnCount, $columnCount);
}
}