diff --git a/picture.go b/picture.go index 7b46df8..a9a17bc 100644 --- a/picture.go +++ b/picture.go @@ -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. // @@ -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 diff --git a/picture_test.go b/picture_test.go index 23054c1..aec0b5c 100644 --- a/picture_test.go +++ b/picture_test.go @@ -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)) @@ -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")) @@ -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) diff --git a/xmlDrawing.go b/xmlDrawing.go index 7981166..f3dbf09 100644 --- a/xmlDrawing.go +++ b/xmlDrawing.go @@ -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.