Skip to content

Fix exception when updating drawing hyperlinks and fix ignored tooltips#2378

Open
lievendf wants to merge 1 commit into
EPPlusSoftware:develop8from
lievendf:fix/drawing-hyperlink-reassignment
Open

Fix exception when updating drawing hyperlinks and fix ignored tooltips#2378
lievendf wants to merge 1 commit into
EPPlusSoftware:develop8from
lievendf:fix/drawing-hyperlink-reassignment

Conversation

@lievendf
Copy link
Copy Markdown
Contributor

@lievendf lievendf commented Jun 4, 2026

Summary

This PR fixes two bugs in ExcelDrawing.cs when working with hyperlinks on drawings (shapes, pictures, etc.):

  1. An exception (NullReferenceException or InvalidOperationException) when changing an existing hyperlink to a new one.
  2. Tooltips not being saved when assigning a new hyperlink with a tooltip.

What was wrong?

  1. The Exception:
    When you changed a drawing's hyperlink, the setter tried to delete the old link using ZipPackage.DeletePart(...). However, drawing hyperlinks are just XML relationships (.rels), not actual physical files inside the ZIP package. This caused an exception:

    • If the old link was a worksheet reference, it threw a NullReferenceException (because the target URI is null).
    • If the old link was an external web URL, it threw InvalidOperationException: Part does not exist.
  2. The Tooltip Bug:
    When writing the tooltip to the drawing's XML, the code checked if (Hyperlink is ExcelHyperLink excelLink). At this point, the backing field hadn't been updated yet, so Hyperlink returned the old hyperlink instead of the new one. This meant any new tooltip you set was ignored.

How this PR fixes it

  1. For the Exception: Changed the code to delete the relationship from the drawing part instead of trying to delete a zip package part:
    _drawings.Part.DeleteRelationship(HypRel.Id);
  2. For the Tooltip: Changed the check to use the incoming value instead of the old Hyperlink property:
    if (value is ExcelHyperLink excelLink)

Added Tests

Added a unit test DrawingHyperlinkUpdate_ShouldNotThrowException in HyperlinkTest.cs that:

  • Assigns an initial external link.
  • Updates it to a new external link.
  • Updates it to an internal sheet reference.
  • Saves the file and reads it back to make sure the tooltip and hyperlink are correct.

@lievendf lievendf force-pushed the fix/drawing-hyperlink-reassignment branch from 84d26db to f331367 Compare June 4, 2026 12:42
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.

2 participants