Skip to content
This repository was archived by the owner on Jan 2, 2019. It is now read-only.

Commit 2058c84

Browse files
author
MarkBaker
committed
Start work on implementing an option to ignore "empty" cells when reading a file
1 parent 7eb10ad commit 2058c84

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

Classes/PHPExcel/Reader/Abstract.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
3636
*/
3737
protected $readDataOnly = false;
3838

39+
/**
40+
* Read empty cells?
41+
* Identifies whether the Reader should read data values for cells all cells, or should ignore cells containing
42+
* null value or empty string
43+
*
44+
* @var boolean
45+
*/
46+
protected $readEmptyCells = true;
47+
3948
/**
4049
* Read charts that are defined in the workbook?
4150
* Identifies whether the Reader should read the definitions for any charts that exist in the workbook;
@@ -89,6 +98,33 @@ public function setReadDataOnly($pValue = false)
8998
return $this;
9099
}
91100

101+
/**
102+
* Read empty cells?
103+
* If this is true (the default), then the Reader will read data values for all cells, irrespective of value.
104+
* If false it will not read data for cells containing a null value or an empty string.
105+
*
106+
* @return boolean
107+
*/
108+
public function getReadEmptyCells()
109+
{
110+
return $this->readEmptyCells;
111+
}
112+
113+
/**
114+
* Set read empty cells
115+
* Set to true (the default) to advise the Reader read data values for all cells, irrespective of value.
116+
* Set to false to advise the Reader to ignore cells containing a null value or an empty string.
117+
*
118+
* @param boolean $pValue
119+
*
120+
* @return PHPExcel_Reader_IReader
121+
*/
122+
public function setReadEmptyCells($pValue = true)
123+
{
124+
$this->readEmptyCells = $pValue;
125+
return $this;
126+
}
127+
92128
/**
93129
* Read charts in workbook?
94130
* If this is true, then the Reader will include any charts that exist in the workbook.

Classes/PHPExcel/Reader/Excel5.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3687,6 +3687,7 @@ private function readLabelSst()
36873687
$column = self::getInt2d($recordData, 2);
36883688
$columnString = PHPExcel_Cell::stringFromColumnIndex($column);
36893689

3690+
$emptyCell = true;
36903691
// Read cell?
36913692
if (($this->getReadFilter() !== null) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->phpSheet->getTitle())) {
36923693
// offset: 4; size: 2; index to XF record
@@ -3727,14 +3728,20 @@ private function readLabelSst()
37273728
}
37283729
}
37293730
}
3730-
$cell = $this->phpSheet->getCell($columnString . ($row + 1));
3731-
$cell->setValueExplicit($richText, PHPExcel_Cell_DataType::TYPE_STRING);
3731+
if ($this->readEmptyCells || trim($richText->getPlainText()) !== '') {
3732+
$cell = $this->phpSheet->getCell($columnString . ($row + 1));
3733+
$cell->setValueExplicit($richText, PHPExcel_Cell_DataType::TYPE_STRING);
3734+
$emptyCell = false;
3735+
}
37323736
} else {
3733-
$cell = $this->phpSheet->getCell($columnString . ($row + 1));
3734-
$cell->setValueExplicit($this->sst[$index]['value'], PHPExcel_Cell_DataType::TYPE_STRING);
3737+
if ($this->readEmptyCells || trim($this->sst[$index]['value']) !== '') {
3738+
$cell = $this->phpSheet->getCell($columnString . ($row + 1));
3739+
$cell->setValueExplicit($this->sst[$index]['value'], PHPExcel_Cell_DataType::TYPE_STRING);
3740+
$emptyCell = false;
3741+
}
37353742
}
37363743

3737-
if (!$this->readDataOnly) {
3744+
if (!$this->readDataOnly && !$emptyCell) {
37383745
// add style information
37393746
$cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
37403747
}
@@ -4108,7 +4115,7 @@ private function readMulBlank()
41084115

41094116
// offset: 4; size: 2 x nc; list of indexes to XF records
41104117
// add style information
4111-
if (!$this->readDataOnly) {
4118+
if (!$this->readDataOnly && $this->readEmptyCells) {
41124119
for ($i = 0; $i < $length / 2 - 3; ++$i) {
41134120
$columnString = PHPExcel_Cell::stringFromColumnIndex($fc + $i);
41144121

@@ -4163,12 +4170,14 @@ private function readLabel()
41634170
$string = $this->readByteStringLong(substr($recordData, 6));
41644171
$value = $string['value'];
41654172
}
4166-
$cell = $this->phpSheet->getCell($columnString . ($row + 1));
4167-
$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
4173+
if ($this->readEmptyCells || trim($value) !== '') {
4174+
$cell = $this->phpSheet->getCell($columnString . ($row + 1));
4175+
$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
41684176

4169-
if (!$this->readDataOnly) {
4170-
// add cell style
4171-
$cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
4177+
if (!$this->readDataOnly) {
4178+
// add cell style
4179+
$cell->setXfIndex($this->mapCellXfIndex[$xfIndex]);
4180+
}
41724181
}
41734182
}
41744183
}
@@ -4198,11 +4207,10 @@ private function readBlank()
41984207
$xfIndex = self::getInt2d($recordData, 4);
41994208

42004209
// add style information
4201-
if (!$this->readDataOnly) {
4210+
if (!$this->readDataOnly && $this->readEmptyCells) {
42024211
$this->phpSheet->getCell($columnString . ($row + 1))->setXfIndex($this->mapCellXfIndex[$xfIndex]);
42034212
}
42044213
}
4205-
42064214
}
42074215

42084216

0 commit comments

Comments
 (0)