summaryrefslogtreecommitdiff
path: root/picture.go
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2018-09-27 23:40:00 +0800
committerGitHub <noreply@github.com>2018-09-27 23:40:00 +0800
commit204139739a34aa6e6c5ea8764008276a6d184ff6 (patch)
tree0b9a795ea20804753134fd54c40d17c5a01c64f6 /picture.go
parent250946568ca1e5a69c07f19dff4d1d3a2264e31d (diff)
parent3e004d900b103379c2d62657a3070de4a2e8585a (diff)
Merge branch 'master' into master
Diffstat (limited to 'picture.go')
-rw-r--r--picture.go132
1 files changed, 92 insertions, 40 deletions
diff --git a/picture.go b/picture.go
index ab72fc8..8785aaf 100644
--- a/picture.go
+++ b/picture.go
@@ -1,3 +1,12 @@
+// Copyright 2016 - 2018 The excelize Authors. All rights reserved. Use of
+// this source code is governed by a BSD-style license that can be found in
+// the LICENSE file.
+//
+// Package excelize providing a set of functions that allow you to write to
+// and read from XLSX files. Support reads and writes XLSX file generated by
+// Microsoft Excelâ„¢ 2007 and later. Support save file without losing original
+// charts of XLSX. This library needs Go version 1.8 or later.
+
package excelize
import (
@@ -14,8 +23,8 @@ import (
"strings"
)
-// parseFormatPictureSet provides function to parse the format settings of the
-// picture with default value.
+// parseFormatPictureSet provides a function to parse the format settings of
+// the picture with default value.
func parseFormatPictureSet(formatSet string) (*formatPicture, error) {
format := formatPicture{
FPrintsWithSheet: true,
@@ -26,7 +35,7 @@ func parseFormatPictureSet(formatSet string) (*formatPicture, error) {
XScale: 1.0,
YScale: 1.0,
}
- err := json.Unmarshal([]byte(formatSet), &format)
+ err := json.Unmarshal(parseFormatSet(formatSet), &format)
return &format, err
}
@@ -78,23 +87,66 @@ func parseFormatPictureSet(formatSet string) (*formatPicture, error) {
// positioning is move and size with cells.
func (f *File) AddPicture(sheet, cell, picture, format string) error {
var err error
- var drawingHyperlinkRID int
- var hyperlinkType string
// Check picture exists first.
if _, err = os.Stat(picture); os.IsNotExist(err) {
return err
}
ext, ok := supportImageTypes[path.Ext(picture)]
if !ok {
- return errors.New("Unsupported image extension")
+ return errors.New("unsupported image extension")
+ }
+ file, _ := ioutil.ReadFile(picture)
+ _, name := filepath.Split(picture)
+ return f.AddPictureFromBytes(sheet, cell, format, name, ext, file)
+}
+
+// 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:
+//
+// package main
+//
+// import (
+// "fmt"
+// _ "image/jpeg"
+// "io/ioutil"
+//
+// "github.com/360EntSecGroup-Skylar/excelize"
+// )
+//
+// func main() {
+// xlsx := excelize.NewFile()
+//
+// file, err := ioutil.ReadFile("./image1.jpg")
+// if err != nil {
+// fmt.Println(err)
+// }
+// err = xlsx.AddPictureFromBytes("Sheet1", "A2", "", "Excel Logo", ".jpg", file)
+// if err != nil {
+// fmt.Println(err)
+// }
+// err = xlsx.SaveAs("./Book1.xlsx")
+// if err != nil {
+// fmt.Println(err)
+// }
+// }
+//
+func (f *File) AddPictureFromBytes(sheet, cell, format, name, extension string, file []byte) error {
+ var err error
+ var drawingHyperlinkRID int
+ var hyperlinkType string
+ ext, ok := supportImageTypes[extension]
+ if !ok {
+ return errors.New("unsupported image extension")
}
- readFile, _ := os.Open(picture)
- image, _, _ := image.DecodeConfig(readFile)
- _, file := filepath.Split(picture)
formatSet, err := parseFormatPictureSet(format)
if err != nil {
return err
}
+ image, _, err := image.DecodeConfig(bytes.NewReader(file))
+ if err != nil {
+ return err
+ }
// Read sheet data.
xlsx := f.workSheetReader(sheet)
// Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
@@ -110,13 +162,13 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error {
}
drawingHyperlinkRID = f.addDrawingRelationships(drawingID, SourceRelationshipHyperLink, formatSet.Hyperlink, hyperlinkType)
}
- f.addDrawingPicture(sheet, drawingXML, cell, file, image.Width, image.Height, drawingRID, drawingHyperlinkRID, formatSet)
- f.addMedia(picture, ext)
+ f.addDrawingPicture(sheet, drawingXML, cell, name, image.Width, image.Height, drawingRID, drawingHyperlinkRID, formatSet)
+ f.addMedia(file, ext)
f.addContentTypePart(drawingID, "drawings")
return err
}
-// addSheetRelationships provides function to add
+// addSheetRelationships provides a function to add
// xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name, relationship
// type and target.
func (f *File) addSheetRelationships(sheet, relType, target, targetMode string) int {
@@ -149,9 +201,9 @@ func (f *File) addSheetRelationships(sheet, relType, target, targetMode string)
return rID
}
-// deleteSheetRelationships provides function to delete relationships in
-// xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name and relationship
-// index.
+// deleteSheetRelationships provides a function to delete relationships in
+// xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name and
+// relationship index.
func (f *File) deleteSheetRelationships(sheet, rID string) {
name, ok := f.sheetMap[trimSheetName(sheet)]
if !ok {
@@ -169,7 +221,7 @@ func (f *File) deleteSheetRelationships(sheet, rID string) {
f.saveFileList(rels, output)
}
-// addSheetLegacyDrawing provides function to add legacy drawing element to
+// addSheetLegacyDrawing provides a function to add legacy drawing element to
// xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
func (f *File) addSheetLegacyDrawing(sheet string, rID int) {
xlsx := f.workSheetReader(sheet)
@@ -178,7 +230,7 @@ func (f *File) addSheetLegacyDrawing(sheet string, rID int) {
}
}
-// addSheetDrawing provides function to add drawing element to
+// addSheetDrawing provides a function to add drawing element to
// xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
func (f *File) addSheetDrawing(sheet string, rID int) {
xlsx := f.workSheetReader(sheet)
@@ -187,7 +239,7 @@ func (f *File) addSheetDrawing(sheet string, rID int) {
}
}
-// addSheetPicture provides function to add picture element to
+// addSheetPicture provides a function to add picture element to
// xl/worksheets/sheet%d.xml by given worksheet name and relationship index.
func (f *File) addSheetPicture(sheet string, rID int) {
xlsx := f.workSheetReader(sheet)
@@ -196,7 +248,7 @@ func (f *File) addSheetPicture(sheet string, rID int) {
}
}
-// countDrawings provides function to get drawing files count storage in the
+// countDrawings provides a function to get drawing files count storage in the
// folder xl/drawings.
func (f *File) countDrawings() int {
count := 0
@@ -208,7 +260,7 @@ func (f *File) countDrawings() int {
return count
}
-// addDrawingPicture provides function to add picture by given sheet,
+// addDrawingPicture provides a function to add picture by given sheet,
// drawingXML, cell, file name, width, height relationship index and format
// sets.
func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, height, rID, hyperlinkRID int, formatSet *formatPicture) {
@@ -263,8 +315,8 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
f.saveFileList(drawingXML, output)
}
-// addDrawingRelationships provides function to add image part relationships in
-// the file xl/drawings/_rels/drawing%d.xml.rels by given drawing index,
+// addDrawingRelationships provides a function to add image part relationships
+// in the file xl/drawings/_rels/drawing%d.xml.rels by given drawing index,
// relationship type and target.
func (f *File) addDrawingRelationships(index int, relType, target, targetMode string) int {
var rels = "xl/drawings/_rels/drawing" + strconv.Itoa(index) + ".xml.rels"
@@ -292,8 +344,8 @@ func (f *File) addDrawingRelationships(index int, relType, target, targetMode st
return rID
}
-// countMedia provides function to get media files count storage in the folder
-// xl/media/image.
+// countMedia provides a function to get media files count storage in the
+// folder xl/media/image.
func (f *File) countMedia() int {
count := 0
for k := range f.XLSX {
@@ -304,17 +356,16 @@ func (f *File) countMedia() int {
return count
}
-// addMedia provides function to add picture into folder xl/media/image by given
-// file name and extension name.
-func (f *File) addMedia(file, ext string) {
+// addMedia provides a function to add picture into folder xl/media/image by
+// given file and extension name.
+func (f *File) addMedia(file []byte, ext string) {
count := f.countMedia()
- dat, _ := ioutil.ReadFile(file)
media := "xl/media/image" + strconv.Itoa(count+1) + ext
- f.XLSX[media] = dat
+ f.XLSX[media] = file
}
-// setContentTypePartImageExtensions provides function to set the content type
-// for relationship parts and the Main Document part.
+// setContentTypePartImageExtensions provides a function to set the content
+// type for relationship parts and the Main Document part.
func (f *File) setContentTypePartImageExtensions() {
var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false}
content := f.contentTypesReader()
@@ -334,7 +385,7 @@ func (f *File) setContentTypePartImageExtensions() {
}
}
-// setContentTypePartVMLExtensions provides function to set the content type
+// setContentTypePartVMLExtensions provides a function to set the content type
// for relationship parts and the Main Document part.
func (f *File) setContentTypePartVMLExtensions() {
vml := false
@@ -352,8 +403,8 @@ func (f *File) setContentTypePartVMLExtensions() {
}
}
-// addContentTypePart provides function to add content type part relationships
-// in the file [Content_Types].xml by given index.
+// addContentTypePart provides a function to add content type part
+// relationships in the file [Content_Types].xml by given index.
func (f *File) addContentTypePart(index int, contentType string) {
setContentType := map[string]func(){
"comments": f.setContentTypePartVMLExtensions,
@@ -387,7 +438,7 @@ func (f *File) addContentTypePart(index int, contentType string) {
})
}
-// getSheetRelationshipsTargetByID provides function to get Target attribute
+// getSheetRelationshipsTargetByID provides a function to get Target attribute
// value in xl/worksheets/_rels/sheet%d.xml.rels by given worksheet name and
// relationship index.
func (f *File) getSheetRelationshipsTargetByID(sheet, rID string) string {
@@ -406,9 +457,9 @@ func (f *File) getSheetRelationshipsTargetByID(sheet, rID string) string {
return ""
}
-// GetPicture provides function to get picture base name and raw content embed
-// in XLSX by given worksheet and cell name. This function returns the file name
-// in XLSX and file contents as []byte data types. For example:
+// GetPicture provides a function to get picture base name and raw content
+// embed in XLSX by given worksheet and cell name. This function returns the
+// file name in XLSX and file contents as []byte data types. For example:
//
// xlsx, err := excelize.OpenFile("./Book1.xlsx")
// if err != nil {
@@ -463,8 +514,9 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte) {
return "", []byte{}
}
-// getDrawingRelationships provides function to get drawing relationships from
-// xl/drawings/_rels/drawing%s.xml.rels by given file name and relationship ID.
+// getDrawingRelationships provides a function to get drawing relationships
+// from xl/drawings/_rels/drawing%s.xml.rels by given file name and
+// relationship ID.
func (f *File) getDrawingRelationships(rels, rID string) *xlsxWorkbookRelation {
_, ok := f.XLSX[rels]
if !ok {