diff options
author | jianxinhou <51222175+jianxinhou@users.noreply.github.com> | 2022-12-01 10:44:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-01 10:44:28 +0800 |
commit | 5e0953d7783ce65707fa89f5a773697b69e82e96 (patch) | |
tree | 8d5ff97b90a74b0db70a316151e51f3ed7e8c5d0 | |
parent | c0713951c8d95fba3a23da39bfb5c85d858d2338 (diff) |
This closes #1405, add new function SetSheetBackgroundFromBytes (#1406)
Co-authored-by: houjianxin.rupert <houjianxin.rupert@bytedance.com>
-rw-r--r-- | picture.go | 7 | ||||
-rw-r--r-- | sheet.go | 27 | ||||
-rw-r--r-- | sheet_test.go | 24 |
3 files changed, 51 insertions, 7 deletions
@@ -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 // @@ -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()) +} |