Bugfix: Work Item GH-554 - Whitespace after toRichTextObject()
This commit is contained in:
parent
88711dc7b9
commit
f968a95ca5
|
@ -34,28 +34,28 @@ date_default_timezone_set('Europe/London');
|
|||
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
|
||||
|
||||
/** Include PHPExcel */
|
||||
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
|
||||
require_once dirname(__FILE__) . '/../src/Bootstrap.php';
|
||||
|
||||
|
||||
// Create new PHPExcel object
|
||||
echo date('H:i:s') , " Create new PHPExcel object" , EOL;
|
||||
$objPHPExcel = new PHPExcel();
|
||||
$objPHPExcel = new \PHPExcel\Spreadsheet();
|
||||
|
||||
// Set document properties
|
||||
echo date('H:i:s') , " Set document properties" , EOL;
|
||||
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
|
||||
->setLastModifiedBy("Maarten Balliauw")
|
||||
->setTitle("PHPExcel Test Document")
|
||||
->setSubject("PHPExcel Test Document")
|
||||
->setDescription("Test document for PHPExcel, generated using PHP classes.")
|
||||
->setKeywords("office PHPExcel php")
|
||||
->setCategory("Test result file");
|
||||
->setLastModifiedBy("Maarten Balliauw")
|
||||
->setTitle("PHPExcel Test Document")
|
||||
->setSubject("PHPExcel Test Document")
|
||||
->setDescription("Test document for PHPExcel, generated using PHP classes.")
|
||||
->setKeywords("office PHPExcel php")
|
||||
->setCategory("Test result file");
|
||||
|
||||
|
||||
// Add some data
|
||||
echo date('H:i:s') , " Add some data" , EOL;
|
||||
|
||||
$html1='<font color="#0000ff">
|
||||
$html1 = '<font color="#0000ff">
|
||||
<h1 align="center">My very first example of rich text<br />generated from html markup</h1>
|
||||
<p>
|
||||
<font size="14" COLOR="rgb(0,255,128)">
|
||||
|
@ -64,11 +64,11 @@ while this block uses an <u>underline</u>.
|
|||
</font>
|
||||
</p>
|
||||
<p align="right"><font size="9" color="red">
|
||||
I want to eat <ins><del>healthy food</del><strong>pizza</strong></ins>.
|
||||
I want to eat <ins><del>healthy food</del> <strong>pizza</strong></ins>.
|
||||
</font>
|
||||
';
|
||||
|
||||
$html2='<p>
|
||||
$html2 = '<p>
|
||||
<font color="#ff0000">
|
||||
100°C is a hot temperature
|
||||
</font>
|
||||
|
@ -78,43 +78,55 @@ $html2='<p>
|
|||
</font>
|
||||
</p>';
|
||||
|
||||
$html3='2<sup>3</sup> equals 8';
|
||||
$html3 = '2<sup>3</sup> equals 8';
|
||||
|
||||
$html4='H<sub>2</sub>SO<sub>4</sub> is the chemical formula for Sulphuric acid';
|
||||
$html4 = 'H<sub>2</sub>SO<sub>4</sub> is the chemical formula for Sulphuric acid';
|
||||
|
||||
$html5 = '<strong>bold</strong>, <em>italic</em>, <strong><em>bold+italic</em></strong>';
|
||||
|
||||
$wizard = new PHPExcel_Helper_HTML;
|
||||
$wizard = new \PHPExcel\Helper\HTML;
|
||||
$richText = $wizard->toRichTextObject($html1);
|
||||
|
||||
$objPHPExcel->setActiveSheetIndex(0)
|
||||
$objPHPExcel->getActiveSheet()
|
||||
->setCellValue('A1', $richText);
|
||||
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(48);
|
||||
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
|
||||
$objPHPExcel->getActiveSheet()
|
||||
->getColumnDimension('A')
|
||||
->setWidth(48);
|
||||
$objPHPExcel->getActiveSheet()
|
||||
->getRowDimension(1)
|
||||
->setRowHeight(-1);
|
||||
$objPHPExcel->getActiveSheet()->getStyle('A1')
|
||||
->getAlignment()
|
||||
->setWrapText(true);
|
||||
|
||||
$richText = $wizard->toRichTextObject($html2);
|
||||
|
||||
$objPHPExcel->setActiveSheetIndex(0)
|
||||
$objPHPExcel->getActiveSheet()
|
||||
->setCellValue('A2', $richText);
|
||||
|
||||
$objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
|
||||
$objPHPExcel->getActiveSheet()->getStyle('A2')
|
||||
$objPHPExcel->getActiveSheet()
|
||||
->getRowDimension(1)
|
||||
->setRowHeight(-1);
|
||||
$objPHPExcel->getActiveSheet()
|
||||
->getStyle('A2')
|
||||
->getAlignment()
|
||||
->setWrapText(true);
|
||||
|
||||
$objPHPExcel->setActiveSheetIndex(0)
|
||||
->setCellValue('A3', $wizard->toRichTextObject($html3));
|
||||
->setCellValue('A3', $wizard->toRichTextObject($html3));
|
||||
|
||||
$objPHPExcel->setActiveSheetIndex(0)
|
||||
->setCellValue('A4', $wizard->toRichTextObject($html4));
|
||||
|
||||
$objPHPExcel->setActiveSheetIndex(0)
|
||||
->setCellValue('A5', $wizard->toRichTextObject($html5));
|
||||
|
||||
|
||||
// Rename worksheet
|
||||
echo date('H:i:s') , " Rename worksheet" , EOL;
|
||||
$objPHPExcel->getActiveSheet()->setTitle('Simple');
|
||||
$objPHPExcel->getActiveSheet()
|
||||
->setTitle('Rich Text Examples');
|
||||
|
||||
|
||||
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
|
||||
|
@ -125,7 +137,7 @@ $objPHPExcel->setActiveSheetIndex(0);
|
|||
echo date('H:i:s') , " Write to Excel2007 format" , EOL;
|
||||
$callStartTime = microtime(true);
|
||||
|
||||
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
|
||||
$objWriter = \PHPExcel\IOFactory::createWriter($objPHPExcel, 'Excel2007');
|
||||
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
|
||||
$callEndTime = microtime(true);
|
||||
$callTime = $callEndTime - $callStartTime;
|
||||
|
@ -140,7 +152,7 @@ echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024
|
|||
echo date('H:i:s') , " Write to Excel5 format" , EOL;
|
||||
$callStartTime = microtime(true);
|
||||
|
||||
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
|
||||
$objWriter = \PHPExcel\IOFactory::createWriter($objPHPExcel, 'Excel5');
|
||||
$objWriter->save(str_replace('.php', '.xls', __FILE__));
|
||||
$callEndTime = microtime(true);
|
||||
$callTime = $callEndTime - $callStartTime;
|
||||
|
|
|
@ -23,12 +23,12 @@
|
|||
**************************************************************************************
|
||||
|
||||
|
||||
Planned for 1.8.2
|
||||
Planned for 1.9
|
||||
- Bugfix: (MBaker) - Fix to getCell() method when cell reference includes a worksheet reference
|
||||
- Bugfix: (ncrypthic) Work Item GH-570 - Ignore inlineStr type if formula element exists
|
||||
- Bugfix: (MBaker) Work Item GH-554 - Whitespace after toRichTextObject()
|
||||
- General: (umpirsky) Work Item GH-548 - Optimize vlookup() sort
|
||||
|
||||
|
||||
2015-04-30 (v1.8.1):
|
||||
- Bugfix: (goncons) Work Item GH-397 - Fix for Writing an Open Document cell with non-numeric formula
|
||||
- Bugfix: (sarciszewski) Work Item GH-329 - Avoid potential divide by zero in basedrawing
|
||||
|
|
|
@ -615,7 +615,7 @@ class HTML
|
|||
$this->initialise();
|
||||
|
||||
// Create a new DOM object
|
||||
$dom = new domDocument;
|
||||
$dom = new \DOMDocument;
|
||||
// Load the HTML file into the DOM object
|
||||
// Note the use of error suppression, because typically this will be an html fragment, so not fully valid markup
|
||||
$loaded = @$dom->loadHTML($html);
|
||||
|
@ -625,9 +625,27 @@ class HTML
|
|||
|
||||
$this->richTextObject = new \PHPExcel\RichText();
|
||||
$this->parseElements($dom);
|
||||
|
||||
// Clean any further spurious whitespace
|
||||
$this->cleanWhitespace();
|
||||
|
||||
return $this->richTextObject;
|
||||
}
|
||||
|
||||
protected function cleanWhitespace()
|
||||
{
|
||||
foreach($this->richTextObject->getRichTextElements() as $key => $element) {
|
||||
$text = $element->getText();
|
||||
// Trim any leading spaces on the first run
|
||||
if ($key == 0) {
|
||||
$text = ltrim($text);
|
||||
}
|
||||
// Trim any spaces immediately after a line break
|
||||
$text = preg_replace('/\n */mu', "\n", $text);
|
||||
$element->setText($text);
|
||||
}
|
||||
}
|
||||
|
||||
protected function buildTextRun()
|
||||
{
|
||||
$text = $this->stringData;
|
||||
|
@ -767,12 +785,16 @@ class HTML
|
|||
|
||||
protected function breakTag()
|
||||
{
|
||||
$this->stringData .= PHP_EOL;
|
||||
$this->stringData .= "\n";
|
||||
}
|
||||
|
||||
protected function parseTextNode(DOMText $textNode)
|
||||
protected function parseTextNode(\DOMText $textNode)
|
||||
{
|
||||
$domText = preg_replace('/\s+/u', ' ', ltrim($textNode->nodeValue));
|
||||
$domText = preg_replace(
|
||||
'/\s+/u',
|
||||
' ',
|
||||
str_replace(["\r", "\n"], ' ', $textNode->nodeValue)
|
||||
);
|
||||
$this->stringData .= $domText;
|
||||
$this->buildTextRun();
|
||||
}
|
||||
|
@ -787,7 +809,7 @@ class HTML
|
|||
}
|
||||
}
|
||||
|
||||
protected function parseElementNode(DOMElement $element)
|
||||
protected function parseElementNode(\DOMElement $element)
|
||||
{
|
||||
$callbackTag = strtolower($element->nodeName);
|
||||
$this->stack[] = $callbackTag;
|
||||
|
@ -795,18 +817,17 @@ class HTML
|
|||
$this->handleCallback($element, $callbackTag, $this->startTagCallbacks);
|
||||
|
||||
$this->parseElements($element);
|
||||
$this->stringData .= ' ';
|
||||
array_pop($this->stack);
|
||||
|
||||
$this->handleCallback($element, $callbackTag, $this->endTagCallbacks);
|
||||
}
|
||||
|
||||
protected function parseElements(DOMNode $element)
|
||||
protected function parseElements(\DOMNode $element)
|
||||
{
|
||||
foreach ($element->childNodes as $child) {
|
||||
if ($child instanceof DOMText) {
|
||||
if ($child instanceof \DOMText) {
|
||||
$this->parseTextNode($child);
|
||||
} elseif ($child instanceof DOMElement) {
|
||||
} elseif ($child instanceof \DOMElement) {
|
||||
$this->parseElementNode($child);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue