@@ -23,6 +23,17 @@ import (
23
23
"strings"
24
24
)
25
25
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
+
26
37
// parseGraphicOptions provides a function to parse the format settings of
27
38
// the picture with default value.
28
39
func parseGraphicOptions (opts * GraphicOptions ) * GraphicOptions {
@@ -52,7 +63,10 @@ func parseGraphicOptions(opts *GraphicOptions) *GraphicOptions {
52
63
// AddPicture provides the method to add picture in a sheet by given picture
53
64
// format set (such as offset, scale, aspect ratio setting and print settings)
54
65
// 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:
56
70
//
57
71
// package main
58
72
//
@@ -167,8 +181,10 @@ func (f *File) AddPicture(sheet, cell, name string, opts *GraphicOptions) error
167
181
// AddPictureFromBytes provides the method to add picture in a sheet by given
168
182
// picture format set (such as offset, scale, aspect ratio setting and print
169
183
// 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:
172
188
//
173
189
// package main
174
190
//
@@ -211,6 +227,9 @@ func (f *File) AddPictureFromBytes(sheet, cell string, pic *Picture) error {
211
227
if ! ok {
212
228
return ErrImgExt
213
229
}
230
+ if pic .InsertType != PictureInsertTypePlaceOverCells {
231
+ return ErrParameterInvalid
232
+ }
214
233
options := parseGraphicOptions (pic .Format )
215
234
img , _ , err := image .DecodeConfig (bytes .NewReader (pic .File ))
216
235
if err != nil {
@@ -577,15 +596,15 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string)
577
596
cond := func (from * xlsxFrom ) bool { return from .Col == col && from .Row == row }
578
597
cond2 := func (from * decodeFrom ) bool { return from .Col == col && from .Row == row }
579
598
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 }
581
600
if buffer , _ := f .Pkg .Load (strings .ReplaceAll (r .Target , ".." , "xl" )); buffer != nil {
582
601
pic .File = buffer .([]byte )
583
602
pic .Format .AltText = a .Pic .NvPicPr .CNvPr .Descr
584
603
pics = append (pics , pic )
585
604
}
586
605
}
587
606
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 }
589
608
if buffer , _ := f .Pkg .Load (strings .ReplaceAll (r .Target , ".." , "xl" )); buffer != nil {
590
609
pic .File = buffer .([]byte )
591
610
pic .Format .AltText = a .Pic .NvPicPr .CNvPr .Descr
@@ -845,7 +864,7 @@ func (f *File) getCellImages(sheet, cell string) ([]Picture, error) {
845
864
if err != nil || r == nil {
846
865
return "" , true , err
847
866
}
848
- pic := Picture {Extension : filepath .Ext (r .Target ), Format : & GraphicOptions {}}
867
+ pic := Picture {Extension : filepath .Ext (r .Target ), Format : & GraphicOptions {}, InsertType : PictureInsertTypePlaceInCell }
849
868
if buffer , _ := f .Pkg .Load (strings .TrimPrefix (strings .ReplaceAll (r .Target , ".." , "xl" ), "/" )); buffer != nil {
850
869
pic .File = buffer .([]byte )
851
870
pics = append (pics , pic )
@@ -882,7 +901,7 @@ func (f *File) getDispImages(sheet, cell string) ([]Picture, error) {
882
901
if cellImg .Pic .NvPicPr .CNvPr .Name == imgID {
883
902
for _ , r := range rels .Relationships {
884
903
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 }
886
905
if buffer , _ := f .Pkg .Load ("xl/" + r .Target ); buffer != nil {
887
906
pic .File = buffer .([]byte )
888
907
pic .Format .AltText = cellImg .Pic .NvPicPr .CNvPr .Descr
0 commit comments