Start work on implementing an option to ignore "empty" cells when reading a file

This commit is contained in:
MarkBaker 2015-12-30 00:24:36 +00:00
parent 7eb10adb3b
commit 2058c8468a
2 changed files with 57 additions and 13 deletions

View File

@ -36,6 +36,15 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
*/ */
protected $readDataOnly = false; 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? * Read charts that are defined in the workbook?
* Identifies whether the Reader should read the definitions for any charts that exist 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; 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? * Read charts in workbook?
* If this is true, then the Reader will include any charts that exist in the workbook. * If this is true, then the Reader will include any charts that exist in the workbook.

View File

@ -3687,6 +3687,7 @@ class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExce
$column = self::getInt2d($recordData, 2); $column = self::getInt2d($recordData, 2);
$columnString = PHPExcel_Cell::stringFromColumnIndex($column); $columnString = PHPExcel_Cell::stringFromColumnIndex($column);
$emptyCell = true;
// Read cell? // Read cell?
if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) { if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
// offset: 4; size: 2; index to XF record // offset: 4; size: 2; index to XF record
@ -3727,14 +3728,20 @@ class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExce
} }
} }
} }
if ($this->readEmptyCells || trim($richText->getPlainText()) !== '') {
$cell = $this->phpSheet->getCell($columnString . ($row + 1)); $cell = $this->phpSheet->getCell($columnString . ($row + 1));
$cell->setValueExplicit($richText, PHPExcel_Cell_DataType::TYPE_STRING); $cell->setValueExplicit($richText, PHPExcel_Cell_DataType::TYPE_STRING);
$emptyCell = false;
}
} else { } else {
if ($this->readEmptyCells || trim($this->sst[$index]['value']) !== '') {
$cell = $this->phpSheet->getCell($columnString . ($row + 1)); $cell = $this->phpSheet->getCell($columnString . ($row + 1));
$cell->setValueExplicit($this->sst[$index]['value'], PHPExcel_Cell_DataType::TYPE_STRING); $cell->setValueExplicit($this->sst[$index]['value'], PHPExcel_Cell_DataType::TYPE_STRING);
$emptyCell = false;
}
} }
if (!$this->readDataOnly) { if (!$this->readDataOnly && !$emptyCell) {
// add style information // add style information
$cell->setXfIndex($this->mapCellXfIndex[$xfIndex]); $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 // offset: 4; size: 2 x nc; list of indexes to XF records
// add style information // add style information
if (!$this->readDataOnly) { if (!$this->readDataOnly && $this->readEmptyCells) {
for ($i = 0; $i < $length / 2 - 3; ++$i) { for ($i = 0; $i < $length / 2 - 3; ++$i) {
$columnString = PHPExcel_Cell::stringFromColumnIndex($fc + $i); $columnString = PHPExcel_Cell::stringFromColumnIndex($fc + $i);
@ -4163,6 +4170,7 @@ class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExce
$string = $this->readByteStringLong(substr($recordData, 6)); $string = $this->readByteStringLong(substr($recordData, 6));
$value = $string['value']; $value = $string['value'];
} }
if ($this->readEmptyCells || trim($value) !== '') {
$cell = $this->phpSheet->getCell($columnString . ($row + 1)); $cell = $this->phpSheet->getCell($columnString . ($row + 1));
$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING); $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
@ -4172,6 +4180,7 @@ class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExce
} }
} }
} }
}
/** /**
@ -4198,11 +4207,10 @@ class PHPExcel_Reader_Excel5 extends PHPExcel_Reader_Abstract implements PHPExce
$xfIndex = self::getInt2d($recordData, 4); $xfIndex = self::getInt2d($recordData, 4);
// add style information // add style information
if (!$this->readDataOnly) { if (!$this->readDataOnly && $this->readEmptyCells) {
$this->phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->mapCellXfIndex[$xfIndex]); $this->phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->mapCellXfIndex[$xfIndex]);
} }
} }
} }