summaryrefslogtreecommitdiff
path: root/picture.go
diff options
context:
space:
mode:
authorRi Xu <xuri.me@gmail.com>2018-02-03 15:02:37 +0800
committerRi Xu <xuri.me@gmail.com>2018-02-03 15:02:37 +0800
commit76219275737f28d1b58ac4c693c5e8f59daf53fd (patch)
tree9ac1d5f8273406b4f16d3bba48d36168f46e242e /picture.go
parent638bd44dd5940bfcc5921171dc00978f3a24f7e8 (diff)
- Add a hyperlink to an image support for the function `AddPicture()`, relate issue #185;
- go test and document has been updated.
Diffstat (limited to 'picture.go')
-rw-r--r--picture.go41
1 files changed, 30 insertions, 11 deletions
diff --git a/picture.go b/picture.go
index fc8b6e7..f37cee6 100644
--- a/picture.go
+++ b/picture.go
@@ -52,13 +52,13 @@ func parseFormatPictureSet(formatSet string) *formatPicture {
// if err != nil {
// fmt.Println(err)
// }
-// // Insert a picture to sheet with scaling.
-// err = xlsx.AddPicture("Sheet1", "D2", "./image1.png", `{"x_scale": 0.5, "y_scale": 0.5}`)
+// // Insert a picture scaling in the cell with location hyperlink.
+// err = xlsx.AddPicture("Sheet1", "D2", "./image1.png", `{"x_scale": 0.5, "y_scale": 0.5, "hyperlink": "#Sheet2!D8", "hyperlink_type": "Location"}`)
// if err != nil {
// fmt.Println(err)
// }
-// // Insert a picture offset in the cell with printing support.
-// err = xlsx.AddPicture("Sheet1", "H2", "./image3.gif", `{"x_offset": 15, "y_offset": 10, "print_obj": true, "lock_aspect_ratio": false, "locked": false}`)
+// // Insert a picture offset in the cell with external hyperlink and printing support.
+// err = xlsx.AddPicture("Sheet1", "H2", "./image3.gif", `{"x_offset": 15, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External", "print_obj": true, "lock_aspect_ratio": false, "locked": false}`)
// if err != nil {
// fmt.Println(err)
// }
@@ -68,8 +68,13 @@ func parseFormatPictureSet(formatSet string) *formatPicture {
// }
// }
//
+// LinkType defines two types of hyperlink "External" for web site or
+// "Location" for moving to one of cell in this workbook. When the
+// "hyperlink_type" is "Location", coordinates need to start with "#".
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
@@ -89,8 +94,15 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error {
pictureID := f.countMedia() + 1
drawingXML := "xl/drawings/drawing" + strconv.Itoa(drawingID) + ".xml"
drawingID, drawingXML = f.prepareDrawing(xlsx, drawingID, sheet, drawingXML)
- drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipImage, "../media/image"+strconv.Itoa(pictureID)+ext)
- f.addDrawingPicture(sheet, drawingXML, cell, file, image.Width, image.Height, drawingRID, formatSet)
+ drawingRID := f.addDrawingRelationships(drawingID, SourceRelationshipImage, "../media/image"+strconv.Itoa(pictureID)+ext, hyperlinkType)
+ // Add picture with hyperlink.
+ if formatSet.Hyperlink != "" && formatSet.HyperlinkType != "" {
+ if formatSet.HyperlinkType == "External" {
+ hyperlinkType = formatSet.HyperlinkType
+ }
+ 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.addContentTypePart(drawingID, "drawings")
return err
@@ -191,7 +203,7 @@ func (f *File) countDrawings() int {
// addDrawingPicture provides 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 int, formatSet *formatPicture) {
+func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, height, rID, hyperlinkRID int, formatSet *formatPicture) {
cell = strings.ToUpper(cell)
fromCol := string(strings.Map(letterOnlyMapF, cell))
fromRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, cell))
@@ -223,6 +235,12 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
pic.NvPicPr.CNvPr.ID = f.countCharts() + f.countMedia() + 1
pic.NvPicPr.CNvPr.Descr = file
pic.NvPicPr.CNvPr.Name = "Picture " + strconv.Itoa(cNvPrID)
+ if hyperlinkRID != 0 {
+ pic.NvPicPr.CNvPr.HlinkClick = &xlsxHlinkClick{
+ R: SourceRelationship,
+ RID: "rId" + strconv.Itoa(hyperlinkRID),
+ }
+ }
pic.BlipFill.Blip.R = SourceRelationship
pic.BlipFill.Blip.Embed = "rId" + strconv.Itoa(rID)
pic.SpPr.PrstGeom.Prst = "rect"
@@ -240,7 +258,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
// addDrawingRelationships provides 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 string) int {
+func (f *File) addDrawingRelationships(index int, relType, target, targetMode string) int {
var rels = "xl/drawings/_rels/drawing" + strconv.Itoa(index) + ".xml.rels"
var drawingRels xlsxWorkbookRels
var rID = 1
@@ -256,9 +274,10 @@ func (f *File) addDrawingRelationships(index int, relType, target string) int {
ID.WriteString(strconv.Itoa(rID))
}
drawingRels.Relationships = append(drawingRels.Relationships, xlsxWorkbookRelation{
- ID: ID.String(),
- Type: relType,
- Target: target,
+ ID: ID.String(),
+ Type: relType,
+ Target: target,
+ TargetMode: targetMode,
})
output, _ := xml.Marshal(drawingRels)
f.saveFileList(rels, string(output))