Skip to content

Commit 5e500f5

Browse files
authored
Introduce new exported PictureInsertType enumeration (#1864)
1 parent 838232f commit 5e500f5

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

picture.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ import (
2323
"strings"
2424
)
2525

26+
// PictureInsertType defines the type of the picture has been inserted into the
27+
// worksheet.
28+
type PictureInsertType int
29+
30+
// Insert picture types.
31+
const (
32+
PictureInsertTypePlaceOverCells PictureInsertType = iota
33+
PictureInsertTypePlaceInCell
34+
PictureInsertTypeDISPIMG
35+
)
36+
2637
// parseGraphicOptions provides a function to parse the format settings of
2738
// the picture with default value.
2839
func parseGraphicOptions(opts *GraphicOptions) *GraphicOptions {
@@ -52,7 +63,10 @@ func parseGraphicOptions(opts *GraphicOptions) *GraphicOptions {
5263
// AddPicture provides the method to add picture in a sheet by given picture
5364
// format set (such as offset, scale, aspect ratio setting and print settings)
5465
// and file path, supported image types: BMP, EMF, EMZ, GIF, JPEG, JPG, PNG,
55-
// SVG, TIF, TIFF, WMF, and WMZ. This function is concurrency safe. For example:
66+
// SVG, TIF, TIFF, WMF, and WMZ. This function is concurrency-safe. Note that
67+
// this function only supports adding pictures placed over the cells currently,
68+
// and doesn't support adding pictures placed in cells or creating the Kingsoft
69+
// WPS Office embedded image cells. For example:
5670
//
5771
// package main
5872
//
@@ -167,8 +181,10 @@ func (f *File) AddPicture(sheet, cell, name string, opts *GraphicOptions) error
167181
// AddPictureFromBytes provides the method to add picture in a sheet by given
168182
// picture format set (such as offset, scale, aspect ratio setting and print
169183
// settings), file base name, extension name and file bytes, supported image
170-
// types: EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ. For
171-
// example:
184+
// types: EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ. Note that
185+
// this function only supports adding pictures placed over the cells currently,
186+
// and doesn't support adding pictures placed in cells or creating the Kingsoft
187+
// WPS Office embedded image cells.For example:
172188
//
173189
// package main
174190
//
@@ -211,6 +227,9 @@ func (f *File) AddPictureFromBytes(sheet, cell string, pic *Picture) error {
211227
if !ok {
212228
return ErrImgExt
213229
}
230+
if pic.InsertType != PictureInsertTypePlaceOverCells {
231+
return ErrParameterInvalid
232+
}
214233
options := parseGraphicOptions(pic.Format)
215234
img, _, err := image.DecodeConfig(bytes.NewReader(pic.File))
216235
if err != nil {
@@ -577,15 +596,15 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string)
577596
cond := func(from *xlsxFrom) bool { return from.Col == col && from.Row == row }
578597
cond2 := func(from *decodeFrom) bool { return from.Col == col && from.Row == row }
579598
cb := func(a *xdrCellAnchor, r *xlsxRelationship) {
580-
pic := Picture{Extension: filepath.Ext(r.Target), Format: &GraphicOptions{}}
599+
pic := Picture{Extension: filepath.Ext(r.Target), Format: &GraphicOptions{}, InsertType: PictureInsertTypePlaceOverCells}
581600
if buffer, _ := f.Pkg.Load(strings.ReplaceAll(r.Target, "..", "xl")); buffer != nil {
582601
pic.File = buffer.([]byte)
583602
pic.Format.AltText = a.Pic.NvPicPr.CNvPr.Descr
584603
pics = append(pics, pic)
585604
}
586605
}
587606
cb2 := func(a *decodeCellAnchor, r *xlsxRelationship) {
588-
pic := Picture{Extension: filepath.Ext(r.Target), Format: &GraphicOptions{}}
607+
pic := Picture{Extension: filepath.Ext(r.Target), Format: &GraphicOptions{}, InsertType: PictureInsertTypePlaceOverCells}
589608
if buffer, _ := f.Pkg.Load(strings.ReplaceAll(r.Target, "..", "xl")); buffer != nil {
590609
pic.File = buffer.([]byte)
591610
pic.Format.AltText = a.Pic.NvPicPr.CNvPr.Descr
@@ -845,7 +864,7 @@ func (f *File) getCellImages(sheet, cell string) ([]Picture, error) {
845864
if err != nil || r == nil {
846865
return "", true, err
847866
}
848-
pic := Picture{Extension: filepath.Ext(r.Target), Format: &GraphicOptions{}}
867+
pic := Picture{Extension: filepath.Ext(r.Target), Format: &GraphicOptions{}, InsertType: PictureInsertTypePlaceInCell}
849868
if buffer, _ := f.Pkg.Load(strings.TrimPrefix(strings.ReplaceAll(r.Target, "..", "xl"), "/")); buffer != nil {
850869
pic.File = buffer.([]byte)
851870
pics = append(pics, pic)
@@ -882,7 +901,7 @@ func (f *File) getDispImages(sheet, cell string) ([]Picture, error) {
882901
if cellImg.Pic.NvPicPr.CNvPr.Name == imgID {
883902
for _, r := range rels.Relationships {
884903
if r.ID == cellImg.Pic.BlipFill.Blip.Embed {
885-
pic := Picture{Extension: filepath.Ext(r.Target), Format: &GraphicOptions{}}
904+
pic := Picture{Extension: filepath.Ext(r.Target), Format: &GraphicOptions{}, InsertType: PictureInsertTypeDISPIMG}
886905
if buffer, _ := f.Pkg.Load("xl/" + r.Target); buffer != nil {
887906
pic.File = buffer.([]byte)
888907
pic.Format.AltText = cellImg.Pic.NvPicPr.CNvPr.Descr

picture_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ import (
1212
"strings"
1313
"testing"
1414

15+
"github.com/stretchr/testify/assert"
1516
_ "golang.org/x/image/bmp"
1617
_ "golang.org/x/image/tiff"
17-
18-
"github.com/stretchr/testify/assert"
1918
)
2019

2120
func BenchmarkAddPictureFromBytes(b *testing.B) {
@@ -59,6 +58,8 @@ func TestAddPicture(t *testing.T) {
5958

6059
// Test add picture to worksheet from bytes
6160
assert.NoError(t, f.AddPictureFromBytes("Sheet1", "Q1", &Picture{Extension: ".png", File: file, Format: &GraphicOptions{AltText: "Excel Logo"}}))
61+
// Test add picture to worksheet from bytes with unsupported insert type
62+
assert.Equal(t, ErrParameterInvalid, f.AddPictureFromBytes("Sheet1", "Q1", &Picture{Extension: ".png", File: file, Format: &GraphicOptions{AltText: "Excel Logo"}, InsertType: PictureInsertTypePlaceInCell}))
6263
// Test add picture to worksheet from bytes with illegal cell reference
6364
assert.Equal(t, newCellNameToCoordinatesError("A", newInvalidCellNameError("A")), f.AddPictureFromBytes("Sheet1", "A", &Picture{Extension: ".png", File: file, Format: &GraphicOptions{AltText: "Excel Logo"}}))
6465

@@ -150,6 +151,7 @@ func TestGetPicture(t *testing.T) {
150151
assert.NoError(t, err)
151152
assert.Len(t, pics[0].File, 13233)
152153
assert.Empty(t, pics[0].Format.AltText)
154+
assert.Equal(t, PictureInsertTypePlaceOverCells, pics[0].InsertType)
153155

154156
f, err = prepareTestBook1()
155157
if !assert.NoError(t, err) {
@@ -249,6 +251,7 @@ func TestGetPicture(t *testing.T) {
249251
assert.NoError(t, err)
250252
assert.Len(t, pics, 2)
251253
assert.Equal(t, "CellImage1", pics[0].Format.AltText)
254+
assert.Equal(t, PictureInsertTypeDISPIMG, pics[0].InsertType)
252255

253256
// Test get embedded cell pictures with invalid formula
254257
assert.NoError(t, f.SetCellFormula("Sheet1", "A1", "=_xlfn.DISPIMG()"))
@@ -463,6 +466,7 @@ func TestGetCellImages(t *testing.T) {
463466
pics, err := f.GetPictures("Sheet1", "A1")
464467
assert.NoError(t, err)
465468
assert.Equal(t, 1, len(pics))
469+
assert.Equal(t, PictureInsertTypePlaceInCell, pics[0].InsertType)
466470
cells, err := f.GetPictureCells("Sheet1")
467471
assert.NoError(t, err)
468472
assert.Equal(t, []string{"A1"}, cells)

xmlDrawing.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,10 @@ type xdrTxBody struct {
409409

410410
// Picture maps the format settings of the picture.
411411
type Picture struct {
412-
Extension string
413-
File []byte
414-
Format *GraphicOptions
412+
Extension string
413+
File []byte
414+
Format *GraphicOptions
415+
InsertType PictureInsertType
415416
}
416417

417418
// GraphicOptions directly maps the format settings of the picture.

0 commit comments

Comments
 (0)