Skip to content
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

Adding Picture into a merged cell is unable to adjust the alignment in the cell #1958

Closed
Kevin8eace opened this issue Jul 19, 2024 · 5 comments

Comments

@Kevin8eace
Copy link

Description

I tried to insert a picture into a merged cell and I wanted to align the picture with the cell as "center".

Steps to reproduce the issue:

  1. Merge Cells
    if err := f.MergeCell("Sheet1", "A1", "C6"); err != nil { ...... }
  2. Add Picture
    if err := f.AddPicture("Sheet1", "A1", "image.png", &excelize.GraphicOptions{ AutoFit: true, LockAspectRatio: true, }); err != nil { ...... }
  3. Apply Alignment Style
    centralStyle, err := f.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{ Horizontal: "center", Vertical: "center", }, }) if err != nil { ...... }

Describe the results you received:
The outcome picture in the cell is still align left not center.
I read the source code in github and found that there's a const PictureInsertType to control the picture insert type, but I got error with
Functional\excelize.go:163:4: unknown field InsertType in struct literal of type excelize.Picture Functional\excelize.go:163:16: undefined: PictureInsertTypePlaceInCell.

And the code I used for trying to involve the PictureInsertType.
if err := f.AddPictureFromBytes("Sheet1", "A1", &excelize.Picture{Extension: ".png", File: file, Format: &excelize.GraphicOptions{AutoFit: true}, PictureInsertType: PictureInsertTypePlaceInCell}); err != nil { ...... }
Describe the results you expected:
I found out that the picture insert type is "insert over cell" with the "AddPicture" function.
I'd like to know in which way can I configure the picture's alignment type or the picture insert type as "add picture into cell".
Output of go version:

go version go1.21.6 windows/amd64

Excelize version or commit ID:

github.com/xuri/excelize/v2 v2.8.1

Environment details (OS, Microsoft Excel™ version, physical, etc.):

@xuri
Copy link
Member

xuri commented Jul 31, 2024

Thanks for your issue. This library support gets the cell images inserted by IMAGE formula function by PR #1857, the PictureInsertType data type introduced by this PR, also reference commit 5dc22e8. You need to upgrade to the master branch code via command go get -u github.com/xuri/excelize/v2@master, this feature will be released in the next version, but as the documentation of the AddPicture function says: this function only supports adding pictures placed over the cells currently, and doesn't support adding pictures placed in cells or creating the Kingsoft WPS Office embedded image cells, so you can't change the picture place as in cells by PictureInsertType, and the alignment style also not use for image, which just take effect for text or rich-text in the cell. Currently, you can specify a value in OffsetX field to change image alignment with cells, for example:

f.AddPicture("Sheet1", "A1", "image.png",
    &excelize.GraphicOptions{
        AutoFit:         true,
        LockAspectRatio: true,
+       OffsetX:         10,
})

You can calculate offset value by merged cell range width and image width, the width of merged cell range can be got by cumulate width of each column width, get each column width by GetColWidth function. I'll consider adding support for adding pictures placed in cells. This feature will be like issue #1873, so I'll close this, and track the feature on that issue. If you have any questions, please let me know, and you can reopen this anytime. Contributions are welcome. I'll certainly accept that patch if somebody did that.

@xuri xuri closed this as completed Jul 31, 2024
@Kevin8eace
Copy link
Author

Appreciate for the information that will have PictureInsertType in the next version.
And also thanks for the tips for using the OffsetX argument when using AddPicture. But I found the result is weird, and I don't know if it's because it's inserted into a merged cell. When tried to use OffsetX with a calculated number for center alignment purpose, the picture ended up being squeezed. (Snapshot pictures are as below: No OffsetX and With OffsetX)
NO_OFFSETX
WITH_OFFSETX

@xuri
Copy link
Member

xuri commented Jul 31, 2024

Could you show us a complete, standalone example program or reproducible demo? Please provide the image file attachment if you can.

@Kevin8eace
Copy link
Author

Kevin8eace commented Aug 1, 2024

Here's the code:

package main

import (
	"fmt"
	_ "image/png"
	excelize "github.com/xuri/excelize/v2"
)

func main() {
	f := excelize.NewFile()

	// Set Column Width
	if err := f.SetColWidth("Sheet1", "A", "G", 20); err != nil {
		fmt.Println(err)
	}
	// Merge Cells
	if err := f.MergeCell("Sheet1", "A1", "C6"); err != nil {
		fmt.Println(err)
	}
	// Add Picture
	if err := f.AddPicture("Sheet1", "A1", "IMAGE.png", &excelize.GraphicOptions{
		AutoFit:         true,
		LockAspectRatio: true,
		OffsetX:         90,
	}); err != nil {
		fmt.Println(err)
	}
	// Save File
	if err := f.SaveAs("Book1.xlsx"); err != nil {
		fmt.Println(err)
	}
}

And the picture I inserted:
IMAGE

@xuri
Copy link
Member

xuri commented Aug 2, 2024

Thanks for your feedback. I think insert to the middle column of the merged cells column, and change image scale in your case would be more works, it should be like this:

f.AddPicture("Sheet1", "B1", "IMAGE.png", &excelize.GraphicOptions{
    OffsetX: 5,
    ScaleX:  0.135,
    ScaleY:  0.135,
})

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

No branches or pull requests

2 participants