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 <?php
/** /**
* PHPExcel * PHPExcel
* *
@ -24,8 +25,6 @@
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE## * @version ##VERSION##, ##DATE##
*/ */
/** PHPExcel root directory */ /** PHPExcel root directory */
if (!defined('PHPEXCEL_ROOT')) { if (!defined('PHPEXCEL_ROOT')) {
/** /**
@ -44,26 +43,28 @@ if (!defined('PHPEXCEL_ROOT')) {
*/ */
class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
{ {
/** /**
* Input encoding * Input encoding
* *
* @var string * @var string
*/ */
private $_inputEncoding = 'ANSI'; protected $_inputEncoding = 'ANSI';
/** /**
* Sheet index to read * Sheet index to read
* *
* @var int * @var int
*/ */
private $_sheetIndex = 0; protected $_sheetIndex = 0;
/** /**
* Formats * Formats
* *
* @var array * @var array
*/ */
private $_formats = array( 'h1' => array( 'font' => array( 'bold' => true, protected $_formats = array(
'h1' => array('font' => array('bold' => true,
'size' => 24, 'size' => 24,
), ),
), // Bold, 24pt ), // Bold, 24pt
@ -93,18 +94,20 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
), ),
), // Blue underlined ), // Blue underlined
'hr' => array('borders' => array('bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN, 'hr' => array('borders' => array('bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN,
'color' => array( PHPExcel_Style_Color::COLOR_BLACK, 'color' => array(\PHPExcel_Style_Color::COLOR_BLACK,
), ),
), ),
), ),
), // Bottom border ), // Bottom border
); );
protected $rowspan = array();
/** /**
* Create a new PHPExcel_Reader_HTML * Create a new PHPExcel_Reader_HTML
*/ */
public function __construct() { public function __construct()
{
$this->_readFilter = new PHPExcel_Reader_DefaultReadFilter(); $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') public function setInputEncoding($pValue = 'ANSI')
{ {
$this->_inputEncoding = $pValue; $this->_inputEncoding = $pValue;
return $this; 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 // 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; protected function _setTableStartColumn($column)
private $_nestedColumn = array('A'); {
private function _setTableStartColumn($column) {
if ($this->_tableLevel == 0) if ($this->_tableLevel == 0)
$column = 'A'; $column = 'A';
++$this->_tableLevel; ++$this->_tableLevel;
@ -177,16 +181,20 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
return $this->_nestedColumn[$this->_tableLevel]; return $this->_nestedColumn[$this->_tableLevel];
} }
private function _getTableStartColumn() { protected function _getTableStartColumn()
{
return $this->_nestedColumn[$this->_tableLevel]; return $this->_nestedColumn[$this->_tableLevel];
} }
private function _releaseTableStartColumn() { protected function _releaseTableStartColumn()
{
--$this->_tableLevel; --$this->_tableLevel;
return array_pop($this->_nestedColumn); return array_pop($this->_nestedColumn);
} }
private function _flushCell($sheet,$column,$row,&$cellContent) { protected function _flushCell($sheet, $column, $row, &$cellContent)
{
if (is_string($cellContent)) { if (is_string($cellContent)) {
// Simple String content // Simple String content
if (trim($cellContent) > '') { if (trim($cellContent) > '') {
@ -194,7 +202,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
// echo 'FLUSH CELL: ' , $column , $row , ' => ' , $cellContent , '<br />'; // echo 'FLUSH CELL: ' , $column , $row , ' => ' , $cellContent , '<br />';
// Write to worksheet to be done here... // Write to worksheet to be done here...
// ... we return the cell so we can mess about with styles more easily // ... 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; $this->_dataArray[$row][$column] = $cellContent;
} }
} else { } else {
@ -205,7 +213,8 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
$cellContent = (string) ''; $cellContent = (string) '';
} }
private function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent){ protected function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent, $format = null)
{
foreach ($element->childNodes as $child) { foreach ($element->childNodes as $child) {
if ($child instanceof DOMText) { if ($child instanceof DOMText) {
$domText = preg_replace('/\s+/', ' ', trim($child->nodeValue)); $domText = preg_replace('/\s+/', ' ', trim($child->nodeValue));
@ -313,7 +322,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
} else { } else {
if ($cellContent > '') { if ($cellContent > '') {
$this->_flushCell($sheet, $column, $row, $cellContent); $this->_flushCell($sheet, $column, $row, $cellContent);
$row += 2; $row++;
} }
// echo 'START OF PARAGRAPH: ' , '<br />'; // echo 'START OF PARAGRAPH: ' , '<br />';
$this->_processDomElement($child, $sheet, $row, $column, $cellContent); $this->_processDomElement($child, $sheet, $row, $column, $cellContent);
@ -324,7 +333,7 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
$sheet->getStyle($column . $row)->applyFromArray($this->_formats[$child->nodeName]); $sheet->getStyle($column . $row)->applyFromArray($this->_formats[$child->nodeName]);
} }
$row += 2; $row++;
$column = 'A'; $column = 'A';
} }
break; break;
@ -367,11 +376,11 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
$this->_processDomElement($child, $sheet, $row, $column, $cellContent); $this->_processDomElement($child, $sheet, $row, $column, $cellContent);
break; break;
case 'tr' : case 'tr' :
++$row;
$column = $this->_getTableStartColumn(); $column = $this->_getTableStartColumn();
$cellContent = ''; $cellContent = '';
// echo 'START OF TABLE ' , $this->_tableLevel , ' ROW<br />'; // 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 />'; // echo 'END OF TABLE ' , $this->_tableLevel , ' ROW<br />';
break; break;
case 'th' : case 'th' :
@ -379,7 +388,46 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
// echo 'START OF TABLE ' , $this->_tableLevel , ' CELL<br />'; // 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 />'; // echo 'END OF TABLE ' , $this->_tableLevel , ' CELL<br />';
while (isset($this->rowspan[$column . $row])) {
++$column;
}
$this->_flushCell($sheet, $column, $row, $cellContent); $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; ++$column;
break; break;
case 'body' : case 'body' :
@ -422,21 +470,16 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
$objPHPExcel->setActiveSheetIndex($this->_sheetIndex); $objPHPExcel->setActiveSheetIndex($this->_sheetIndex);
// Create a new DOM object // Create a new DOM object
$dom = new DOMDocument; $dom = new domDocument;
// Reload the HTML file into the DOM object // 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); $loaded = $dom->loadHTMLFile($pFilename);
}
if ($loaded === FALSE) { 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 // Discard white space
$dom->preserveWhiteSpace = false; $dom->preserveWhiteSpace = false;
$row = 0; $row = 0;
$column = 'A'; $column = 'A';
$content = ''; $content = '';
@ -451,7 +494,8 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
* *
* @return int * @return int
*/ */
public function getSheetIndex() { public function getSheetIndex()
{
return $this->_sheetIndex; return $this->_sheetIndex;
} }
@ -461,9 +505,12 @@ class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_
* @param int $pValue Sheet index * @param int $pValue Sheet index
* @return PHPExcel_Reader_HTML * @return PHPExcel_Reader_HTML
*/ */
public function setSheetIndex($pValue = 0) { public function setSheetIndex($pValue = 0)
{
$this->_sheetIndex = $pValue; $this->_sheetIndex = $pValue;
return $this; 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: (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: (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: (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): 2014-03-02 (v1.8.0):