Skip to content

Add support for adding images anchored to one cell #746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2019

Conversation

karlvr
Copy link
Contributor

@karlvr karlvr commented Feb 14, 2019

Addresses #738 #650 and is related to #702.

There is a bug in cell-position-xform.js that assumes that columns are 640,000 EMU wide and 180,000 EMU tall. This assumption is broken if you customise cell width or height, and the bug appears if you use fractional columns or rows to try to position your image.

This PR adds support for adding images using just a tl and then providing an ext that contains a width and height in pixels. The width and height are converted internally to EMU using a conversion at 96dpi. That seems to be the right thing to do, but something I'm a little unsure about!

This change should read as well as write the one-cell-anchor-xform, although I have only used writing so far but there is a test for reading and writing.

@karlvr
Copy link
Contributor Author

karlvr commented Feb 14, 2019

You can test it by adding https://github.com/karlvr/exceljs#integration to your package.json.

@alubbe alubbe requested a review from guyonroche April 3, 2019 08:58
@WebDeg-Brian
Copy link

WebDeg-Brian commented Apr 3, 2019

I just added it to my package.json and works like a charm. Is ext compulsory?
Edit: It would be awesome to have this fix merged ASAP. I'm stuck with this problem for quite a while now.

@karlvr
Copy link
Contributor Author

karlvr commented Apr 3, 2019

I think you need to provide either ext or use the existing method. I guess we could allow ext to be omitted if we just used the image width and height?

@WebDeg-Brian
Copy link

@karlvr definitely. If ext is not specified, we should use the original image's dimensions. One might get the image programmatically somewhere without knowing the exact width and height. Obviously there are libraries out there that help with finding images' sizes, but still would be nice if your PR does it out of the box

@karlvr
Copy link
Contributor Author

karlvr commented Apr 5, 2019 via email

@guyonroche
Copy link
Collaborator

Looking into this now. Looks good, my only comment so far would be to offset tl by -1, -1 like the two-cell anchor. I'll look into this

@lukasz-t-michalski
Copy link

Hi @karlvr , I have an issue using this functionality - exporting images looks great but they aren't getting deleted when I deleted a row from the Excel file. When I use use the default (tl: br:) the attribute editAs: 'oneCell' resolved this issue, but it doesn't seem to work when I use tl: ext: Did anyone else came across this problem?

@guyonroche guyonroche merged commit d66ea6c into exceljs:master May 3, 2019
@Devil7DK
Copy link

@karlvr I have issues when using ext for image size.

The excel says that "We found unreadable content in 'Export.xlsx'. Do you want to recovery the contents of this workbook?" After repairing the file using excel, its opening fine.

The error log says "Repaired Records: Drawing from /xl/drawings/drawing1.xml part (Drawing shape)". So i compared the drawing1.xml from both repaired and unrepaired workbooks. And found that if i remove editAs="oneCell" attribute from all xdr:oneCellAnchor tags, the error is gone.

@censys-git
Copy link

If you set ext with height and width it works but you get the MS error and repair. I did a diff on the drawings xml of the original and repaired. No changes what-so-ever were made other than on the root xdr:oneCellAnchor where excelJS sets an attribute 'editAs="absolute"' or 'editAs="oneCell"', Microsoft wipes that out (no attribute at all). That is the only change. So very confused on what the heck we need to do to make this work. Should you not be setting this attribute? Is it defined elsewhere where MS is barfing?

@YSHIDM
Copy link

YSHIDM commented Aug 29, 2023

If you set ext with height and width it works but you get the MS error and repair. I did a diff on the drawings xml of the original and repaired. No changes what-so-ever were made other than on the root xdr:oneCellAnchor where excelJS sets an attribute 'editAs="absolute"' or 'editAs="oneCell"', Microsoft wipes that out (no attribute at all). That is the only change. So very confused on what the heck we need to do to make this work. Should you not be setting this attribute? Is it defined elsewhere where MS is barfing?

+1

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.

7 participants