Skip to content

Commit 88908a8

Browse files
authored
Merge pull request #4089 from oleibman/issue1515minor
Html Writer Minor Fixes
2 parents c196847 + 6c1a2e5 commit 88908a8

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
4141
- Problem rendering line chart with missing plot label. [PR #4074](https://github.com/PHPOffice/PhpSpreadsheet/pull/4074)
4242
- More RTL in Xlsx/Html Comments [Issue #4004](https://github.com/PHPOffice/PhpSpreadsheet/issues/4004) [PR #4065](https://github.com/PHPOffice/PhpSpreadsheet/pull/4065)
4343
- Empty String in sharedStrings. [Issue #4063](https://github.com/PHPOffice/PhpSpreadsheet/issues/4063) [PR #4064](https://github.com/PHPOffice/PhpSpreadsheet/pull/4064)
44+
- Html Writer Minor Fixes. [PR #4089](https://github.com/PHPOffice/PhpSpreadsheet/pull/4089)
4445
- Changes to INDEX function. [Issue #64](https://github.com/PHPOffice/PhpSpreadsheet/issues/64) [PR #4088](https://github.com/PHPOffice/PhpSpreadsheet/pull/4088)
4546
- Ods Reader and Whitespace Text Nodes. [Issue #804](https://github.com/PHPOffice/PhpSpreadsheet/issues/804) [PR #4087](https://github.com/PHPOffice/PhpSpreadsheet/pull/4087)
4647
- Ods Xml Reader and Whitespace Text Nodes. [Issue #804](https://github.com/PHPOffice/PhpSpreadsheet/issues/804) [PR #4087](https://github.com/PHPOffice/PhpSpreadsheet/pull/4087)

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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
);
46+
$sheet->getStyle('B2:D4')->getBorders()->applyFromArray(
47+
[
48+
'allBorders' => [
49+
'borderStyle' => Border::BORDER_NONE,
50+
'color' => ['rgb' => '808080'],
51+
],
52+
],
53+
);
54+
55+
$writer = new Html($spreadsheet);
56+
$writer->setUseInlineCss(true);
57+
$html = $writer->generateHTMLAll();
58+
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);
59+
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);
60+
$spreadsheet->disconnectWorksheets();
61+
}
62+
}

0 commit comments

Comments
 (0)