Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1405, add new function SetSheetBackgroundFromBytes #1406

Merged
merged 4 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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
//
Expand Down
27 changes: 22 additions & 5 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package excelize
import (
"encoding/xml"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -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())
}