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

Add feature to get text & background color from inline CSS : td&th #180

Closed
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion docs/references/features-cross-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@
<td></td>
<td></td>
<td></td>
<td></td>
<td style="text-align: center; color: orange;">●</td>
<td></td>
<td></td>
<td></td>
Expand Down
19 changes: 19 additions & 0 deletions samples/46_ReadHtml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

// Turn off error reporting
error_reporting(0);

use PhpOffice\PhpSpreadsheet\IOFactory;

require __DIR__ . '/Header.php';

$html = __DIR__ . '/templates/46readHtml.html';
$callStartTime = microtime(true);

$objReader = IOFactory::createReader('Html');
$objPHPExcel = $objReader->load($html);

$helper->logRead('Html', $html, $callStartTime);

// Save
$helper->write($objPHPExcel, __FILE__);
130 changes: 130 additions & 0 deletions samples/templates/46readHtml.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Competency List</title>
</head>
<body>
<table class="w3-table-all notranslate">
<tr>
<th style="background-color: #0000FF;color:#FFFFFF">Color Name</th>
<th style="background-color: #FF0000">HEX</th>
<th style="background-color: #008000">Color</th>
</tr>

<tr>
<td style="color:#000000">Black</td>
<td>#000000</td>
<td style="background-color:#000000">&nbsp;</td>
</tr>

<tr>
<td style="color:#0000FF">Blue</td>
<td>#0000FF</td>
<td style="background-color:#0000FF">&nbsp;</td>
</tr>


<tr>
<td style="color:#008000">Green</td>
<td>#008000</td>
<td style="background-color:#008000">&nbsp;</td>
</tr>

<tr>
<td style="color:#00FFFF">Cyan</td>
<td>#00FFFF</td>
<td style="background-color:#00FFFF">&nbsp;</td>
</tr>

<tr>
<td style="color:#800080">Purple</td>
<td>#800080</td>
<td style="background-color:#800080">&nbsp;</td>
</tr>


<tr>
<td style="color:#800080">Grey</td>
<td>#808080</td>
<td style="background-color:#808080">&nbsp;</td>
</tr>

<tr>
<td style="color:#87CEEB">SkyBlue</td>
<td>#87CEEB</td>
<td style="background-color:#87CEEB">&nbsp;</td>
</tr>


<tr>
<td style="color:#A52A2A">Brown</td>
<td>#A52A2A</td>
<td style="background-color:#A52A2A">&nbsp;</td>
</tr>

<tr>
<td style="color:#C0C0C0;background-color: #000000">Silver</td>
<td>#C0C0C0</td>
<td style="background-color:#C0C0C0">&nbsp;</td>
</tr>

<tr>
<td style="color:#D2691E">Chocolate</td>
<td>#D2691E</td>
<td style="background-color:#D2691E">&nbsp;</td>
</tr>

<tr>
<td style="color:#D2B48C;background-color: #000000">Tan</td>
<td>#D2B48C</td>
<td style="background-color:#D2B48C">&nbsp;</td>
</tr>


<tr>
<td style="color:#EE82EE;background-color: #000000">Violet</td>
<td>#EE82EE</td>
<td style="background-color:#EE82EE">&nbsp;</td>
</tr>


<tr>
<td style="color:#FF0000;background-color: #000000">Red</td>
<td>#FF0000</td>
<td style="background-color:#FF0000">&nbsp;</td>
</tr>

<tr>
<td style="color:#FFC0CB;background-color: #000000">Pink</td>
<td>#FFC0CB</td>
<td style="background-color:#FFC0CB">&nbsp;</td>
</tr>

<tr>
<td style="color:#FFD700;background-color: #000000">Gold</td>
<td>#FFD700</td>
<td style="background-color:#FFD700">&nbsp;</td>
</tr>

<tr>
<td style="color:#FFFF00;background-color: #000000">Yellow</td>
<td>#FFFF00</td>
<td style="background-color:#FFFF00">&nbsp;</td>
</tr>

<tr>
<td style="color:#FFFFE0;background-color: #000000">LightYellow</td>
<td>#FFFFE0</td>
<td style="background-color:#FFFFE0">&nbsp;</td>
</tr>

<tr>
<td style="color:#FFFFFF;background-color: #000000">White</td>
<td>#FFFFFF</td>
<td style="background-color:#FFFFFF">&nbsp;</td>
</tr>
</table>
</body>
</html>
47 changes: 47 additions & 0 deletions src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ protected function processDomElement(DOMNode $element, Worksheet $sheet, &$row,
case 'td':
$this->processDomElement($child, $sheet, $row, $column, $cellContent);

// apply inline style
$this->applyInlineStyle($sheet, $row, $column, $attributeArray);

while (isset($this->rowspan[$column . $row])) {
++$column;
}
Expand Down Expand Up @@ -584,4 +587,48 @@ public function securityScan($xml)

return $xml;
}

/**
* Apply inline css inline style.
*
* NOTES :
* Currently only intended for td & th element,
* and only takes 'background-color' and 'color'; property with HEX color
*
* TODO :
* - Implement to other propertie, such as border
*
* @param Worksheet $sheet
* @param array $attributeArray
* @param int $row
* @param string $column
*/
private function applyInlineStyle(&$sheet, $row, $column, $attributeArray)
{
$supported_styles = ['background-color', 'color'];

// add color styles (background & text) from dom element,currently support : td & th, using ONLY inline css style with RGB color
if (isset($attributeArray['style'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can return early here to avoid too many nested blocks:

if (!isset($attributeArray['style'])) {
    return;
}

$styles = explode(';', $attributeArray['style']);
foreach ($styles as $st) {
$value = explode(':', $st);
if (!empty(trim($value[0]))) {
if (in_array(trim($value[0]), $supported_styles)) {
$style_color = null;
//check if has #, so we can get clean hex
if (substr(trim($value[1]), 0, 1) == '#') {
$style_color = substr(trim($value[1]), 1);
}
if ($style_color) {
if (trim($value[0]) == 'background-color') {
$sheet->getStyle($column . $row)->applyFromArray(['fill' => ['type' => Fill::FILL_SOLID, 'color' => ['rgb' => "{$style_color}"]]]);
} elseif (trim($value[0]) == 'color') {
$sheet->getStyle($column . $row)->applyFromArray(['font' => ['color' => ['rgb' => "$style_color}"]]]);
}
}
}
}
}
}
}
}