I currently have the below code that will loop through the excel file and return rows with missing data. the output is as below:
- Row number: 37
- Missing : in cell B37
- Missing : in cell D37
What I would like to return is the following
- Row number: 37
- Missing nameFirst : in cell B37
- Missing nameLast : in cell D37
Where nameFirst is the value of cell B1 and nameLast is the value of D1.
<?PHP
require_once 'Excel/PHPExcel.php';
require_once 'Excel/PHPExcel/Reader/Excel2007.php';
class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
public function readCell($column, $row, $worksheetName = '') {
$Retour=false;
$column=PHPExcel_Cell::columnIndexFromString($column);// Warning ! A=1, not zero as usual
if($row<1 || $column>4)
$Retour=false;
else
$Retour=true;
return $Retour;
}
}
echo '<pre>';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objReader->setReadFilter( new MyReadFilter() );
$objPHPExcel = $objReader->load('test.xlsx');
$worksheet=$objPHPExcel->getSheet(0);
foreach ($worksheet->getRowIterator() as $row) {
if($row->getRowIndex()>1){
echo ' - Row number: ' . $row->getRowIndex() . "
";
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
foreach ($cellIterator as $cell) {
$colIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn());
if($cell->getCalculatedValue() == "")
echo ' - Missing <THIS IS WHERE I WANT THE INDEX CELL FOR THE COLUMN> :' . 'in cell ' . $cell->getCoordinate() . "
";
}
}
}
echo '</pre>';
?>
I tried changing the echo to include the variable $colIndex in addition to:
echo ' - Missing '. $worksheet->getCellByColumnAndRow($colIndex, 1)->getValue(). '. :' . $cell->getCoordinate() . "
";
But this causes a return of one row and only one correct cell and a whole bunch of blank variables
- Row number: 2
- Missing nameFirst. :B1
- Missing . :E1
- Missing . :F1
- Missing . :G1
- Missing . :H1