Skip to content

Don't Iterate Cell Ranges on Non-existent Sheets - #5035

Merged
oleibman merged 4 commits into
PHPOffice:masterfrom
oleibman:issue5032
Oct 9, 2026
Merged

oleibman merged 4 commits into
PHPOffice:masterfrom
oleibman:issue5032

Conversation

@oleibman

@oleibman oleibman commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Fix #5032. When evaluating formulas, stop iterating a cell range when the sheet on which the range is located does not exist.

While testing, discovered some commented-out tests in ParseFormulaTest, having nothing to do with this change, that could now be commented back in.

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

Fix PHPOffice#5032. When evaluating formulas, stop iterating a cell range when the sheet on which the range is located does not exist.

While testing, discovered some commented-out tests in `ParseFormulaTest`, having nothing to do with this change, that could now be commented back in.
@dkulyk

dkulyk commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

I tried a bounded range with this fix. With 1, 2, 42 in C1:C3 of the sheet that holds the formulas:

Formula Result
=MAX(Missing!C:C) #REF!
=MAX(Missing!C1:C5) 42
=SUM(Missing!C1:C3) 45
=IFERROR(MAX(Missing!C1:C5),"oops") 42

So a whole column or row on a missing sheet gives #REF!, and a bounded range on it is evaluated on the formula's own sheet.

@oleibman

oleibman commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator Author

@dkulyk Thank you for the feedback. I will see what I can do.

@dkulyk noticed a case which wasn't handled properly. I believe the fix works for everything ... except `COLUMNS` and `ROWS`, where Excel seems to ignore the sheet, and I just can't figure out how to do it.
@oleibman

oleibman commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator Author

@dkulyk Please give the latest push a shot. As the commit message makes clear, I think this works for almost everything, except for COLUMNS and ROWS, which in Excel return the "expected" answer if the reference is to a sheet which doesn't exist. I just can't figure out how to do that in PhpSpreadsheet. I'm willing to take that hit - the function isn't much used, and, since the worksheet is ignored, it should probably be omitted.

@dkulyk

dkulyk commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

I checked the bounded ranges from my table. Looks good to me.

On ROWS/COLUMNS: LibreOffice 26.8 makes no exception for them either. In an xlsx it gives #NAME? for every formula with a missing sheet, =ROWS(Missing!B2:K6) included.

@oleibman
oleibman enabled auto-merge October 9, 2026 19:05
@oleibman
oleibman added this pull request to the merge queue Oct 9, 2026
Merged via the queue into PHPOffice:master with commit 242961d Oct 9, 2026
13 checks passed
@oleibman
oleibman deleted the issue5032 branch October 9, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Referring to a not existing sheet hangs the script on $writer->save()

2 participants