Skip to content

Commit f04aa8d

Browse files
authoredJun 19, 2024
Add new AutoFitIgnoreAspect field in the GraphicOptions data type (qax-os#1923)
- Support fill the cell with the image and ignore its aspect ratio - Update the unit tests
1 parent 1a99dd4 commit f04aa8d

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed
 

‎picture.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ func parseGraphicOptions(opts *GraphicOptions) *GraphicOptions {
140140
// The optional parameter "AutoFit" specifies if you make graph object size
141141
// auto-fits the cell, the default value of that is 'false'.
142142
//
143+
// The optional parameter "AutoFitIgnoreAspect" specifies if fill the cell with
144+
// the image and ignore its aspect ratio, the default value of that is 'false'.
145+
// This option only works when the "AutoFit" is enabled.
146+
//
143147
// The optional parameter "OffsetX" specifies the horizontal offset of the graph
144148
// object with the cell, the default value of that is 0.
145149
//
@@ -735,6 +739,9 @@ func (f *File) drawingResize(sheet, cell string, width, height float64, opts *Gr
735739
asp := float64(cellHeight) / height
736740
height, width = float64(cellHeight), width*asp
737741
}
742+
if opts.AutoFitIgnoreAspect {
743+
width, height = float64(cellWidth), float64(cellHeight)
744+
}
738745
width, height = width-float64(opts.OffsetX), height-float64(opts.OffsetY)
739746
w, h = int(width*opts.ScaleX), int(height*opts.ScaleY)
740747
return

‎picture_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func TestAddPicture(t *testing.T) {
4848
// Test add picture to worksheet with autofit
4949
assert.NoError(t, f.AddPicture("Sheet1", "A30", filepath.Join("test", "images", "excel.jpg"), &GraphicOptions{AutoFit: true}))
5050
assert.NoError(t, f.AddPicture("Sheet1", "B30", filepath.Join("test", "images", "excel.jpg"), &GraphicOptions{OffsetX: 10, OffsetY: 10, AutoFit: true}))
51+
assert.NoError(t, f.AddPicture("Sheet1", "C30", filepath.Join("test", "images", "excel.jpg"), &GraphicOptions{AutoFit: true, AutoFitIgnoreAspect: true}))
5152
_, err = f.NewSheet("AddPicture")
5253
assert.NoError(t, err)
5354
assert.NoError(t, f.SetRowHeight("AddPicture", 10, 30))
@@ -82,7 +83,7 @@ func TestAddPicture(t *testing.T) {
8283
// Test get picture cells
8384
cells, err := f.GetPictureCells("Sheet1")
8485
assert.NoError(t, err)
85-
assert.Equal(t, []string{"F21", "A30", "B30", "Q1", "Q8", "Q15", "Q22", "Q28"}, cells)
86+
assert.Equal(t, []string{"F21", "A30", "B30", "C30", "Q1", "Q8", "Q15", "Q22", "Q28"}, cells)
8687
assert.NoError(t, f.Close())
8788

8889
f, err = OpenFile(filepath.Join("test", "TestAddPicture1.xlsx"))
@@ -91,7 +92,7 @@ func TestAddPicture(t *testing.T) {
9192
f.Drawings.Delete(path)
9293
cells, err = f.GetPictureCells("Sheet1")
9394
assert.NoError(t, err)
94-
assert.Equal(t, []string{"F21", "A30", "B30", "Q1", "Q8", "Q15", "Q22", "Q28"}, cells)
95+
assert.Equal(t, []string{"F21", "A30", "B30", "C30", "Q1", "Q8", "Q15", "Q22", "Q28"}, cells)
9596
// Test get picture cells with unsupported charset
9697
f.Drawings.Delete(path)
9798
f.Pkg.Store(path, MacintoshCyrillicCharset)

‎xmlDrawing.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -417,18 +417,19 @@ type Picture struct {
417417

418418
// GraphicOptions directly maps the format settings of the picture.
419419
type GraphicOptions struct {
420-
AltText string
421-
PrintObject *bool
422-
Locked *bool
423-
LockAspectRatio bool
424-
AutoFit bool
425-
OffsetX int
426-
OffsetY int
427-
ScaleX float64
428-
ScaleY float64
429-
Hyperlink string
430-
HyperlinkType string
431-
Positioning string
420+
AltText string
421+
PrintObject *bool
422+
Locked *bool
423+
LockAspectRatio bool
424+
AutoFit bool
425+
AutoFitIgnoreAspect bool
426+
OffsetX int
427+
OffsetY int
428+
ScaleX float64
429+
ScaleY float64
430+
Hyperlink string
431+
HyperlinkType string
432+
Positioning string
432433
}
433434

434435
// Shape directly maps the format settings of the shape.

0 commit comments

Comments
 (0)