2019-03-23 16:09:48 +08:00
package excelize
import (
2019-06-27 21:58:14 +08:00
_ "image/gif"
_ "image/jpeg"
2019-03-23 16:09:48 +08:00
_ "image/png"
2019-06-27 21:58:14 +08:00
_ "golang.org/x/image/tiff"
"fmt"
2019-03-23 16:09:48 +08:00
"io/ioutil"
2019-03-24 13:08:32 +08:00
"os"
2019-03-23 20:08:06 +08:00
"path/filepath"
2019-03-24 09:07:57 +08:00
"strings"
2019-03-23 16:09:48 +08:00
"testing"
2019-03-24 13:08:32 +08:00
"github.com/stretchr/testify/assert"
2019-03-23 16:09:48 +08:00
)
func BenchmarkAddPictureFromBytes ( b * testing . B ) {
f := NewFile ( )
2019-03-23 20:08:06 +08:00
imgFile , err := ioutil . ReadFile ( filepath . Join ( "test" , "images" , "excel.png" ) )
2019-03-23 16:09:48 +08:00
if err != nil {
2019-03-23 20:08:06 +08:00
b . Error ( "unable to load image for benchmark" )
2019-03-23 16:09:48 +08:00
}
b . ResetTimer ( )
for i := 1 ; i <= b . N ; i ++ {
2019-12-24 01:09:28 +08:00
if err := f . AddPictureFromBytes ( "Sheet1" , fmt . Sprint ( "A" , i ) , "" , "excel" , ".png" , imgFile ) ; err != nil {
b . Error ( err )
}
2019-03-23 16:09:48 +08:00
}
}
2019-03-24 13:08:32 +08:00
func TestAddPicture ( t * testing . T ) {
2019-06-27 21:58:14 +08:00
f , err := OpenFile ( filepath . Join ( "test" , "Book1.xlsx" ) )
2019-03-24 13:08:32 +08:00
if ! assert . NoError ( t , err ) {
t . FailNow ( )
}
// Test add picture to worksheet with offset and location hyperlink.
2019-12-24 01:09:28 +08:00
assert . NoError ( t , f . AddPicture ( "Sheet2" , "I9" , filepath . Join ( "test" , "images" , "excel.jpg" ) ,
` { "x_offset": 140, "y_offset": 120, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"} ` ) )
2019-03-24 13:08:32 +08:00
// Test add picture to worksheet with offset, external hyperlink and positioning.
2019-12-24 01:09:28 +08:00
assert . NoError ( t , f . AddPicture ( "Sheet1" , "F21" , filepath . Join ( "test" , "images" , "excel.jpg" ) ,
` { "x_offset": 10, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External", "positioning": "oneCell"} ` ) )
2019-03-24 13:08:32 +08:00
2019-06-27 21:58:14 +08:00
file , err := ioutil . ReadFile ( filepath . Join ( "test" , "images" , "excel.png" ) )
2019-12-24 01:09:28 +08:00
assert . NoError ( t , err )
2019-03-24 13:08:32 +08:00
2020-05-17 17:36:53 +08:00
// 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} ` ) )
2019-03-24 13:08:32 +08:00
// Test add picture to worksheet from bytes.
2019-06-27 21:58:14 +08:00
assert . NoError ( t , f . AddPictureFromBytes ( "Sheet1" , "Q1" , "" , "Excel Logo" , ".png" , file ) )
2019-03-24 13:08:32 +08:00
// Test add picture to worksheet from bytes with illegal cell coordinates.
2019-06-27 21:58:14 +08:00
assert . EqualError ( t , f . AddPictureFromBytes ( "Sheet1" , "A" , "" , "Excel Logo" , ".png" , file ) , ` cannot convert cell "A" to coordinates: invalid cell name "A" ` )
assert . NoError ( t , f . AddPicture ( "Sheet1" , "Q8" , filepath . Join ( "test" , "images" , "excel.gif" ) , "" ) )
assert . NoError ( t , f . AddPicture ( "Sheet1" , "Q15" , filepath . Join ( "test" , "images" , "excel.jpg" ) , "" ) )
assert . NoError ( t , f . AddPicture ( "Sheet1" , "Q22" , filepath . Join ( "test" , "images" , "excel.tif" ) , "" ) )
2019-03-24 13:08:32 +08:00
// Test write file to given path.
2019-06-27 21:58:14 +08:00
assert . NoError ( t , f . SaveAs ( filepath . Join ( "test" , "TestAddPicture.xlsx" ) ) )
2019-03-24 13:08:32 +08:00
}
func TestAddPictureErrors ( t * testing . T ) {
xlsx , err := OpenFile ( filepath . Join ( "test" , "Book1.xlsx" ) )
2019-12-24 01:09:28 +08:00
assert . NoError ( t , err )
2019-03-24 13:08:32 +08:00
// Test add picture to worksheet with invalid file path.
err = xlsx . AddPicture ( "Sheet1" , "G21" , filepath . Join ( "test" , "not_exists_dir" , "not_exists.icon" ) , "" )
if assert . Error ( t , err ) {
assert . True ( t , os . IsNotExist ( err ) , "Expected os.IsNotExist(err) == true" )
}
// Test add picture to worksheet with unsupport file type.
err = xlsx . AddPicture ( "Sheet1" , "G21" , filepath . Join ( "test" , "Book1.xlsx" ) , "" )
assert . EqualError ( t , err , "unsupported image extension" )
err = xlsx . AddPictureFromBytes ( "Sheet1" , "G21" , "" , "Excel Logo" , "jpg" , make ( [ ] byte , 1 ) )
assert . EqualError ( t , err , "unsupported image extension" )
// Test add picture to worksheet with invalid file data.
err = xlsx . AddPictureFromBytes ( "Sheet1" , "G21" , "" , "Excel Logo" , ".jpg" , make ( [ ] byte , 1 ) )
assert . EqualError ( t , err , "image: unknown format" )
}
func TestGetPicture ( t * testing . T ) {
2019-12-22 00:02:09 +08:00
f , err := prepareTestBook1 ( )
2019-03-24 13:08:32 +08:00
if ! assert . NoError ( t , err ) {
t . FailNow ( )
}
2019-12-22 00:02:09 +08:00
file , raw , err := f . GetPicture ( "Sheet1" , "F21" )
2019-03-24 13:08:32 +08:00
assert . NoError ( t , err )
if ! assert . NotEmpty ( t , filepath . Join ( "test" , file ) ) || ! assert . NotEmpty ( t , raw ) ||
! assert . NoError ( t , ioutil . WriteFile ( filepath . Join ( "test" , file ) , raw , 0644 ) ) {
t . FailNow ( )
}
// Try to get picture from a worksheet with illegal cell coordinates.
2019-12-22 00:02:09 +08:00
_ , _ , err = f . GetPicture ( "Sheet1" , "A" )
2019-03-24 13:08:32 +08:00
assert . EqualError ( t , err , ` cannot convert cell "A" to coordinates: invalid cell name "A" ` )
// Try to get picture from a worksheet that doesn't contain any images.
2019-12-22 00:02:09 +08:00
file , raw , err = f . GetPicture ( "Sheet3" , "I9" )
2019-04-16 10:57:21 +08:00
assert . EqualError ( t , err , "sheet Sheet3 is not exist" )
2019-03-24 13:08:32 +08:00
assert . Empty ( t , file )
assert . Empty ( t , raw )
// Try to get picture from a cell that doesn't contain an image.
2019-12-22 00:02:09 +08:00
file , raw , err = f . GetPicture ( "Sheet2" , "A2" )
2019-03-24 13:08:32 +08:00
assert . NoError ( t , err )
assert . Empty ( t , file )
assert . Empty ( t , raw )
2019-12-22 00:02:09 +08:00
f . getDrawingRelationships ( "xl/worksheets/_rels/sheet1.xml.rels" , "rId8" )
f . getDrawingRelationships ( "" , "" )
f . getSheetRelationshipsTargetByID ( "" , "" )
f . deleteSheetRelationships ( "" , "" )
2019-03-24 13:08:32 +08:00
// Try to get picture from a local storage file.
2019-12-24 01:09:28 +08:00
assert . NoError ( t , f . SaveAs ( filepath . Join ( "test" , "TestGetPicture.xlsx" ) ) )
2019-03-24 13:08:32 +08:00
2019-12-22 00:02:09 +08:00
f , err = OpenFile ( filepath . Join ( "test" , "TestGetPicture.xlsx" ) )
2019-12-24 01:09:28 +08:00
assert . NoError ( t , err )
2019-03-24 13:08:32 +08:00
2019-12-22 00:02:09 +08:00
file , raw , err = f . GetPicture ( "Sheet1" , "F21" )
2019-03-24 13:08:32 +08:00
assert . NoError ( t , err )
if ! assert . NotEmpty ( t , filepath . Join ( "test" , file ) ) || ! assert . NotEmpty ( t , raw ) ||
! assert . NoError ( t , ioutil . WriteFile ( filepath . Join ( "test" , file ) , raw , 0644 ) ) {
t . FailNow ( )
}
// Try to get picture from a local storage file that doesn't contain an image.
2019-12-22 00:02:09 +08:00
file , raw , err = f . GetPicture ( "Sheet1" , "F22" )
assert . NoError ( t , err )
assert . Empty ( t , file )
assert . Empty ( t , raw )
// Test get picture from none drawing worksheet.
f = NewFile ( )
file , raw , err = f . GetPicture ( "Sheet1" , "F22" )
2019-03-24 13:08:32 +08:00
assert . NoError ( t , err )
assert . Empty ( t , file )
assert . Empty ( t , raw )
}
func TestAddDrawingPicture ( t * testing . T ) {
// testing addDrawingPicture with illegal cell coordinates.
f := NewFile ( )
assert . EqualError ( t , f . addDrawingPicture ( "sheet1" , "" , "A" , "" , 0 , 0 , 0 , 0 , nil ) , ` cannot convert cell "A" to coordinates: invalid cell name "A" ` )
}
2019-03-24 09:07:57 +08:00
func TestAddPictureFromBytes ( t * testing . T ) {
f := NewFile ( )
imgFile , err := ioutil . ReadFile ( "logo.png" )
2019-12-22 00:02:09 +08:00
assert . NoError ( t , err , "Unable to load logo for test" )
assert . NoError ( t , f . AddPictureFromBytes ( "Sheet1" , fmt . Sprint ( "A" , 1 ) , "" , "logo" , ".png" , imgFile ) )
assert . NoError ( t , f . AddPictureFromBytes ( "Sheet1" , fmt . Sprint ( "A" , 50 ) , "" , "logo" , ".png" , imgFile ) )
2019-03-24 09:07:57 +08:00
imageCount := 0
for fileName := range f . XLSX {
if strings . Contains ( fileName , "media/image" ) {
imageCount ++
}
}
assert . Equal ( t , 1 , imageCount , "Duplicate image should only be stored once." )
2019-12-22 00:02:09 +08:00
assert . EqualError ( t , f . AddPictureFromBytes ( "SheetN" , fmt . Sprint ( "A" , 1 ) , "" , "logo" , ".png" , imgFile ) , "sheet SheetN is not exist" )
2019-03-24 09:07:57 +08:00
}
2020-01-22 01:08:18 +08:00
func TestDeletePicture ( t * testing . T ) {
f , err := OpenFile ( filepath . Join ( "test" , "Book1.xlsx" ) )
assert . NoError ( t , err )
assert . NoError ( t , f . DeletePicture ( "Sheet1" , "A1" ) )
assert . NoError ( t , f . AddPicture ( "Sheet1" , "P1" , filepath . Join ( "test" , "images" , "excel.jpg" ) , "" ) )
assert . NoError ( t , f . DeletePicture ( "Sheet1" , "P1" ) )
assert . NoError ( t , f . SaveAs ( filepath . Join ( "test" , "TestDeletePicture.xlsx" ) ) )
// Test delete picture on not exists worksheet.
assert . EqualError ( t , f . DeletePicture ( "SheetN" , "A1" ) , "sheet SheetN is not exist" )
// Test delete picture with invalid coordinates.
assert . EqualError ( t , f . DeletePicture ( "Sheet1" , "" ) , ` cannot convert cell "" to coordinates: invalid cell name "" ` )
// Test delete picture on no chart worksheet.
assert . NoError ( t , NewFile ( ) . DeletePicture ( "Sheet1" , "A1" ) )
}
2020-05-17 17:36:53 +08:00
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" ` )
}