Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Html Writer Styles when Using Inline Css #3680

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/PhpSpreadsheet/Writer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ private function generateRowCellDataValue(Worksheet $worksheet, Cell $cell, stri
* @param null|Cell|string $cell
* @param array|string $cssClass
*/
private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass, string $cellType): string
private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass): string
{
$cellData = ' ';
if ($cell instanceof Cell) {
Expand All @@ -1384,14 +1384,10 @@ private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass, st
$cssClass .= ' style' . $cell->getXfIndex();
$cssClass .= ' ' . $cell->getDataType();
} elseif (is_array($cssClass)) {
if ($cellType == 'th') {
if (isset($this->cssStyles['th.style' . $cell->getXfIndex()])) {
$cssClass = array_merge($cssClass, $this->cssStyles['th.style' . $cell->getXfIndex()]);
}
} else {
if (isset($this->cssStyles['td.style' . $cell->getXfIndex()])) {
$cssClass = array_merge($cssClass, $this->cssStyles['td.style' . $cell->getXfIndex()]);
}
$index = $cell->getXfIndex();
$styleIndex = 'td.style' . $index . ', th.style' . $index;
if (isset($this->cssStyles[$styleIndex])) {
$cssClass = array_merge($cssClass, $this->cssStyles[$styleIndex]);
}

// General horizontal alignment: Actual horizontal alignment depends on dataType
Expand Down Expand Up @@ -1511,7 +1507,7 @@ private function generateRow(Worksheet $worksheet, array $values, $row, $cellTyp
[$cell, $cssClass, $coordinate] = $this->generateRowCellCss($worksheet, $cellAddress, $row, $colNum);

// Cell Data
$cellData = $this->generateRowCellData($worksheet, $cell, $cssClass, $cellType);
$cellData = $this->generateRowCellData($worksheet, $cell, $cssClass);

// Hyperlink?
if ($worksheet->hyperlinkExists($coordinate) && !$worksheet->getHyperlink($coordinate)->isInternal()) {
Expand Down
37 changes: 37 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use PHPUnit\Framework\TestCase;

class Issue3678Test extends TestCase
{
public function testInlineAndNot(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue(1);
$styleArray = [
'fill' => [
'fillType' => Fill::FILL_SOLID,
'color' => ['rgb' => 'FFFF00'],
],
];
$sheet->getStyle('A1')->applyFromArray($styleArray);
$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";
$style2 = $style1 . '; text-align:right; width:42pt';
$writer = new Html($spreadsheet);
$html = $writer->generateHtmlAll();
self::assertStringContainsString('td.style1, th.style1 { ' . $style1 . ' }', $html);
self::assertStringContainsString('<td class="column0 style1 n">1</td>', $html);
self::assertStringContainsString('table.sheet0 col.col0 { width:42pt }', $html);
self::assertStringContainsString('.n { text-align:right }', $html);
$writer->setUseInlineCss(true);
$html = $writer->generateHtmlAll();
self::assertStringContainsString('<td style="' . $style2 . '">1</td>', $html);
$spreadsheet->disconnectWorksheets();
}
}