Skip to content

Commit

Permalink
Support set work sheet background image.
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Jan 24, 2017
1 parent 9559f45 commit bd5b033
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 15 deletions.
2 changes: 1 addition & 1 deletion col.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) {
// | | | (x2,y2)|
// +-----+------------+------------+
//
// Example of an object that covers some of the area from cell A1 to B2.
// Example of an object that covers some of the area from cell A1 to B2.
//
// Based on the width and height of the object we need to calculate 8 vars:
//
Expand Down
27 changes: 27 additions & 0 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,30 @@ func TestSetCellFormula(t *testing.T) {
t.Log(err)
}
}

func TestSetSheetBackground(t *testing.T) {
xlsx, err := OpenFile("./test/Workbook1.xlsx")
if err != nil {
t.Log(err)
}
err = xlsx.SetSheetBackground("sheet2", "./test/images/background.png")
if err != nil {
t.Log(err)
}
err = xlsx.SetSheetBackground("sheet2", "./test/Workbook1.xlsx")
if err != nil {
t.Log(err)
}
err = xlsx.SetSheetBackground("sheet2", "./test/images/background.jpg")
if err != nil {
t.Log(err)
}
err = xlsx.SetSheetBackground("sheet2", "./test/images/background.jpg")
if err != nil {
t.Log(err)
}
err = xlsx.Save()
if err != nil {
t.Log(err)
}
}
2 changes: 1 addition & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func CreateFile() *File {
}
}

// Save provides function override the xlsx file with origin path.
// Save provides function to override the xlsx file with origin path.
func (f *File) Save() error {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
Expand Down
10 changes: 6 additions & 4 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ func ReadZipReader(r *zip.Reader) (map[string]string, int, error) {
return fileList, worksheets, nil
}

// Read XML content as string.
// readXML provides function to read XML content as string.
func (f *File) readXML(name string) string {
if content, ok := f.XLSX[name]; ok {
return content
}
return ""
}

// Update given file content in file list of XLSX.
// saveFileList provides function to update given file content in file list of
// XLSX.
func (f *File) saveFileList(name string, content string) {
f.XLSX[name] = XMLHeader + content
}
Expand All @@ -63,7 +64,8 @@ func readFile(file *zip.File) string {
return string(buff.Bytes())
}

// Convert integer to Excel sheet column title.
// toAlphaString provides function to convert integer to Excel sheet column
// title.
func toAlphaString(value int) string {
if value < 0 {
return ""
Expand All @@ -77,7 +79,7 @@ func toAlphaString(value int) string {
return ans
}

// Convert Excel sheet column title to int.
// titleToNumber provides function to convert Excel sheet column title to int.
func titleToNumber(s string) int {
weight := 0.0
sum := 0
Expand Down
38 changes: 31 additions & 7 deletions picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,23 @@ func (f *File) addSheetDrawing(sheet string, rID int) {
if err != nil {
fmt.Println(err)
}
f.saveFileList(name, string(output))
f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
}

// addSheetPicture provides function to add picture element to
// xl/worksheets/sheet%d.xml by given sheet name and relationship index.
func (f *File) addSheetPicture(sheet string, rID int) {
var xlsx xlsxWorksheet
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
xlsx.Picture = &xlsxPicture{
RID: "rId" + strconv.Itoa(rID),
}
output, err := xml.Marshal(xlsx)
if err != nil {
fmt.Println(err)
}
f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
}

// countDrawings provides function to get drawing files count storage in the
Expand Down Expand Up @@ -270,13 +286,10 @@ func (f *File) addMedia(file string, ext string) {
f.XLSX[media] = string(dat)
}

// addDrawingContentTypePart provides function to add image part relationships
// in http://purl.oclc.org/ooxml/officeDocument/relationships/image and
// appropriate content type.
func (f *File) addDrawingContentTypePart(index int) {
func (f *File) setContentTypePartImageExtensions() {
var imageTypes = map[string]bool{"jpeg": false, "png": false, "gif": false}
var content xlsxTypes
xml.Unmarshal([]byte(f.readXML(`[Content_Types].xml`)), &content)
xml.Unmarshal([]byte(f.readXML("[Content_Types].xml")), &content)
for _, v := range content.Defaults {
_, ok := imageTypes[v.Extension]
if ok {
Expand All @@ -291,6 +304,17 @@ func (f *File) addDrawingContentTypePart(index int) {
})
}
}
output, _ := xml.Marshal(content)
f.saveFileList("[Content_Types].xml", string(output))
}

// addDrawingContentTypePart provides function to add image part relationships
// in http://purl.oclc.org/ooxml/officeDocument/relationships/image and
// appropriate content type.
func (f *File) addDrawingContentTypePart(index int) {
f.setContentTypePartImageExtensions()
var content xlsxTypes
xml.Unmarshal([]byte(f.readXML("[Content_Types].xml")), &content)
for _, v := range content.Overrides {
if v.PartName == "/xl/drawings/drawing"+strconv.Itoa(index)+".xml" {
output, _ := xml.Marshal(content)
Expand All @@ -303,7 +327,7 @@ func (f *File) addDrawingContentTypePart(index int) {
ContentType: "application/vnd.openxmlformats-officedocument.drawing+xml",
})
output, _ := xml.Marshal(content)
f.saveFileList(`[Content_Types].xml`, string(output))
f.saveFileList("[Content_Types].xml", string(output))
}

// getSheetRelationshipsTargetByID provides function to get Target attribute
Expand Down
32 changes: 30 additions & 2 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package excelize
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"os"
"path"
"strconv"
"strings"
)
Expand Down Expand Up @@ -193,8 +196,8 @@ func (f *File) GetActiveSheetIndex() int {
xml.Unmarshal([]byte(f.readXML(buffer.String())), &xlsx)
for _, sheetView := range xlsx.SheetViews.SheetView {
if sheetView.TabSelected {
id, _ := strconv.Atoi(strings.TrimPrefix(v.ID, "rId"))
return id
ID, _ := strconv.Atoi(strings.TrimPrefix(v.ID, "rId"))
return ID
}
}
buffer.Reset()
Expand Down Expand Up @@ -258,3 +261,28 @@ func (f *File) GetSheetMap() map[int]string {
}
return sheetMap
}

// SetSheetBackground provides function to set background picture by given sheet
// index.
func (f *File) SetSheetBackground(sheet, picture string) error {
var supportTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png"}
var err error
// Check picture exists first.
if _, err = os.Stat(picture); os.IsNotExist(err) {
return err
}
ext, ok := supportTypes[path.Ext(picture)]
if !ok {
return errors.New("Unsupported image extension")
}
// Read sheet data.
var xlsx xlsxWorksheet
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
pictureID := f.countMedia() + 1
rID := f.addSheetRelationships(sheet, SourceRelationshipImage, "../media/image"+strconv.Itoa(pictureID)+ext, "")
f.addSheetPicture(sheet, rID)
f.addMedia(picture, ext)
f.setContentTypePartImageExtensions()
return err
}
Binary file added test/images/background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bd5b033

Please sign in to comment.