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

Improve HTML Writer #1464

Merged
merged 9 commits into from
May 18, 2020
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
27 changes: 9 additions & 18 deletions docs/topics/reading-and-writing-to-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -681,35 +681,26 @@ Supported methods:
- `generateStyles()`
- `generateSheetData()`
- `generateHTMLFooter()`
- `generateHTMLAll()`

Here's an example which retrieves all parts independently and merges
them into a resulting HTML page:

``` php
<?php
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
echo $writer->generateHTMLHeader();
?>

<style>
<!--
$hdr = $writer->generateHTMLHeader();
$sty = $writer->generateStyles(false); // do not write <style> and </style>
$newstyle = <<<EOF
<style type='text/css'>
$sty
html {
font-family: Times New Roman;
font-size: 9pt;
background-color: white;
background-color: yellow;
}

<?php
echo $writer->generateStyles(false); // do not write <style> and </style>
?>

-->
</style>

<?php
EOF;
echo preg_replace('@</head>@', "$newstyle\n</head>", $hdr);
echo $writer->generateSheetData();
echo $writer->generateHTMLFooter();
?>
```

#### Writing UTF-8 HTML files
Expand Down
14 changes: 14 additions & 0 deletions samples/Basic/17a_Html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use PhpOffice\PhpSpreadsheet\Writer\Html;

require __DIR__ . '/../Header.php';
$spreadsheet = require __DIR__ . '/../templates/sampleSpreadsheet.php';

$filename = $helper->getFilename(__FILE__, 'html');
$writer = new Html($spreadsheet);

$callStartTime = microtime(true);
$writer->setEmbedImages(true);
$writer->save($filename);
$helper->logWrite($writer, $filename, $callStartTime);
1 change: 1 addition & 0 deletions samples/Basic/25_In_memory_image.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
$drawing->setMimeType(MemoryDrawing::MIMETYPE_DEFAULT);
$drawing->setHeight(36);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
$drawing->setCoordinates('C5');

// Save
$helper->write($spreadsheet, __FILE__, ['Xlsx', 'Html']);
144 changes: 100 additions & 44 deletions src/PhpSpreadsheet/Style/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,15 @@ private static function formatAsFraction(&$value, &$format)
$adjustedDecimalPart = $decimalPart / $GCD;
$adjustedDecimalDivisor = $decimalDivisor / $GCD;

if ((strpos($format, '0') !== false) || (strpos($format, '#') !== false) || (substr($format, 0, 3) == '? ?')) {
if ((strpos($format, '0') !== false)) {
$value = "$sign$integerPart $adjustedDecimalPart/$adjustedDecimalDivisor";
} elseif ((strpos($format, '#') !== false)) {
if ($integerPart == 0) {
$value = "$sign$adjustedDecimalPart/$adjustedDecimalDivisor";
} else {
$value = "$sign$integerPart $adjustedDecimalPart/$adjustedDecimalDivisor";
}
} elseif ((substr($format, 0, 3) == '? ?')) {
if ($integerPart == 0) {
$integerPart = '';
}
Expand Down Expand Up @@ -653,9 +661,12 @@ private static function formatStraightNumericValue($value, $format, array $match

private static function formatAsNumber($value, $format)
{
if ($format === self::FORMAT_CURRENCY_EUR_SIMPLE) {
return 'EUR ' . sprintf('%1.2f', $value);
}
// The "_" in this string has already been stripped out,
// so this test is never true. Furthermore, testing
// on Excel shows this format uses Euro symbol, not "EUR".
//if ($format === self::FORMAT_CURRENCY_EUR_SIMPLE) {
// return 'EUR ' . sprintf('%1.2f', $value);
//}

// Some non-number strings are quoted, so we'll get rid of the quotes, likewise any positional * symbols
$format = str_replace(['"', '*'], '', $format);
Expand Down Expand Up @@ -717,6 +728,89 @@ private static function formatAsNumber($value, $format)
return $value;
}

private static function splitFormatCompare($value, $cond, $val, $dfcond, $dfval)
{
if (!$cond) {
$cond = $dfcond;
$val = $dfval;
}
switch ($cond) {
case '>':
return $value > $val;

case '<':
return $value < $val;

case '<=':
return $value <= $val;

case '<>':
return $value != $val;

case '=':
return $value == $val;
}

return $value >= $val;
}

private static function splitFormat($sections, $value)
{
// Extract the relevant section depending on whether number is positive, negative, or zero?
// Text not supported yet.
// Here is how the sections apply to various values in Excel:
// 1 section: [POSITIVE/NEGATIVE/ZERO/TEXT]
// 2 sections: [POSITIVE/ZERO/TEXT] [NEGATIVE]
// 3 sections: [POSITIVE/TEXT] [NEGATIVE] [ZERO]
// 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
$cnt = count($sections);
$color_regex = '/\\[(' . implode('|', Color::NAMED_COLORS) . ')\\]/';
$cond_regex = '/\\[(>|>=|<|<=|=|<>)([+-]?\\d+([.]\\d+)?)\\]/';
$colors = ['', '', '', '', ''];
$condops = ['', '', '', '', ''];
$condvals = [0, 0, 0, 0, 0];
for ($idx = 0; $idx < $cnt; ++$idx) {
if (preg_match($color_regex, $sections[$idx], $matches)) {
$colors[$idx] = $matches[0];
$sections[$idx] = preg_replace($color_regex, '', $sections[$idx]);
}
if (preg_match($cond_regex, $sections[$idx], $matches)) {
$condops[$idx] = $matches[1];
$condvals[$idx] = $matches[2];
$sections[$idx] = preg_replace($cond_regex, '', $sections[$idx]);
}
}
$color = $colors[0];
$format = $sections[0];
$absval = $value;
switch ($cnt) {
case 2:
$absval = abs($value);
if (!self::splitFormatCompare($value, $condops[0], $condvals[0], '>=', 0)) {
$color = $colors[1];
$format = $sections[1];
}

break;
case 3:
case 4:
$absval = abs($value);
if (!self::splitFormatCompare($value, $condops[0], $condvals[0], '>', 0)) {
if (self::splitFormatCompare($value, $condops[1], $condvals[1], '<', 0)) {
$color = $colors[1];
$format = $sections[1];
} else {
$color = $colors[2];
$format = $sections[2];
}
}

break;
}

return [$color, $format, $absval];
}

/**
* Convert a value in a pre-defined format to a PHP string.
*
Expand Down Expand Up @@ -745,50 +839,12 @@ public static function toFormattedString($value, $format, $callBack = null)
// Get the sections, there can be up to four sections, separated with a semi-colon (but only if not a quoted literal)
$sections = preg_split('/(;)(?=(?:[^"]|"[^"]*")*$)/u', $format);

// Extract the relevant section depending on whether number is positive, negative, or zero?
// Text not supported yet.
// Here is how the sections apply to various values in Excel:
// 1 section: [POSITIVE/NEGATIVE/ZERO/TEXT]
// 2 sections: [POSITIVE/ZERO/TEXT] [NEGATIVE]
// 3 sections: [POSITIVE/TEXT] [NEGATIVE] [ZERO]
// 4 sections: [POSITIVE] [NEGATIVE] [ZERO] [TEXT]
switch (count($sections)) {
case 1:
$format = $sections[0];

break;
case 2:
$format = ($value >= 0) ? $sections[0] : $sections[1];
$value = abs($value); // Use the absolute value
break;
case 3:
$format = ($value > 0) ?
$sections[0] : (($value < 0) ?
$sections[1] : $sections[2]);
$value = abs($value); // Use the absolute value
break;
case 4:
$format = ($value > 0) ?
$sections[0] : (($value < 0) ?
$sections[1] : $sections[2]);
$value = abs($value); // Use the absolute value
break;
default:
// something is wrong, just use first section
$format = $sections[0];

break;
}
[$colors, $format, $value] = self::splitFormat($sections, $value);

// In Excel formats, "_" is used to add spacing,
// The following character indicates the size of the spacing, which we can't do in HTML, so we just use a standard space
$format = preg_replace('/_./', ' ', $format);

// Save format with color information for later use below
$formatColor = $format;
// Strip colour information
$color_regex = '/\[(' . implode('|', Color::NAMED_COLORS) . ')\]/';
$format = preg_replace($color_regex, '', $format);
// Let's begin inspecting the format and converting the value to a formatted string

// Check for date/time characters (not inside quotes)
Expand All @@ -809,7 +865,7 @@ public static function toFormattedString($value, $format, $callBack = null)
// Additional formatting provided by callback function
if ($callBack !== null) {
[$writerInstance, $function] = $callBack;
$value = $writerInstance->$function($value, $formatColor);
$value = $writerInstance->$function($value, $colors);
}

return $value;
Expand Down
Loading