Skip to content

Commit

Permalink
Add new AutoFitIgnoreAspect field in the GraphicOptions data type (#1923
Browse files Browse the repository at this point in the history
)

- Support fill the cell with the image and ignore its aspect ratio
- Update the unit tests
  • Loading branch information
wangsongyan committed Jun 19, 2024
1 parent 1a99dd4 commit f04aa8d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
7 changes: 7 additions & 0 deletions picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func parseGraphicOptions(opts *GraphicOptions) *GraphicOptions {
// The optional parameter "AutoFit" specifies if you make graph object size
// auto-fits the cell, the default value of that is 'false'.
//
// The optional parameter "AutoFitIgnoreAspect" specifies if fill the cell with
// the image and ignore its aspect ratio, the default value of that is 'false'.
// This option only works when the "AutoFit" is enabled.
//
// The optional parameter "OffsetX" specifies the horizontal offset of the graph
// object with the cell, the default value of that is 0.
//
Expand Down Expand Up @@ -735,6 +739,9 @@ func (f *File) drawingResize(sheet, cell string, width, height float64, opts *Gr
asp := float64(cellHeight) / height
height, width = float64(cellHeight), width*asp
}
if opts.AutoFitIgnoreAspect {
width, height = float64(cellWidth), float64(cellHeight)
}
width, height = width-float64(opts.OffsetX), height-float64(opts.OffsetY)
w, h = int(width*opts.ScaleX), int(height*opts.ScaleY)
return
Expand Down
5 changes: 3 additions & 2 deletions picture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func TestAddPicture(t *testing.T) {
// Test add picture to worksheet with autofit
assert.NoError(t, f.AddPicture("Sheet1", "A30", filepath.Join("test", "images", "excel.jpg"), &GraphicOptions{AutoFit: true}))
assert.NoError(t, f.AddPicture("Sheet1", "B30", filepath.Join("test", "images", "excel.jpg"), &GraphicOptions{OffsetX: 10, OffsetY: 10, AutoFit: true}))
assert.NoError(t, f.AddPicture("Sheet1", "C30", filepath.Join("test", "images", "excel.jpg"), &GraphicOptions{AutoFit: true, AutoFitIgnoreAspect: true}))
_, err = f.NewSheet("AddPicture")
assert.NoError(t, err)
assert.NoError(t, f.SetRowHeight("AddPicture", 10, 30))
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestAddPicture(t *testing.T) {
// Test get picture cells
cells, err := f.GetPictureCells("Sheet1")
assert.NoError(t, err)
assert.Equal(t, []string{"F21", "A30", "B30", "Q1", "Q8", "Q15", "Q22", "Q28"}, cells)
assert.Equal(t, []string{"F21", "A30", "B30", "C30", "Q1", "Q8", "Q15", "Q22", "Q28"}, cells)
assert.NoError(t, f.Close())

f, err = OpenFile(filepath.Join("test", "TestAddPicture1.xlsx"))
Expand All @@ -91,7 +92,7 @@ func TestAddPicture(t *testing.T) {
f.Drawings.Delete(path)
cells, err = f.GetPictureCells("Sheet1")
assert.NoError(t, err)
assert.Equal(t, []string{"F21", "A30", "B30", "Q1", "Q8", "Q15", "Q22", "Q28"}, cells)
assert.Equal(t, []string{"F21", "A30", "B30", "C30", "Q1", "Q8", "Q15", "Q22", "Q28"}, cells)
// Test get picture cells with unsupported charset
f.Drawings.Delete(path)
f.Pkg.Store(path, MacintoshCyrillicCharset)
Expand Down
25 changes: 13 additions & 12 deletions xmlDrawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,19 @@ type Picture struct {

// GraphicOptions directly maps the format settings of the picture.
type GraphicOptions struct {
AltText string
PrintObject *bool
Locked *bool
LockAspectRatio bool
AutoFit bool
OffsetX int
OffsetY int
ScaleX float64
ScaleY float64
Hyperlink string
HyperlinkType string
Positioning string
AltText string
PrintObject *bool
Locked *bool
LockAspectRatio bool
AutoFit bool
AutoFitIgnoreAspect bool
OffsetX int
OffsetY int
ScaleX float64
ScaleY float64
Hyperlink string
HyperlinkType string
Positioning string
}

// Shape directly maps the format settings of the shape.
Expand Down

0 comments on commit f04aa8d

Please sign in to comment.