Skip to content

Commit 10123c4

Browse files
committed
Html Writer Minor Fixes
While researching issue PHPOffice#1551, I came across some minor problems. When a spreadsheet does not have a title, which is often the case for spreadsheets created with Excel (note that this is not the case for spreadsheets created with PhpSpreadsheet), if you try to save it as Html, it throws an exception. It will now use the sheet title of the active sheet as a title in this case. When writing an Html spreadsheet using `useInlineCss(true)`, gridlines are not handled properly. This is addressed by adding `class=gridlines gridlinesp` to the cell's `td` tag, and by suppressing any border attributes which would be styled as `none #000000`. It would be unusual to turn off gridlines for specific cells, but that can still be accomplished by using `Border::BORDER_NONE` in conjunction with any color other than `#000000` - see new test `testHideSomeGridlines`.
1 parent 1b68270 commit 10123c4

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

src/PhpSpreadsheet/Writer/Html.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,11 @@ public function generateHTMLHeader(bool $includeStyles = false): string
355355
$html .= ' <head>' . PHP_EOL;
356356
$html .= ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' . PHP_EOL;
357357
$html .= ' <meta name="generator" content="PhpSpreadsheet, https://github.com/PHPOffice/PhpSpreadsheet" />' . PHP_EOL;
358-
$html .= ' <title>' . htmlspecialchars($properties->getTitle(), Settings::htmlEntityFlags()) . '</title>' . PHP_EOL;
358+
$title = $properties->getTitle();
359+
if ($title === '') {
360+
$title = $this->spreadsheet->getActiveSheet()->getTitle();
361+
}
362+
$html .= ' <title>' . htmlspecialchars($title, Settings::htmlEntityFlags()) . '</title>' . PHP_EOL;
359363
$html .= self::generateMeta($properties->getCreator(), 'author');
360364
$html .= self::generateMeta($properties->getTitle(), 'title');
361365
$html .= self::generateMeta($properties->getDescription(), 'description');
@@ -1462,11 +1466,21 @@ private function generateRowWriteCell(
14621466
$xcssClass['height'] = $height;
14631467
}
14641468
//** end of redundant code **
1469+
if ($this->useInlineCss) {
1470+
foreach (['border-top', 'border-bottom', 'border-right', 'border-left'] as $borderType) {
1471+
if (($xcssClass[$borderType] ?? '') === 'none #000000') {
1472+
unset($xcssClass[$borderType]);
1473+
}
1474+
}
1475+
}
14651476

14661477
if ($htmlx) {
14671478
$xcssClass['position'] = 'relative';
14681479
}
14691480
$html .= ' style="' . $this->assembleCSS($xcssClass) . '"';
1481+
if ($this->useInlineCss) {
1482+
$html .= ' class="gridlines gridlinesp"';
1483+
}
14701484
}
14711485
$html = $this->generateRowSpans($html, $rowSpan, $colSpan);
14721486

tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public function testInlineAndNot(): void
2424
];
2525
$sheet->getStyle('A1')->applyFromArray($styleArray);
2626
$style1 = "vertical-align:bottom; border-bottom:none #000000; border-top:none #000000; border-left:none #000000; border-right:none #000000; color:#000000; font-family:'Calibri'; font-size:11pt; background-color:#FFFF00";
27-
$style2 = $style1 . '; text-align:right; width:42pt';
27+
$style2 = "vertical-align:bottom; color:#000000; font-family:'Calibri'; font-size:11pt; background-color:#FFFF00";
28+
$style2 .= '; text-align:right; width:42pt';
2829
$writer = new Html($spreadsheet);
2930
$html = $writer->generateHtmlAll();
3031
self::assertStringContainsString('td.style1, th.style1 { ' . $style1 . ' }', $html);
@@ -33,7 +34,7 @@ public function testInlineAndNot(): void
3334
self::assertStringContainsString('.n { text-align:right }', $html);
3435
$writer->setUseInlineCss(true);
3536
$html = $writer->generateHtmlAll();
36-
self::assertStringContainsString('<td style="' . $style2 . '">1</td>', $html);
37+
self::assertStringContainsString('<td style="' . $style2 . '" class="gridlines gridlinesp">1</td>', $html);
3738
$spreadsheet->disconnectWorksheets();
3839
}
3940
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
8+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9+
use PhpOffice\PhpSpreadsheet\Style\Border;
10+
use PhpOffice\PhpSpreadsheet\Writer\Html;
11+
use PHPUnit\Framework\TestCase;
12+
13+
class NoTitleTest extends TestCase
14+
{
15+
public function testNoTitle(): void
16+
{
17+
$file = 'tests/data/Reader/Xlsx/blankcell.xlsx';
18+
$reader = new XlsxReader();
19+
$spreadsheet = $reader->load($file);
20+
self::assertSame('', $spreadsheet->getProperties()->getTitle());
21+
22+
$writer = new Html($spreadsheet);
23+
$writer->setUseInlineCss(true);
24+
$html = $writer->generateHTMLAll();
25+
self::assertStringContainsString('<title>Sheet1</title>', $html);
26+
self::assertStringContainsString('<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:left; width:42pt" class="gridlines gridlinesp">C1</td>', $html);
27+
$writer->setUseInlineCss(false);
28+
$html = $writer->generateHTMLAll();
29+
self::assertStringContainsString('<td class="column2 style0 s">C1</td>', $html);
30+
$spreadsheet->disconnectWorksheets();
31+
}
32+
33+
public function testHideSomeGridlines(): void
34+
{
35+
$spreadsheet = new Spreadsheet();
36+
$sheet = $spreadsheet->getActiveSheet();
37+
$sheet->fromArray(
38+
[
39+
[1, 2, 3, 4, 5, 6],
40+
[7, 8, 9, 10, 11, 12],
41+
[17, 18, 19, 20, 21, 22],
42+
[27, 28, 29, 30, 31, 32],
43+
[37, 38, 39, 40, 41, 42],
44+
]);
45+
$sheet->getStyle('B2:D4')->getBorders()->applyFromArray(
46+
[
47+
'allBorders' => [
48+
'borderStyle' => Border::BORDER_NONE,
49+
'color' => ['rgb' => '808080'],
50+
],
51+
],
52+
);
53+
54+
$writer = new Html($spreadsheet);
55+
$writer->setUseInlineCss(true);
56+
$html = $writer->generateHTMLAll();
57+
self::assertStringContainsString('<td style="vertical-align:bottom; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt" class="gridlines gridlinesp">7</td>', $html);
58+
self::assertStringContainsString('<td style="vertical-align:bottom; border-bottom:none #808080; border-top:none #808080; border-left:none #808080; border-right:none #808080; color:#000000; font-family:\'Calibri\'; font-size:11pt; text-align:right; width:42pt" class="gridlines gridlinesp">19</td>', $html);
59+
$spreadsheet->disconnectWorksheets();
60+
}
61+
}

0 commit comments

Comments
 (0)