Skip to content

copy row#4440

Closed
torresmald wants to merge 2 commits into
PHPOffice:masterfrom
torresmald:master
Closed

copy row#4440
torresmald wants to merge 2 commits into
PHPOffice:masterfrom
torresmald:master

Conversation

@torresmald
Copy link
Copy Markdown

This is:

  • a bugfix
  • a new feature
  • refactoring
  • additional unit tests

Checklist:

  • Changes are covered by unit tests
    • Changes are covered by existing unit tests
    • New unit tests have been added
  • Code style is respected
  • Commit message explains why the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
  • CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
  • Documentation is updated as necessary

Why this change is needed?

Provide an explanation of why this change is needed, with links to any Issues (if appropriate).
If this is a bugfix or a new feature, and there are no existing Issues, then please also create an issue that will make it easier to track progress with this PR.

@oleibman
Copy link
Copy Markdown
Collaborator

oleibman commented Apr 9, 2025

You can probably clean up the phpcs and php-cs-fixer errors with:

composer fix

Comment thread README.md
@@ -1,5 +1,7 @@
# PhpSpreadsheet

BY JONATHAN
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why these changes are here. Please delete them. There is space below in this file to describe the changes you are making.

* @return void
*/
public function copyRow($from, $to) {
$this->getRowDimension($to)->setRowHeight($this->getRowDimension($from)->getRowHeight());
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's say you want to copy from row 1, which has cells A1/C1/E1, to row 2 which has cells A2/B2/F2. I think your code will replace A2/C2/E2 but leave B2/F2 unchanged, which is probably not what you want - you need to clear B2/F2.

$toCellCoordinates = $fromCellIt->getColumn().$to;
// Cache data as trying to save the cells as variables doesn't work
$style = $this->getCell($fromCoordinates)->getStyle();
$value = $this->getCell($fromCoordinates)->getValue();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be right for formulas. Let's say cell K3 has the value 10, and A1 has the value =1+K3. In Excel, if I now copy row 1 to row 2, cell A2 will have the value =1+K4. This is probably how your method should behave. It can get tricky. Rather than trying to re-invent the wheel, your loop should probably call copyCells for each cell that you want to copy. That will (I hope) update the formulas and take care of the styles.

@oleibman
Copy link
Copy Markdown
Collaborator

oleibman commented Apr 9, 2025

In addition to other comments, you will need to add unit tests to demonstrate that your function works correctly.

@torresmald
Copy link
Copy Markdown
Author

torresmald commented Apr 9, 2025 via email

@oleibman
Copy link
Copy Markdown
Collaborator

According to author, it was unintentional to make this a pull request. Closing.

@oleibman oleibman closed this Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants