From 2058c8468a8fa9e7c5f5e2e2e98c09aaff9fc6ac Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Wed, 30 Dec 2015 00:24:36 +0000 Subject: [PATCH] Start work on implementing an option to ignore "empty" cells when reading a file --- Classes/PHPExcel/Reader/Abstract.php | 36 ++++++++++++++++++++++++++++ Classes/PHPExcel/Reader/Excel5.php | 34 ++++++++++++++++---------- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/Classes/PHPExcel/Reader/Abstract.php b/Classes/PHPExcel/Reader/Abstract.php index 5902574..189c70a 100644 --- a/Classes/PHPExcel/Reader/Abstract.php +++ b/Classes/PHPExcel/Reader/Abstract.php @@ -36,6 +36,15 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader */ protected $readDataOnly = false; + /** + * Read empty cells? + * Identifies whether the Reader should read data values for cells all cells, or should ignore cells containing + * null value or empty string + * + * @var boolean + */ + protected $readEmptyCells = true; + /** * Read charts that are defined in the workbook? * Identifies whether the Reader should read the definitions for any charts that exist in the workbook; @@ -89,6 +98,33 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader return $this; } + /** + * Read empty cells? + * If this is true (the default), then the Reader will read data values for all cells, irrespective of value. + * If false it will not read data for cells containing a null value or an empty string. + * + * @return boolean + */ + public function getReadEmptyCells() + { + return $this->readEmptyCells; + } + + /** + * Set read empty cells + * Set to true (the default) to advise the Reader read data values for all cells, irrespective of value. + * Set to false to advise the Reader to ignore cells containing a null value or an empty string. + * + * @param boolean $pValue + * + * @return PHPExcel_Reader_IReader + */ + public function setReadEmptyCells($pValue = true) + { + $this->readEmptyCells = $pValue; + return $this; + } + /** * Read charts in workbook? * If this is true, then the Reader will include any charts that exist in the workbook. diff --git a/Classes/PHPExcel/Reader/Excel5.php b/Classes/PHPExcel/Reader/Excel5.php index 770d435..8dd15af 100644 --- a/Classes/PHPExcel/Reader/Excel5.php +++ b/Classes/PHPExcel/Reader/Excel5.php @@ -3687,6 +3687,7 @@ class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExce $column = self::getInt2d($recordData, 2); $columnString = PHPExcel_Cell::stringFromColumnIndex($column); + $emptyCell = true; // Read cell? if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) { // offset: 4; size: 2; index to XF record @@ -3727,14 +3728,20 @@ class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExce } } } - $cell = $this->phpSheet->getCell($columnString . ($row + 1)); - $cell->setValueExplicit($richText, PHPExcel_Cell_DataType::TYPE_STRING); + if ($this->readEmptyCells || trim($richText->getPlainText()) !== '') { + $cell = $this->phpSheet->getCell($columnString . ($row + 1)); + $cell->setValueExplicit($richText, PHPExcel_Cell_DataType::TYPE_STRING); + $emptyCell = false; + } } else { - $cell = $this->phpSheet->getCell($columnString . ($row + 1)); - $cell->setValueExplicit($this->sst[$index]['value'], PHPExcel_Cell_DataType::TYPE_STRING); + if ($this->readEmptyCells || trim($this->sst[$index]['value']) !== '') { + $cell = $this->phpSheet->getCell($columnString . ($row + 1)); + $cell->setValueExplicit($this->sst[$index]['value'], PHPExcel_Cell_DataType::TYPE_STRING); + $emptyCell = false; + } } - if (!$this->readDataOnly) { + if (!$this->readDataOnly && !$emptyCell) { // add style information $cell->setXfIndex($this->mapCellXfIndex[$xfIndex]); } @@ -4108,7 +4115,7 @@ class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExce // offset: 4; size: 2 x nc; list of indexes to XF records // add style information - if (!$this->readDataOnly) { + if (!$this->readDataOnly && $this->readEmptyCells) { for ($i = 0; $i < $length / 2 - 3; ++$i) { $columnString = PHPExcel_Cell::stringFromColumnIndex($fc + $i); @@ -4163,12 +4170,14 @@ class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExce $string = $this->readByteStringLong(substr($recordData, 6)); $value = $string['value']; } - $cell = $this->phpSheet->getCell($columnString . ($row + 1)); - $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING); + if ($this->readEmptyCells || trim($value) !== '') { + $cell = $this->phpSheet->getCell($columnString . ($row + 1)); + $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING); - if (!$this->readDataOnly) { - // add cell style - $cell->setXfIndex($this->mapCellXfIndex[$xfIndex]); + if (!$this->readDataOnly) { + // add cell style + $cell->setXfIndex($this->mapCellXfIndex[$xfIndex]); + } } } } @@ -4198,11 +4207,10 @@ class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExce $xfIndex = self::getInt2d($recordData, 4); // add style information - if (!$this->readDataOnly) { + if (!$this->readDataOnly && $this->readEmptyCells) { $this->phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->mapCellXfIndex[$xfIndex]); } } - }