From 79dfe1c3070b3c4af14a40acaa5bd8cb477acd3e Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 13 Jul 2018 17:40:47 +0800 Subject: GoDoc updated. --- picture.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'picture.go') diff --git a/picture.go b/picture.go index ab72fc8..04d5062 100644 --- a/picture.go +++ b/picture.go @@ -26,7 +26,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 } -- cgit v1.2.1 From ec37b114c3b704a84c66fcf3e135c9df88ffb24d Mon Sep 17 00:00:00 2001 From: xuri Date: Mon, 6 Aug 2018 10:21:24 +0800 Subject: Fixes #256 and format document. --- picture.go | 57 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'picture.go') diff --git a/picture.go b/picture.go index 04d5062..d039ae0 100644 --- a/picture.go +++ b/picture.go @@ -14,8 +14,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, @@ -116,7 +116,7 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error { 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 +149,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 +169,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 +178,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 +187,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 +196,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 +208,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 +263,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 +292,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,8 +304,8 @@ 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. +// addMedia provides a function to add picture into folder xl/media/image by +// given file name and extension name. func (f *File) addMedia(file, ext string) { count := f.countMedia() dat, _ := ioutil.ReadFile(file) @@ -313,8 +313,8 @@ func (f *File) addMedia(file, ext string) { f.XLSX[media] = dat } -// 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 +334,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 +352,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 +387,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 +406,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 +463,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 { -- cgit v1.2.1 From ce5b37a4ac93196f90cfef2aec381a9b7d153fdd Mon Sep 17 00:00:00 2001 From: Farmerx Date: Mon, 20 Aug 2018 16:53:51 +0800 Subject: =?UTF-8?q?#=20fix=20:=20file=20close=20=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- picture.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'picture.go') diff --git a/picture.go b/picture.go index d039ae0..97e72a6 100644 --- a/picture.go +++ b/picture.go @@ -88,7 +88,11 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error { if !ok { return errors.New("Unsupported image extension") } - readFile, _ := os.Open(picture) + readFile, err := os.Open(picture) + if err!=nil{ + return err + } + defer readFile.Close() image, _, _ := image.DecodeConfig(readFile) _, file := filepath.Split(picture) formatSet, err := parseFormatPictureSet(format) -- cgit v1.2.1 From b4a6e61ec34d4a0db1110907cc969f0d7d38991a Mon Sep 17 00:00:00 2001 From: xuri Date: Wed, 12 Sep 2018 15:47:56 +0800 Subject: Fix golint errors under confidence 0.1 --- picture.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'picture.go') diff --git a/picture.go b/picture.go index 97e72a6..decc37d 100644 --- a/picture.go +++ b/picture.go @@ -1,3 +1,11 @@ +// 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. +// +// 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 import ( @@ -86,10 +94,10 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error { } ext, ok := supportImageTypes[path.Ext(picture)] if !ok { - return errors.New("Unsupported image extension") + return errors.New("unsupported image extension") } readFile, err := os.Open(picture) - if err!=nil{ + if err != nil { return err } defer readFile.Close() -- cgit v1.2.1 From 6ced438f39030e8a9a521548d4112dd002dc2ebe Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 14 Sep 2018 00:24:49 +0800 Subject: New function `AddPictureFromBytes()` has been added, this resolve #259 and close #271. --- picture.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 13 deletions(-) (limited to 'picture.go') diff --git a/picture.go b/picture.go index decc37d..e473498 100644 --- a/picture.go +++ b/picture.go @@ -86,8 +86,6 @@ 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 @@ -96,14 +94,55 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error { if !ok { return errors.New("unsupported image extension") } - readFile, err := os.Open(picture) + 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") + } + formatSet, err := parseFormatPictureSet(format) if err != nil { return err } - defer readFile.Close() - image, _, _ := image.DecodeConfig(readFile) - _, file := filepath.Split(picture) - formatSet, err := parseFormatPictureSet(format) + image, _, err := image.DecodeConfig(bytes.NewReader(file)) if err != nil { return err } @@ -122,8 +161,8 @@ 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 } @@ -317,12 +356,11 @@ func (f *File) countMedia() int { } // addMedia provides a function to add picture into folder xl/media/image by -// given file name and extension name. -func (f *File) addMedia(file, ext string) { +// 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 a function to set the content -- cgit v1.2.1 From 13a9769cc5bde486c52d8e45661ff8108cd786ae Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 14 Sep 2018 00:44:23 +0800 Subject: Comments style changed. --- picture.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'picture.go') diff --git a/picture.go b/picture.go index e473498..16b428f 100644 --- a/picture.go +++ b/picture.go @@ -1,11 +1,11 @@ +// 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. -// -// 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 import ( -- cgit v1.2.1 From 3e004d900b103379c2d62657a3070de4a2e8585a Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 14 Sep 2018 00:58:48 +0800 Subject: Comments style changed. --- picture.go | 1 + 1 file changed, 1 insertion(+) (limited to 'picture.go') diff --git a/picture.go b/picture.go index 16b428f..8785aaf 100644 --- a/picture.go +++ b/picture.go @@ -6,6 +6,7 @@ // 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 ( -- cgit v1.2.1