I'm trying to read some worksheets using a filter, but the problem is that I can't get the cell values, when those cells are using cells
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objReader->setLoadSheetsOnly('Data Sheet #1');
$myFilter = new CellReadFilter(1, 7, range('A', 'F'));
$objReader->setReadDataOnly(true);
$objReader->setReadFilter($myFilter);
$objPHPExcel = $objReader->load('sampleData/example1.xls');
$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach ($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo $sheetIndex, ' => ', $loadedSheetName, '<br />';
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
}
My excel file, on the F1
cell, I use the following formula: =C2
, but the output of my script says that F1 is null
and not 23.45 as expected.
0 => Data Sheet #1
array (size=3)
1 =>
array (size=6)
'A' => string 'Integer Numbers' (length=15)
'B' => float 123
'C' => float 234
'D' => float -345
'E' => float 456
'F' => null
2 =>
array (size=6)
'A' => string 'Floating Point Numbers' (length=22)
'B' => float 1.23
'C' => float 23.45
'D' => float 3.45E-6
'E' => float -45.678
'F' => float 56.78
3 =>
array (size=6)
'A' => string 'Strings' (length=7)
'B' => string 'Hello' (length=5)
'C' => string 'World' (length=5)
'D' => null
'E' => string 'PHPExcel' (length=8)
'F' => null
and my cell filter class looks like in the documentation sample:
class CellReadFilter implements PHPExcel_Reader_IReadFilter {
private $_startRow = 0;
private $_endRow = 0;
private $_column = array();
public function __construct($startRow, $endRow, $column) {
$this->_startRow = $startRow;
$this->_endRow = $endRow;
$this->_column = array_merge($column, array('AA'));
}
public function readCell($column, $row, $worksheetName = '') {
if ($row >= $this->_startRow && $row <= $this->_endRow) {
if (in_array($column, $this->_column)) { return true; }
}
return false;
}
}
Validation for you read filter, modify the readCell to test what criteria are being applied, and to identify which cells match the criteria and why they are being accepted/rejected:
public function readCell($column, $row, $worksheetName = '') {
echo 'Testing worksheet ', $worksheetName, 'row ', $row, ' column ', $column, PHP_EOL;
if ($row >= $this->_startRow && $row <= $this->_endRow) {
'Cell is within row range',PHP_EOL;
if (in_array($column, $this->_column)) {
'VALID: Cell is within column range',PHP_EOL;
return true;
}
'INVALID: Cell is outside column range',PHP_EOL;
} else {
'INVALID: Cell is outside row range',PHP_EOL;
}
return false;
}