Skip to content

This closes #2217: Preserve editAs positioning when copying pictures with twoCellAnchor - #2378

Open
blankInPajamas wants to merge 1 commit into
qax-os:masterfrom
blankInPajamas:fix/picture-copy-scale
Open

This closes #2217: Preserve editAs positioning when copying pictures with twoCellAnchor#2378
blankInPajamas wants to merge 1 commit into
qax-os:masterfrom
blankInPajamas:fix/picture-copy-scale

Conversation

@blankInPajamas

Copy link
Copy Markdown

Fixes #2217

Problem

Copying a picture via AddPictureFromBytes produced a visibly stretched/
distorted copy when the source picture was anchored with a twoCellAnchor
element carrying an explicit editAs="oneCell" attribute. This is a valid,
spec-compliant anchor pattern commonly produced by WPS Office (and other
third-party tools), distinct from excelize's own writer, which always emits
a true oneCellAnchor element for size-locked pictures.

Root cause

Two issues compounded to cause this:

  1. extractPictureFromAnchor and extractPictureFromDecodeAnchor only
    inferred Format.Positioning = "oneCell" when the anchor had no <to>
    element at all. They never checked the anchor's own editAs attribute,
    so a twoCellAnchor with editAs="oneCell" (which does have a <to>)
    was never recognized as size-locked.

  2. For anchors that fall through to the fallback decode path
    (extractDecodeCellAnchor, used when a drawing part mixes chart/
    graphicFrame anchors together with picture anchors), the re-parse of the
    anchor's inner content did not preserve the parent anchor's editAs
    attribute, so it was lost even when correctly read initially.

The end result: Format.Positioning came back empty for these pictures, so
AddPictureFromBytes wrote a plain twoCellAnchor with no editAs on
copy — making the copied picture resize with its cells instead of keeping
its original locked size.

Fix

  • Recognize editAs directly when reading a picture's anchor, falling back
    to the existing "oneCell when no to" inference only when editAs is
    absent.
  • Preserve the parent anchor's editAs when the decode-fallback path
    re-parses anchor content.

Testing

  • Added a new case to TestGetPicture covering a twoCellAnchor with
    editAs="oneCell" and a to element present, asserting GetPictures
    returns Format.Positioning == "oneCell".
  • Verified against a real .xlsx file produced by WPS Office: before the
    fix, copying the picture dropped editAs="oneCell" from the resulting
    drawing*.xml (becoming a plain twoCellAnchor, which visibly stretched
    the picture); after the fix, the copy correctly writes a proper
    size-locked oneCellAnchor, matching the source picture's appearance.
  • Ran the full test suite (go test ./...) with no regressions.
  • Proceeded to run (gofmt -l) . and (go vet ./...) and found no errors

@blankInPajamas blankInPajamas changed the title fix: preserve editAs positioning when copying pictures with twoCellAnchor This closes #2217: Preserve editAs positioning when copying pictures with twoCellAnchor Aug 16, 2026
@xuri xuri added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent image size after read image and insert image back

2 participants