Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xuri
excelize
提交
5e500f5e
excelize
项目概览
xuri
/
excelize
通知
13
Star
2
Fork
4
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
excelize
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
5e500f5e
编写于
3月 27, 2024
作者:
Y
yangyile-yyle88
提交者:
GitHub
3月 27, 2024
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Introduce new exported PictureInsertType enumeration (#1864)
上级
838232fd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
36 addition
and
12 deletion
+36
-12
picture.go
picture.go
+26
-7
picture_test.go
picture_test.go
+6
-2
xmlDrawing.go
xmlDrawing.go
+4
-3
未找到文件。
picture.go
浏览文件 @
5e500f5e
...
...
@@ -23,6 +23,17 @@ import (
"strings"
)
// PictureInsertType defines the type of the picture has been inserted into the
// worksheet.
type
PictureInsertType
int
// Insert picture types.
const
(
PictureInsertTypePlaceOverCells
PictureInsertType
=
iota
PictureInsertTypePlaceInCell
PictureInsertTypeDISPIMG
)
// parseGraphicOptions provides a function to parse the format settings of
// the picture with default value.
func
parseGraphicOptions
(
opts
*
GraphicOptions
)
*
GraphicOptions
{
...
...
@@ -52,7 +63,10 @@ func parseGraphicOptions(opts *GraphicOptions) *GraphicOptions {
// AddPicture provides the method to add picture in a sheet by given picture
// format set (such as offset, scale, aspect ratio setting and print settings)
// and file path, supported image types: BMP, EMF, EMZ, GIF, JPEG, JPG, PNG,
// SVG, TIF, TIFF, WMF, and WMZ. This function is concurrency safe. For example:
// SVG, TIF, TIFF, WMF, and WMZ. This function is concurrency-safe. Note that
// this function only supports adding pictures placed over the cells currently,
// and doesn't support adding pictures placed in cells or creating the Kingsoft
// WPS Office embedded image cells. For example:
//
// package main
//
...
...
@@ -167,8 +181,10 @@ func (f *File) AddPicture(sheet, cell, name string, opts *GraphicOptions) error
// AddPictureFromBytes provides the method to add picture in a sheet by given
// picture format set (such as offset, scale, aspect ratio setting and print
// settings), file base name, extension name and file bytes, supported image
// types: EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ. For
// example:
// types: EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ. Note that
// this function only supports adding pictures placed over the cells currently,
// and doesn't support adding pictures placed in cells or creating the Kingsoft
// WPS Office embedded image cells.For example:
//
// package main
//
...
...
@@ -211,6 +227,9 @@ func (f *File) AddPictureFromBytes(sheet, cell string, pic *Picture) error {
if
!
ok
{
return
ErrImgExt
}
if
pic
.
InsertType
!=
PictureInsertTypePlaceOverCells
{
return
ErrParameterInvalid
}
options
:=
parseGraphicOptions
(
pic
.
Format
)
img
,
_
,
err
:=
image
.
DecodeConfig
(
bytes
.
NewReader
(
pic
.
File
))
if
err
!=
nil
{
...
...
@@ -577,7 +596,7 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string)
cond
:=
func
(
from
*
xlsxFrom
)
bool
{
return
from
.
Col
==
col
&&
from
.
Row
==
row
}
cond2
:=
func
(
from
*
decodeFrom
)
bool
{
return
from
.
Col
==
col
&&
from
.
Row
==
row
}
cb
:=
func
(
a
*
xdrCellAnchor
,
r
*
xlsxRelationship
)
{
pic
:=
Picture
{
Extension
:
filepath
.
Ext
(
r
.
Target
),
Format
:
&
GraphicOptions
{}}
pic
:=
Picture
{
Extension
:
filepath
.
Ext
(
r
.
Target
),
Format
:
&
GraphicOptions
{}
,
InsertType
:
PictureInsertTypePlaceOverCells
}
if
buffer
,
_
:=
f
.
Pkg
.
Load
(
strings
.
ReplaceAll
(
r
.
Target
,
".."
,
"xl"
));
buffer
!=
nil
{
pic
.
File
=
buffer
.
([]
byte
)
pic
.
Format
.
AltText
=
a
.
Pic
.
NvPicPr
.
CNvPr
.
Descr
...
...
@@ -585,7 +604,7 @@ func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string)
}
}
cb2
:=
func
(
a
*
decodeCellAnchor
,
r
*
xlsxRelationship
)
{
pic
:=
Picture
{
Extension
:
filepath
.
Ext
(
r
.
Target
),
Format
:
&
GraphicOptions
{}}
pic
:=
Picture
{
Extension
:
filepath
.
Ext
(
r
.
Target
),
Format
:
&
GraphicOptions
{}
,
InsertType
:
PictureInsertTypePlaceOverCells
}
if
buffer
,
_
:=
f
.
Pkg
.
Load
(
strings
.
ReplaceAll
(
r
.
Target
,
".."
,
"xl"
));
buffer
!=
nil
{
pic
.
File
=
buffer
.
([]
byte
)
pic
.
Format
.
AltText
=
a
.
Pic
.
NvPicPr
.
CNvPr
.
Descr
...
...
@@ -845,7 +864,7 @@ func (f *File) getCellImages(sheet, cell string) ([]Picture, error) {
if
err
!=
nil
||
r
==
nil
{
return
""
,
true
,
err
}
pic
:=
Picture
{
Extension
:
filepath
.
Ext
(
r
.
Target
),
Format
:
&
GraphicOptions
{}}
pic
:=
Picture
{
Extension
:
filepath
.
Ext
(
r
.
Target
),
Format
:
&
GraphicOptions
{}
,
InsertType
:
PictureInsertTypePlaceInCell
}
if
buffer
,
_
:=
f
.
Pkg
.
Load
(
strings
.
TrimPrefix
(
strings
.
ReplaceAll
(
r
.
Target
,
".."
,
"xl"
),
"/"
));
buffer
!=
nil
{
pic
.
File
=
buffer
.
([]
byte
)
pics
=
append
(
pics
,
pic
)
...
...
@@ -882,7 +901,7 @@ func (f *File) getDispImages(sheet, cell string) ([]Picture, error) {
if
cellImg
.
Pic
.
NvPicPr
.
CNvPr
.
Name
==
imgID
{
for
_
,
r
:=
range
rels
.
Relationships
{
if
r
.
ID
==
cellImg
.
Pic
.
BlipFill
.
Blip
.
Embed
{
pic
:=
Picture
{
Extension
:
filepath
.
Ext
(
r
.
Target
),
Format
:
&
GraphicOptions
{}}
pic
:=
Picture
{
Extension
:
filepath
.
Ext
(
r
.
Target
),
Format
:
&
GraphicOptions
{}
,
InsertType
:
PictureInsertTypeDISPIMG
}
if
buffer
,
_
:=
f
.
Pkg
.
Load
(
"xl/"
+
r
.
Target
);
buffer
!=
nil
{
pic
.
File
=
buffer
.
([]
byte
)
pic
.
Format
.
AltText
=
cellImg
.
Pic
.
NvPicPr
.
CNvPr
.
Descr
...
...
picture_test.go
浏览文件 @
5e500f5e
...
...
@@ -12,10 +12,9 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
_
"golang.org/x/image/bmp"
_
"golang.org/x/image/tiff"
"github.com/stretchr/testify/assert"
)
func
BenchmarkAddPictureFromBytes
(
b
*
testing
.
B
)
{
...
...
@@ -59,6 +58,8 @@ func TestAddPicture(t *testing.T) {
// Test add picture to worksheet from bytes
assert
.
NoError
(
t
,
f
.
AddPictureFromBytes
(
"Sheet1"
,
"Q1"
,
&
Picture
{
Extension
:
".png"
,
File
:
file
,
Format
:
&
GraphicOptions
{
AltText
:
"Excel Logo"
}}))
// Test add picture to worksheet from bytes with unsupported insert type
assert
.
Equal
(
t
,
ErrParameterInvalid
,
f
.
AddPictureFromBytes
(
"Sheet1"
,
"Q1"
,
&
Picture
{
Extension
:
".png"
,
File
:
file
,
Format
:
&
GraphicOptions
{
AltText
:
"Excel Logo"
},
InsertType
:
PictureInsertTypePlaceInCell
}))
// Test add picture to worksheet from bytes with illegal cell reference
assert
.
Equal
(
t
,
newCellNameToCoordinatesError
(
"A"
,
newInvalidCellNameError
(
"A"
)),
f
.
AddPictureFromBytes
(
"Sheet1"
,
"A"
,
&
Picture
{
Extension
:
".png"
,
File
:
file
,
Format
:
&
GraphicOptions
{
AltText
:
"Excel Logo"
}}))
...
...
@@ -150,6 +151,7 @@ func TestGetPicture(t *testing.T) {
assert
.
NoError
(
t
,
err
)
assert
.
Len
(
t
,
pics
[
0
]
.
File
,
13233
)
assert
.
Empty
(
t
,
pics
[
0
]
.
Format
.
AltText
)
assert
.
Equal
(
t
,
PictureInsertTypePlaceOverCells
,
pics
[
0
]
.
InsertType
)
f
,
err
=
prepareTestBook1
()
if
!
assert
.
NoError
(
t
,
err
)
{
...
...
@@ -249,6 +251,7 @@ func TestGetPicture(t *testing.T) {
assert
.
NoError
(
t
,
err
)
assert
.
Len
(
t
,
pics
,
2
)
assert
.
Equal
(
t
,
"CellImage1"
,
pics
[
0
]
.
Format
.
AltText
)
assert
.
Equal
(
t
,
PictureInsertTypeDISPIMG
,
pics
[
0
]
.
InsertType
)
// Test get embedded cell pictures with invalid formula
assert
.
NoError
(
t
,
f
.
SetCellFormula
(
"Sheet1"
,
"A1"
,
"=_xlfn.DISPIMG()"
))
...
...
@@ -463,6 +466,7 @@ func TestGetCellImages(t *testing.T) {
pics
,
err
:=
f
.
GetPictures
(
"Sheet1"
,
"A1"
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
1
,
len
(
pics
))
assert
.
Equal
(
t
,
PictureInsertTypePlaceInCell
,
pics
[
0
]
.
InsertType
)
cells
,
err
:=
f
.
GetPictureCells
(
"Sheet1"
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
[]
string
{
"A1"
},
cells
)
...
...
xmlDrawing.go
浏览文件 @
5e500f5e
...
...
@@ -409,9 +409,10 @@ type xdrTxBody struct {
// Picture maps the format settings of the picture.
type
Picture
struct
{
Extension
string
File
[]
byte
Format
*
GraphicOptions
Extension
string
File
[]
byte
Format
*
GraphicOptions
InsertType
PictureInsertType
}
// GraphicOptions directly maps the format settings of the picture.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录