From 5e0953d7783ce65707fa89f5a773697b69e82e96 Mon Sep 17 00:00:00 2001 From: jianxinhou <51222175+jianxinhou@users.noreply.github.com> Date: Thu, 1 Dec 2022 10:44:28 +0800 Subject: [PATCH] This closes #1405, add new function SetSheetBackgroundFromBytes (#1406) Co-authored-by: houjianxin.rupert --- picture.go | 7 +++++-- sheet.go | 27 ++++++++++++++++++++++----- sheet_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/picture.go b/picture.go index b4629b0..045e2af 100644 --- a/picture.go +++ b/picture.go @@ -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 // diff --git a/sheet.go b/sheet.go index e241abd..bbf529a 100644 --- a/sheet.go +++ b/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) diff --git a/sheet_test.go b/sheet_test.go index 08c7c1a..2494cfb 100644 --- a/sheet_test.go +++ b/sheet_test.go @@ -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()) +}