Feature: (cifren/MBaker) Work Item GH-205 - Handling merge cells in HTML Reader

This commit is contained in:
MarkBaker 2014-12-07 14:45:55 +00:00
parent b2e82a0e11
commit d7ea3e2ab0
2 changed files with 420 additions and 372 deletions

View File

@ -1,4 +1,5 @@
<?php
/**
* PHPExcel
*
@ -24,8 +25,6 @@
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE##
*/
/** PHPExcel root directory */
if (!defined('PHPEXCEL_ROOT')) {
/**
@ -44,67 +43,71 @@ if (!defined('PHPEXCEL_ROOT')) {
*/
class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
{
/**
* Input encoding
*
* @var string
*/
private $_inputEncoding = 'ANSI';
protected $_inputEncoding = 'ANSI';
/**
* Sheet index to read
*
* @var int
*/
private $_sheetIndex = 0;
protected $_sheetIndex = 0;
/**
* Formats
*
* @var array
*/
private $_formats = array( 'h1' => array( 'font' => array( 'bold' => true,
protected $_formats = array(
'h1' => array('font' => array('bold' => true,
'size' => 24,
),
), // Bold, 24pt
'h2' => array( 'font' => array( 'bold' => true,
'h2' => array('font' => array('bold' => true,
'size' => 18,
),
), // Bold, 18pt
'h3' => array( 'font' => array( 'bold' => true,
'h3' => array('font' => array('bold' => true,
'size' => 13.5,
),
), // Bold, 13.5pt
'h4' => array( 'font' => array( 'bold' => true,
'h4' => array('font' => array('bold' => true,
'size' => 12,
),
), // Bold, 12pt
'h5' => array( 'font' => array( 'bold' => true,
'h5' => array('font' => array('bold' => true,
'size' => 10,
),
), // Bold, 10pt
'h6' => array( 'font' => array( 'bold' => true,
'h6' => array('font' => array('bold' => true,
'size' => 7.5,
),
), // Bold, 7.5pt
'a' => array( 'font' => array( 'underline' => true,
'color' => array( 'argb' => PHPExcel_Style_Color::COLOR_BLUE,
'a' => array('font' => array('underline' => true,
'color' => array('argb' => PHPExcel_Style_Color::COLOR_BLUE,
),
),
), // Blue underlined
'hr' => array( 'borders' => array( 'bottom' => array( 'style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => array( PHPExcel_Style_Color::COLOR_BLACK,
'hr' => array('borders' => array('bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => array(\PHPExcel_Style_Color::COLOR_BLACK,
),
),
),
), // Bottom border
);
protected $rowspan = array();
/**
* Create a new PHPExcel_Reader_HTML
*/
public function __construct() {
public function __construct()
{
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
}
@ -149,6 +152,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
public function setInputEncoding($pValue = 'ANSI')
{
$this->_inputEncoding = $pValue;
return $this;
}
@ -163,12 +167,12 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
}
// Data Array used for testing only, should write to PHPExcel object on completion of tests
private $_dataArray = array();
protected $_dataArray = array();
protected $_tableLevel = 0;
protected $_nestedColumn = array('A');
private $_tableLevel = 0;
private $_nestedColumn = array('A');
private function _setTableStartColumn($column) {
protected function _setTableStartColumn($column)
{
if ($this->_tableLevel == 0)
$column = 'A';
++$this->_tableLevel;
@ -177,16 +181,20 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
return $this->_nestedColumn[$this->_tableLevel];
}
private function _getTableStartColumn() {
protected function _getTableStartColumn()
{
return $this->_nestedColumn[$this->_tableLevel];
}
private function _releaseTableStartColumn() {
protected function _releaseTableStartColumn()
{
--$this->_tableLevel;
return array_pop($this->_nestedColumn);
}
private function _flushCell($sheet,$column,$row,&$cellContent) {
protected function _flushCell($sheet, $column, $row, &$cellContent)
{
if (is_string($cellContent)) {
// Simple String content
if (trim($cellContent) > '') {
@ -194,7 +202,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
// echo 'FLUSH CELL: ' , $column , $row , ' => ' , $cellContent , '<br />';
// Write to worksheet to be done here...
// ... we return the cell so we can mess about with styles more easily
$cell = $sheet->setCellValue($column.$row,$cellContent,true);
$sheet->setCellValue($column . $row, $cellContent, true);
$this->_dataArray[$row][$column] = $cellContent;
}
} else {
@ -205,10 +213,11 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
$cellContent = (string) '';
}
private function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent){
foreach($element->childNodes as $child){
protected function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent, $format = null)
{
foreach ($element->childNodes as $child) {
if ($child instanceof DOMText) {
$domText = preg_replace('/\s+/',' ',trim($child->nodeValue));
$domText = preg_replace('/\s+/', ' ', trim($child->nodeValue));
if (is_string($cellContent)) {
// simply append the text if the cell content is a plain text string
$cellContent .= $domText;
@ -216,29 +225,29 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
// but if we have a rich text run instead, we need to append it correctly
// TODO
}
} elseif($child instanceof DOMElement) {
} elseif ($child instanceof DOMElement) {
// echo '<b>DOM ELEMENT: </b>' , strtoupper($child->nodeName) , '<br />';
$attributeArray = array();
foreach($child->attributes as $attribute) {
foreach ($child->attributes as $attribute) {
// echo '<b>ATTRIBUTE: </b>' , $attribute->name , ' => ' , $attribute->value , '<br />';
$attributeArray[$attribute->name] = $attribute->value;
}
switch($child->nodeName) {
switch ($child->nodeName) {
case 'meta' :
foreach($attributeArray as $attributeName => $attributeValue) {
switch($attributeName) {
foreach ($attributeArray as $attributeName => $attributeValue) {
switch ($attributeName) {
case 'content':
// TODO
// Extract character set, so we can convert to UTF-8 if required
break;
}
}
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
break;
case 'title' :
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
$sheet->setTitle($cellContent);
$cellContent = '';
break;
@ -252,19 +261,19 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
// echo 'STYLING, SPAN OR DIV<br />';
if ($cellContent > '')
$cellContent .= ' ';
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
if ($cellContent > '')
$cellContent .= ' ';
// echo 'END OF STYLING, SPAN OR DIV<br />';
break;
case 'hr' :
$this->_flushCell($sheet,$column,$row,$cellContent);
$this->_flushCell($sheet, $column, $row, $cellContent);
++$row;
if (isset($this->_formats[$child->nodeName])) {
$sheet->getStyle($column.$row)->applyFromArray($this->_formats[$child->nodeName]);
$sheet->getStyle($column . $row)->applyFromArray($this->_formats[$child->nodeName]);
} else {
$cellContent = '----------';
$this->_flushCell($sheet,$column,$row,$cellContent);
$this->_flushCell($sheet, $column, $row, $cellContent);
}
++$row;
case 'br' :
@ -273,26 +282,26 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
$cellContent .= "\n";
} else {
// Otherwise flush our existing content and move the row cursor on
$this->_flushCell($sheet,$column,$row,$cellContent);
$this->_flushCell($sheet, $column, $row, $cellContent);
++$row;
}
// echo 'HARD LINE BREAK: ' , '<br />';
break;
case 'a' :
// echo 'START OF HYPERLINK: ' , '<br />';
foreach($attributeArray as $attributeName => $attributeValue) {
switch($attributeName) {
foreach ($attributeArray as $attributeName => $attributeValue) {
switch ($attributeName) {
case 'href':
// echo 'Link to ' , $attributeValue , '<br />';
$sheet->getCell($column.$row)->getHyperlink()->setUrl($attributeValue);
$sheet->getCell($column . $row)->getHyperlink()->setUrl($attributeValue);
if (isset($this->_formats[$child->nodeName])) {
$sheet->getStyle($column.$row)->applyFromArray($this->_formats[$child->nodeName]);
$sheet->getStyle($column . $row)->applyFromArray($this->_formats[$child->nodeName]);
}
break;
}
}
$cellContent .= ' ';
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
// echo 'END OF HYPERLINK:' , '<br />';
break;
case 'h1' :
@ -308,23 +317,23 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
// If we're inside a table, replace with a \n
$cellContent .= "\n";
// echo 'LIST ENTRY: ' , '<br />';
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
// echo 'END OF LIST ENTRY:' , '<br />';
} else {
if ($cellContent > '') {
$this->_flushCell($sheet,$column,$row,$cellContent);
$row += 2;
$this->_flushCell($sheet, $column, $row, $cellContent);
$row++;
}
// echo 'START OF PARAGRAPH: ' , '<br />';
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
// echo 'END OF PARAGRAPH:' , '<br />';
$this->_flushCell($sheet,$column,$row,$cellContent);
$this->_flushCell($sheet, $column, $row, $cellContent);
if (isset($this->_formats[$child->nodeName])) {
$sheet->getStyle($column.$row)->applyFromArray($this->_formats[$child->nodeName]);
$sheet->getStyle($column . $row)->applyFromArray($this->_formats[$child->nodeName]);
}
$row += 2;
$row++;
$column = 'A';
}
break;
@ -333,27 +342,27 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
// If we're inside a table, replace with a \n
$cellContent .= "\n";
// echo 'LIST ENTRY: ' , '<br />';
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
// echo 'END OF LIST ENTRY:' , '<br />';
} else {
if ($cellContent > '') {
$this->_flushCell($sheet,$column,$row,$cellContent);
$this->_flushCell($sheet, $column, $row, $cellContent);
}
++$row;
// echo 'LIST ENTRY: ' , '<br />';
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
// echo 'END OF LIST ENTRY:' , '<br />';
$this->_flushCell($sheet,$column,$row,$cellContent);
$this->_flushCell($sheet, $column, $row, $cellContent);
$column = 'A';
}
break;
case 'table' :
$this->_flushCell($sheet,$column,$row,$cellContent);
$this->_flushCell($sheet, $column, $row, $cellContent);
$column = $this->_setTableStartColumn($column);
// echo 'START OF TABLE LEVEL ' , $this->_tableLevel , '<br />';
if ($this->_tableLevel > 1)
--$row;
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
// echo 'END OF TABLE LEVEL ' , $this->_tableLevel , '<br />';
$column = $this->_releaseTableStartColumn();
if ($this->_tableLevel > 1) {
@ -364,22 +373,61 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
break;
case 'thead' :
case 'tbody' :
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
break;
case 'tr' :
++$row;
$column = $this->_getTableStartColumn();
$cellContent = '';
// echo 'START OF TABLE ' , $this->_tableLevel , ' ROW<br />';
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
++$row;
// echo 'END OF TABLE ' , $this->_tableLevel , ' ROW<br />';
break;
case 'th' :
case 'td' :
// echo 'START OF TABLE ' , $this->_tableLevel , ' CELL<br />';
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
// echo 'END OF TABLE ' , $this->_tableLevel , ' CELL<br />';
$this->_flushCell($sheet,$column,$row,$cellContent);
while (isset($this->rowspan[$column . $row])) {
++$column;
}
$this->_flushCell($sheet, $column, $row, $cellContent);
if (isset($attributeArray['style']) && !empty($attributeArray['style'])) {
$styleAry = $this->getPhpExcelStyleArray($attributeArray['style']);
if (!empty($styleAry)) {
$sheet->getStyle($column . $row)->applyFromArray($styleAry);
}
}
//create merging rowspan
if (isset($attributeArray['rowspan']) && isset($attributeArray['colspan'])) {
$columnTo = $column;
for ($i = 0; $i < $attributeArray['colspan'] - 1; $i++) {
++$columnTo;
}
$range = $column . $row . ':' . $columnTo . ($row + $attributeArray['rowspan'] - 1);
foreach (\PHPExcel_Cell::extractAllCellReferencesInRange($range) as $value) {
$this->rowspan[$value] = true;
}
$sheet->mergeCells($range);
} elseif (isset($attributeArray['rowspan'])) {
$range = $column . $row . ':' . $column . ($row + $attributeArray['rowspan'] - 1);
foreach (\PHPExcel_Cell::extractAllCellReferencesInRange($range) as $value) {
$this->rowspan[$value] = true;
}
$sheet->mergeCells($range);
} elseif (isset($attributeArray['colspan'])) {
$columnTo = $column;
for ($i = 0; $i < $attributeArray['colspan'] - 1; $i++) {
++$columnTo;
}
$sheet->mergeCells($column . $row . ':' . $columnTo . $row);
$column = $columnTo;
}
++$column;
break;
case 'body' :
@ -387,10 +435,10 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
$column = 'A';
$content = '';
$this->_tableLevel = 0;
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
break;
default:
$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
$this->_processDomElement($child, $sheet, $row, $column, $cellContent);
}
}
}
@ -409,38 +457,33 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
// Open file to validate
$this->_openFile($pFilename);
if (!$this->_isValidFormat()) {
fclose ($this->_fileHandle);
fclose($this->_fileHandle);
throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid HTML file.");
}
// Close after validating
fclose ($this->_fileHandle);
fclose($this->_fileHandle);
// Create new PHPExcel
while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
$objPHPExcel->createSheet();
}
$objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
$objPHPExcel->setActiveSheetIndex($this->_sheetIndex);
// Create a new DOM object
$dom = new DOMDocument;
$dom = new domDocument;
// Reload the HTML file into the DOM object
if ((version_compare(PHP_VERSION, '5.4.0') >= 0) && defined(LIBXML_DTDLOAD)) {
$loaded = $dom->loadHTMLFile($pFilename, PHPExcel_Settings::getLibXmlLoaderOptions());
} else {
$loaded = $dom->loadHTMLFile($pFilename);
}
if ($loaded === FALSE) {
throw new PHPExcel_Reader_Exception('Failed to load '. $pFilename. ' as a DOM Document');
throw new PHPExcel_Reader_Exception('Failed to load ', $pFilename, ' as a DOM Document');
}
// Discard white space
$dom->preserveWhiteSpace = false;
$row = 0;
$column = 'A';
$content = '';
$this->_processDomElement($dom,$objPHPExcel->getActiveSheet(),$row,$column,$content);
$this->_processDomElement($dom, $objPHPExcel->getActiveSheet(), $row, $column, $content);
// Return
return $objPHPExcel;
@ -451,7 +494,8 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
*
* @return int
*/
public function getSheetIndex() {
public function getSheetIndex()
{
return $this->_sheetIndex;
}
@ -461,9 +505,12 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
* @param int $pValue Sheet index
* @return PHPExcel_Reader_HTML
*/
public function setSheetIndex($pValue = 0) {
public function setSheetIndex($pValue = 0)
{
$this->_sheetIndex = $pValue;
return $this;
}
}

View File

@ -49,6 +49,7 @@ Planned for v1.8.1
- Feature: (CQD) Work Item GH-389 - Additional Mac CJK codepage definitions
- Feature: (bolovincev) Work Item GH-269 - Update Worksheet.php getStyleByColumnAndRow() to allow a range of cells rather than just a single cell
- Feature: (MBaker) - New methods added for testing cell status within merge groups
- Feature: (cifren/MBaker) Work Item GH-205 - Handling merge cells in HTML Reader
2014-03-02 (v1.8.0):