Skip to content
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

Removing last row incorrect behavior #1364

Closed
TimGa opened this issue Feb 17, 2020 · 2 comments · Fixed by #1365
Closed

Removing last row incorrect behavior #1364

TimGa opened this issue Feb 17, 2020 · 2 comments · Fixed by #1365

Comments

@TimGa
Copy link
Contributor

TimGa commented Feb 17, 2020

This is:

- [X] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Before
+-----+                         After
|A1|B1|                         +-----+
+-----+                         |A1|B1|
|A2|B2| -> Removing last row -> +-----+
+-----+                         |A2|B2|
|A3|B3|                         +-----+
+-----+

After removing last 3rd row from the table
rest rows (1st and,2nd) should remain

What is the current behavior?

Before
+-----+
|A1|B1|                         After
+-----+                         +-----+
|A2|B2| -> Removing last row -> |A1|B1|
+-----+                         +-----+
|A3|B3|
+-----+

But in fact when removing last 3rd row,
the 2nd row also disappears

What are the steps to reproduce?

<?php

require_once 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$arrayData = [
    ['A1'],
    ['A2'],
    ['A3'],
];

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->fromArray($arrayData);

$worksheet->removeRow(3, 1);

echo $worksheet->getCellByColumnAndRow(1, 1)->getValue() . PHP_EOL; // expect 'A1'
echo $worksheet->getCellByColumnAndRow(1, 2)->getValue() . PHP_EOL; // expect 'A2'

expected result is:

A1
A2

actual result is:

A1

Which versions of PhpSpreadsheet and PHP are affected?

PhpSpreadsheet v1.10.1
PHP v7.4.2

@stale
Copy link

stale bot commented Apr 17, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label Apr 17, 2020
@stale stale bot closed this as completed Apr 25, 2020
PowerKiKi pushed a commit that referenced this issue Apr 26, 2020
`$highestRow = $this->getHighestDataRow();` was calculated after `$this->getCellCollection()->removeRow($pRow + $r);` - this is the root reason for incorrect rows removal because removing last row will change '$this->getHighestDataRow()' value, but removing row from the middle will not change it. So, removing last row causes incorrect `$highestRow` value that is used for wiping out empty rows from the bottom of the table:
```php
for ($r = 0; $r < $pNumRows; ++$r) {
    $this->getCellCollection()->removeRow($highestRow);
    --$highestRow;
}
```
To prevent this incorrect behavior I've moved highest row calculation before row removal.
But this still doesn't solve another problem when trying remove non existing rows: in this case the code above will remove `$pNumRows` rows from below of the table, e.g. if `$highestRow=4` and `$pNumRows=6`, than rows 4, 3, 2, 1, 0, -1 will be deleted. Obviously, this is not good, that is why I've added `$removedRowsCounter` to fix this issue.
And finally, moved Exception to early if statement to get away from unnecessary 'if-else'.

Fixes #1364
Closes #1365
@drola drola mentioned this issue Jun 1, 2020
5 tasks
@oleibman
Copy link
Collaborator

Fixed by PR #1365.

@oleibman oleibman removed the stale label Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants