summaryrefslogtreecommitdiff
path: root/sheet.go
diff options
context:
space:
mode:
authorjianxinhou <51222175+jianxinhou@users.noreply.github.com>2022-12-01 10:44:28 +0800
committerGitHub <noreply@github.com>2022-12-01 10:44:28 +0800
commit5e0953d7783ce65707fa89f5a773697b69e82e96 (patch)
tree8d5ff97b90a74b0db70a316151e51f3ed7e8c5d0 /sheet.go
parentc0713951c8d95fba3a23da39bfb5c85d858d2338 (diff)
This closes #1405, add new function SetSheetBackgroundFromBytes (#1406)
Co-authored-by: houjianxin.rupert <houjianxin.rupert@bytedance.com>
Diffstat (limited to 'sheet.go')
-rw-r--r--sheet.go27
1 files changed, 22 insertions, 5 deletions
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)