Add new AutoFitIgnoreAspect field in the GraphicOptions data type (#1923)

- Support fill the cell with the image and ignore its aspect ratio
- Update the unit tests
This commit is contained in:
wangsongyan 2024-06-19 20:45:25 +08:00 committed by GitHub
parent 1a99dd4a23
commit f04aa8dd31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 14 deletions

View File

@ -140,6 +140,10 @@ func parseGraphicOptions(opts *GraphicOptions) *GraphicOptions {
// The optional parameter "AutoFit" specifies if you make graph object size // The optional parameter "AutoFit" specifies if you make graph object size
// auto-fits the cell, the default value of that is 'false'. // 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 // The optional parameter "OffsetX" specifies the horizontal offset of the graph
// object with the cell, the default value of that is 0. // 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 asp := float64(cellHeight) / height
height, width = float64(cellHeight), width*asp 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) width, height = width-float64(opts.OffsetX), height-float64(opts.OffsetY)
w, h = int(width*opts.ScaleX), int(height*opts.ScaleY) w, h = int(width*opts.ScaleX), int(height*opts.ScaleY)
return return

View File

@ -48,6 +48,7 @@ func TestAddPicture(t *testing.T) {
// Test add picture to worksheet with autofit // 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", "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", "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") _, err = f.NewSheet("AddPicture")
assert.NoError(t, err) assert.NoError(t, err)
assert.NoError(t, f.SetRowHeight("AddPicture", 10, 30)) assert.NoError(t, f.SetRowHeight("AddPicture", 10, 30))
@ -82,7 +83,7 @@ func TestAddPicture(t *testing.T) {
// Test get picture cells // Test get picture cells
cells, err := f.GetPictureCells("Sheet1") cells, err := f.GetPictureCells("Sheet1")
assert.NoError(t, err) 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()) assert.NoError(t, f.Close())
f, err = OpenFile(filepath.Join("test", "TestAddPicture1.xlsx")) f, err = OpenFile(filepath.Join("test", "TestAddPicture1.xlsx"))
@ -91,7 +92,7 @@ func TestAddPicture(t *testing.T) {
f.Drawings.Delete(path) f.Drawings.Delete(path)
cells, err = f.GetPictureCells("Sheet1") cells, err = f.GetPictureCells("Sheet1")
assert.NoError(t, err) 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 // Test get picture cells with unsupported charset
f.Drawings.Delete(path) f.Drawings.Delete(path)
f.Pkg.Store(path, MacintoshCyrillicCharset) f.Pkg.Store(path, MacintoshCyrillicCharset)

View File

@ -417,18 +417,19 @@ type Picture struct {
// GraphicOptions directly maps the format settings of the picture. // GraphicOptions directly maps the format settings of the picture.
type GraphicOptions struct { type GraphicOptions struct {
AltText string AltText string
PrintObject *bool PrintObject *bool
Locked *bool Locked *bool
LockAspectRatio bool LockAspectRatio bool
AutoFit bool AutoFit bool
OffsetX int AutoFitIgnoreAspect bool
OffsetY int OffsetX int
ScaleX float64 OffsetY int
ScaleY float64 ScaleX float64
Hyperlink string ScaleY float64
HyperlinkType string Hyperlink string
Positioning string HyperlinkType string
Positioning string
} }
// Shape directly maps the format settings of the shape. // Shape directly maps the format settings of the shape.