Skip to content

Fix Set-ExcelRange -Height changing the height of the row after the range - #1741

Open
Mike-Crowley wants to merge 1 commit into
dfinke:masterfrom
Mike-Crowley:fix-1738-row-height
Open

Fix Set-ExcelRange -Height changing the height of the row after the range#1741
Mike-Crowley wants to merge 1 commit into
dfinke:masterfrom
Mike-Crowley:fix-1738-row-height

Conversation

@Mike-Crowley

Copy link
Copy Markdown

Fixes #1738

Problem

Set-ExcelRange -Height (and Set-ExcelRow -Height, which delegates to it) also changed the height of the first row after the requested range. From the issue's repro, setting row 1 to 42 and rows 4:5 to 32 produced:

42, 42, 15, 32, 32, 32, 15   # rows 2 and 6 wrongly modified

Cause

An off-by-one in the row loop in Set-ExcelRange. EPPlus's ExcelRange.Rows is a row count (End.Row - Start.Row + 1), so

($Range.Start.Row)..($Range.Start.Row + $Range.Rows)

iterates one row past the end of the range (a 1-row range loops N..N+1). The equivalent -Width logic for columns already subtracts 1, which is why Set-ExcelColumn -Width was unaffected.

Fix

Subtract 1, matching the -Width branch:

($Range.Start.Row)..($Range.Start.Row + $Range.Rows - 1)

The issue's repro now produces 42, 15, 15, 32, 32, 15, 15 as expected. As a side effect this also fixes a crash when the range ends at the worksheet's last possible row (the old code called .Row(1048577), which throws).

Tests

Added a regression context to Set-Row_Set-Column-SetFormat.tests.ps1 covering Set-ExcelRow on a single row and Set-ExcelRange on a multi-row range, asserting the neighbouring rows keep the default height. The new tests fail against the unfixed code ("Expected 15, but got 42") and pass with the fix. Also added the same Import-Module $PSScriptRoot\..\ImportExcel.psd1 -Force line used by sibling test files so this file tests the repo code when run standalone.

Verified on Windows PowerShell 5.1 and PowerShell 7; full test file passes (22/22).

🤖 Generated with Claude Code

…ange

Set-ExcelRange -Height (and Set-ExcelRow -Height, which delegates to it)
iterated one row past the end of the requested range: for a range
starting at row N with R rows it looped N..(N+R), setting R+1 row
heights. Subtract 1 so only the rows inside the range are changed,
matching the -Width logic for columns.

Adds regression tests covering Set-ExcelRow on a single row and
Set-ExcelRange on a multi-row range, asserting neighbouring rows keep
the default height, and imports the local module in the test file so it
tests the repo code when run standalone.

Fixes dfinke#1738

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Mike-Crowley

Copy link
Copy Markdown
Author

Note on the failing Azure Pipelines checks: the failure is pre-existing pipeline infrastructure, not this change. Every job stops at CI/CI.ps1:113 with:

Invoke-Pester : A parameter cannot be found that matches parameter name 'OutputFile'.

CI.ps1 uses Pester v4 syntax (-OutputFile, $testResults.TestResult), and the build agents now install Pester 5+, which removed those. Other open PRs (#1723, #1729) fail all six jobs the same way. The GitHub Actions workflow (.github/workflows/ci.yml), which runs plain Invoke-Pester, is the one that reflects this PR's actual test results once approved to run.

@dfinke

dfinke commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Hi @Mike-Crowley, you're a machine! Thanks for all the PRs and analysis, fantastic. Lots to chew on.

It will take be a time to work through this and I have a lot on my plate.

At a quick glance, I had been planning to turn off the Azure CI/CD. Had not taken the time to do so.

I am going to kick off copilot on the PRs (thank you again for all of them), they look good so far.

Then will proceed from there.

Thank you - Doug

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes an off-by-one error in Set-ExcelRange -Height that caused the row immediately after a targeted range to have its height modified as well (and could crash when the range ended at the worksheet’s last row). Adds a regression test context to ensure only the intended rows are affected.

Changes:

  • Fix row-iteration bounds for -Height when applying to an ExcelRange (stop at end row, not one past it).
  • Add Pester regression coverage for Set-ExcelRow -Height and Set-ExcelRange -Height, asserting neighboring rows retain the default height.
  • Ensure the modified test file imports the module directly when run standalone.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
Public/Set-ExcelRange.ps1 Corrects the height-setting loop to iterate only within the requested row range.
tests/Set-Row_Set-Column-SetFormat.tests.ps1 Adds regression tests for Issue #1738 and imports the module for standalone test execution.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Mike-Crowley

Copy link
Copy Markdown
Author

Happy to help! As I recently mentioned in another issue, ImportExcel is my favorite open source PowerShell project and I wanted to use some of my Claude Fable budget to clear out a bit of the tech debt.

@dfinke

dfinke commented Jul 31, 2026

Copy link
Copy Markdown
Owner

That is great. So GitHub Copilot AI is reviewing Claude Fable work - love it

Glad ImportExcel has helped out.

I used GPT-5.x to work out addtions to ImportExcle to make it experiment making it "agentic".

I have a local branch, when I publish it, I'll tag you.

Good stuff!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set-ExcelRow -Height modifies the height of the next row as well

3 participants