Co-authored-by: houjianxin.rupert <houjianxin.rupert@bytedance.com>
This commit is contained in:
parent
c0713951c8
commit
5e0953d778
|
@ -38,7 +38,8 @@ func parsePictureOptions(opts string) (*pictureOptions, error) {
|
|||
|
||||
// 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. This function is concurrency safe. For example:
|
||||
// and file path, supported image types: EMF, EMZ, GIF, JPEG, JPG, PNG, SVG,
|
||||
// TIF, TIFF, WMF, and WMZ. This function is concurrency safe. For example:
|
||||
//
|
||||
// package main
|
||||
//
|
||||
|
@ -121,7 +122,9 @@ func (f *File) AddPicture(sheet, cell, picture, format string) 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. For example:
|
||||
// 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:
|
||||
//
|
||||
// package main
|
||||
//
|
||||
|
|
27
sheet.go
27
sheet.go
|
@ -485,23 +485,40 @@ func (f *File) getSheetXMLPath(sheet string) (string, bool) {
|
|||
}
|
||||
|
||||
// SetSheetBackground provides a function to set background picture by given
|
||||
// worksheet name and file path.
|
||||
// worksheet name and file path. Supported image types: EMF, EMZ, GIF, JPEG,
|
||||
// JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ.
|
||||
func (f *File) SetSheetBackground(sheet, picture string) error {
|
||||
var err error
|
||||
// Check picture exists first.
|
||||
if _, err = os.Stat(picture); os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
ext, ok := supportedImageTypes[path.Ext(picture)]
|
||||
file, _ := os.ReadFile(filepath.Clean(picture))
|
||||
return f.setSheetBackground(sheet, path.Ext(picture), file)
|
||||
}
|
||||
|
||||
// SetSheetBackgroundFromBytes provides a function to set background picture by
|
||||
// given worksheet name, extension name and image data. Supported image types:
|
||||
// EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ.
|
||||
func (f *File) SetSheetBackgroundFromBytes(sheet, extension string, picture []byte) error {
|
||||
if len(picture) == 0 {
|
||||
return ErrParameterInvalid
|
||||
}
|
||||
return f.setSheetBackground(sheet, extension, picture)
|
||||
}
|
||||
|
||||
// setSheetBackground provides a function to set background picture by given
|
||||
// worksheet name, file name extension and image data.
|
||||
func (f *File) setSheetBackground(sheet, extension string, file []byte) error {
|
||||
imageType, ok := supportedImageTypes[extension]
|
||||
if !ok {
|
||||
return ErrImgExt
|
||||
}
|
||||
file, _ := os.ReadFile(filepath.Clean(picture))
|
||||
name := f.addMedia(file, ext)
|
||||
name := f.addMedia(file, imageType)
|
||||
sheetXMLPath, _ := f.getSheetXMLPath(sheet)
|
||||
sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetXMLPath, "xl/worksheets/") + ".rels"
|
||||
rID := f.addRels(sheetRels, SourceRelationshipImage, strings.Replace(name, "xl", "..", 1), "")
|
||||
if err = f.addSheetPicture(sheet, rID); err != nil {
|
||||
if err := f.addSheetPicture(sheet, rID); err != nil {
|
||||
return err
|
||||
}
|
||||
f.addSheetNameSpace(sheet, SourceRelationship)
|
||||
|
|
|
@ -3,6 +3,8 @@ package excelize
|
|||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -463,3 +465,25 @@ func TestAttrValToFloat(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
assert.Equal(t, 42.1, got)
|
||||
}
|
||||
|
||||
func TestSetSheetBackgroundFromBytes(t *testing.T) {
|
||||
f := NewFile()
|
||||
f.SetSheetName("Sheet1", ".svg")
|
||||
for i, imageTypes := range []string{".svg", ".emf", ".emz", ".gif", ".jpg", ".png", ".tif", ".wmf", ".wmz"} {
|
||||
file := fmt.Sprintf("excelize%s", imageTypes)
|
||||
if i > 0 {
|
||||
file = filepath.Join("test", "images", fmt.Sprintf("excel%s", imageTypes))
|
||||
f.NewSheet(imageTypes)
|
||||
}
|
||||
img, err := os.Open(file)
|
||||
assert.NoError(t, err)
|
||||
content, err := io.ReadAll(img)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, img.Close())
|
||||
assert.NoError(t, f.SetSheetBackgroundFromBytes(imageTypes, imageTypes, content))
|
||||
}
|
||||
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetSheetBackgroundFromBytes.xlsx")))
|
||||
assert.NoError(t, f.Close())
|
||||
|
||||
assert.EqualError(t, f.SetSheetBackgroundFromBytes("Sheet1", ".svg", nil), ErrParameterInvalid.Error())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue