Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
Excelize
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Operations
Operations
Metrics
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
Globars Forks
Excelize
Commits
98221a33
Unverified
Commit
98221a33
authored
May 17, 2020
by
xuri
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Merge pull request #410
parent
9b7d8463
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
75 additions
and
3 deletions
+75
-3
picture.go
picture.go
+51
-2
picture_test.go
picture_test.go
+21
-0
rows.go
rows.go
+2
-1
xmlDrawing.go
xmlDrawing.go
+1
-0
No files found.
picture.go
View file @
98221a33
...
...
@@ -32,6 +32,7 @@ func parseFormatPictureSet(formatSet string) (*formatPicture, error) {
FPrintsWithSheet
:
true
,
FLocksWithSheet
:
false
,
NoChangeAspect
:
false
,
Autofit
:
false
,
OffsetX
:
0
,
OffsetY
:
0
,
XScale
:
1.0
,
...
...
@@ -244,8 +245,12 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
if
err
!=
nil
{
return
err
}
width
=
int
(
float64
(
width
)
*
formatSet
.
XScale
)
height
=
int
(
float64
(
height
)
*
formatSet
.
YScale
)
if
formatSet
.
Autofit
{
width
,
height
,
col
,
row
,
err
=
f
.
drawingResize
(
sheet
,
cell
,
float64
(
width
),
float64
(
height
),
formatSet
)
if
err
!=
nil
{
return
err
}
}
col
--
row
--
colStart
,
rowStart
,
_
,
_
,
colEnd
,
rowEnd
,
x2
,
y2
:=
...
...
@@ -578,3 +583,47 @@ func (f *File) drawingsWriter() {
}
}
}
// drawingResize calculate the height and width after resizing.
func
(
f
*
File
)
drawingResize
(
sheet
string
,
cell
string
,
width
,
height
float64
,
formatSet
*
formatPicture
)
(
w
,
h
,
c
,
r
int
,
err
error
)
{
var
mergeCells
[]
MergeCell
mergeCells
,
err
=
f
.
GetMergeCells
(
sheet
)
if
err
!=
nil
{
return
}
var
rng
[]
int
var
inMergeCell
bool
if
c
,
r
,
err
=
CellNameToCoordinates
(
cell
);
err
!=
nil
{
return
}
cellWidth
,
cellHeight
:=
f
.
getColWidth
(
sheet
,
c
),
f
.
getRowHeight
(
sheet
,
r
)
for
_
,
mergeCell
:=
range
mergeCells
{
if
inMergeCell
,
err
=
f
.
checkCellInArea
(
cell
,
mergeCell
[
0
]);
err
!=
nil
{
return
}
if
inMergeCell
{
rng
,
_
=
areaRangeToCoordinates
(
mergeCell
.
GetStartAxis
(),
mergeCell
.
GetEndAxis
())
sortCoordinates
(
rng
)
}
}
if
inMergeCell
{
c
,
r
=
rng
[
0
],
rng
[
1
]
for
col
:=
rng
[
0
]
-
1
;
col
<
rng
[
2
];
col
++
{
cellWidth
+=
f
.
getColWidth
(
sheet
,
col
)
}
for
row
:=
rng
[
1
]
-
1
;
row
<
rng
[
3
];
row
++
{
cellHeight
+=
f
.
getRowHeight
(
sheet
,
row
)
}
}
if
float64
(
cellWidth
)
<
width
{
asp
:=
float64
(
cellWidth
)
/
width
width
,
height
=
float64
(
cellWidth
),
height
*
asp
}
if
float64
(
cellHeight
)
<
height
{
asp
:=
float64
(
cellHeight
)
/
height
height
,
width
=
float64
(
cellHeight
),
width
*
asp
}
width
,
height
=
width
-
float64
(
formatSet
.
OffsetX
),
height
-
float64
(
formatSet
.
OffsetY
)
w
,
h
=
int
(
width
*
formatSet
.
XScale
),
int
(
height
*
formatSet
.
YScale
)
return
}
picture_test.go
View file @
98221a33
...
...
@@ -47,6 +47,15 @@ func TestAddPicture(t *testing.T) {
file
,
err
:=
ioutil
.
ReadFile
(
filepath
.
Join
(
"test"
,
"images"
,
"excel.png"
))
assert
.
NoError
(
t
,
err
)
// Test add picture to worksheet with autofit.
assert
.
NoError
(
t
,
f
.
AddPicture
(
"Sheet1"
,
"A30"
,
filepath
.
Join
(
"test"
,
"images"
,
"excel.jpg"
),
`{"autofit": true}`
))
assert
.
NoError
(
t
,
f
.
AddPicture
(
"Sheet1"
,
"B30"
,
filepath
.
Join
(
"test"
,
"images"
,
"excel.jpg"
),
`{"x_offset": 10, "y_offset": 10, "autofit": true}`
))
f
.
NewSheet
(
"AddPicture"
)
assert
.
NoError
(
t
,
f
.
SetRowHeight
(
"AddPicture"
,
10
,
30
))
assert
.
NoError
(
t
,
f
.
MergeCell
(
"AddPicture"
,
"B3"
,
"D9"
))
assert
.
NoError
(
t
,
f
.
AddPicture
(
"AddPicture"
,
"C6"
,
filepath
.
Join
(
"test"
,
"images"
,
"excel.jpg"
),
`{"autofit": true}`
))
assert
.
NoError
(
t
,
f
.
AddPicture
(
"AddPicture"
,
"A1"
,
filepath
.
Join
(
"test"
,
"images"
,
"excel.jpg"
),
`{"autofit": true}`
))
// Test add picture to worksheet from bytes.
assert
.
NoError
(
t
,
f
.
AddPictureFromBytes
(
"Sheet1"
,
"Q1"
,
""
,
"Excel Logo"
,
".png"
,
file
))
// Test add picture to worksheet from bytes with illegal cell coordinates.
...
...
@@ -181,3 +190,15 @@ func TestDeletePicture(t *testing.T) {
// Test delete picture on no chart worksheet.
assert
.
NoError
(
t
,
NewFile
()
.
DeletePicture
(
"Sheet1"
,
"A1"
))
}
func
TestDrawingResize
(
t
*
testing
.
T
)
{
f
:=
NewFile
()
// Test calculate drawing resize on not exists worksheet.
_
,
_
,
_
,
_
,
err
:=
f
.
drawingResize
(
"SheetN"
,
"A1"
,
1
,
1
,
nil
)
assert
.
EqualError
(
t
,
err
,
"sheet SheetN is not exist"
)
// Test calculate drawing resize with invalid coordinates.
_
,
_
,
_
,
_
,
err
=
f
.
drawingResize
(
"Sheet1"
,
""
,
1
,
1
,
nil
)
assert
.
EqualError
(
t
,
err
,
`cannot convert cell "" to coordinates: invalid cell name ""`
)
f
.
Sheet
[
"xl/worksheets/sheet1.xml"
]
.
MergeCells
=
&
xlsxMergeCells
{
Cells
:
[]
*
xlsxMergeCell
{{
Ref
:
"A:A"
}}}
assert
.
EqualError
(
t
,
f
.
AddPicture
(
"Sheet1"
,
"A1"
,
filepath
.
Join
(
"test"
,
"images"
,
"excel.jpg"
),
`{"autofit": true}`
),
`cannot convert cell "A" to coordinates: invalid cell name "A"`
)
}
rows.go
View file @
98221a33
...
...
@@ -148,7 +148,8 @@ func (err ErrSheetNotExist) Error() string {
return
fmt
.
Sprintf
(
"sheet %s is not exist"
,
string
(
err
.
SheetName
))
}
// Rows return a rows iterator. For example:
// Rows returns a rows iterator, used for streaming reading data for a
// worksheet with a large data. For example:
//
// rows, err := f.Rows("Sheet1")
// if err != nil {
...
...
xmlDrawing.go
View file @
98221a33
...
...
@@ -419,6 +419,7 @@ type formatPicture struct {
FPrintsWithSheet
bool
`json:"print_obj"`
FLocksWithSheet
bool
`json:"locked"`
NoChangeAspect
bool
`json:"lock_aspect_ratio"`
Autofit
bool
`json:"autofit"`
OffsetX
int
`json:"x_offset"`
OffsetY
int
`json:"y_offset"`
XScale
float64
`json:"x_scale"`
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment