-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cell::getCoordinate returns wrong value #2546
Comments
This is because any cell maintained in user code is actually a pointer to the last cell referenced in the cell collection. When you iterate over the cells, you're setting $lastCell as a pointer to the cell which contains a non-null value ( <?php
require __DIR__ . '/vendor/autoload.php';
// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$row = $spreadsheet->getActiveSheet()->getRowIterator()->current();
$lastCellAddress = null;
foreach ($row->getCellIterator() as $cell) {
if ($cell->getValue() !== null) {
$lastCellAddress = $cell->getCoordinate();
} else {
break;
}
}
lastCell = $spreadsheet->getActiveSheet()->getCell($lastCellAddress ?? 'A1' );
var_dump($lastCell->getValue()); //
var_dump($lastCell->getCoordinate()); //
die; Alternatively, tell the $columnIterator = $row->getCellIterator();
$columnIterator ->setIterateOnlyExistingCells(true);
foreach ($columnIterator as $cell) { ... } |
@MarkBaker, is this treatment for "any cell maintained in user code" also the explanation for issue #2262? |
@oleibman There's a good chance that's the case; the calculation engine takes great care to maintain (and restore if necessary) the active cell pointer; but note everything is necessarily as clean in doing so |
This is:
I have .xlsx file with some data (regular table with first row as table header, L-column is last with values) and trying to get coordinates of last cell of first row where value is not null.
What is the expected behavior?
Expeceted coordinates: L1
What is the current behavio
Current coordinates: M1
What are the steps to reproduce?
Itearate row cells, check cell value and stop on null value.
Which versions of PhpSpreadsheet and PHP are affected?
The text was updated successfully, but these errors were encountered: