Skip to content

Commit

Permalink
- Support to set the positioning of a picture, relate issue qax-os#214;
Browse files Browse the repository at this point in the history
- go test and godoc has been updated
  • Loading branch information
xuri committed Apr 26, 2018
1 parent 9ee57fd commit a9c7d66
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## Introduction

Excelize is a library written in pure Golang and 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. The full API docs can be seen using go's built-in documentation tool, or online at [godoc.org](https://godoc.org/github.com/360EntSecGroup-Skylar/excelize) or [Chinese translation](https://xuri.me/excelize/zh_cn).
Excelize is a library written in pure Golang and 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. The full API docs can be seen using go's built-in documentation tool, or online at [godoc.org](https://godoc.org/github.com/360EntSecGroup-Skylar/excelize) and [Chinese translation](https://xuri.me/excelize/zh_cn).

## Basic Usage

Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## 简介

Excelize 是 Go 语言编写的用于操作 Office Excel 文档类库,基于 ECMA-376 Office OpenXML 标准。可以使用它来读取、写入由 Microsoft Excel™ 2007 及以上版本创建的 XLSX 文档。相比较其他的开源类库,Excelize 支持写入原本带有图片(表)、透视表和切片器等复杂样式的文档,还支持向 Excel 文档中插入图片与图表,并且在保存后不会丢失文档原有样式,可以应用于各类报表系统中。使用本类库要求使用的 Go 语言为 1.8 或更高版本,完整的 API 使用文档请访问 [godoc.org](https://godoc.org/github.com/360EntSecGroup-Skylar/excelize) [中文翻译](https://xuri.me/excelize/zh_cn)
Excelize 是 Go 语言编写的用于操作 Office Excel 文档类库,基于 ECMA-376 Office OpenXML 标准。可以使用它来读取、写入由 Microsoft Excel™ 2007 及以上版本创建的 XLSX 文档。相比较其他的开源类库,Excelize 支持写入原本带有图片(表)、透视表和切片器等复杂样式的文档,还支持向 Excel 文档中插入图片与图表,并且在保存后不会丢失文档原有样式,可以应用于各类报表系统中。使用本类库要求使用的 Go 语言为 1.8 或更高版本,完整的 API 使用文档请访问 [godoc.org](https://godoc.org/github.com/360EntSecGroup-Skylar/excelize) 或查看 [中文翻译](https://xuri.me/excelize/zh_cn)

## 快速上手

Expand Down
2 changes: 1 addition & 1 deletion chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ func (f *File) addDrawingChart(sheet, drawingXML, cell string, width, height, rI
content.Xdr = NameSpaceDrawingMLSpreadSheet
cNvPrID := f.drawingParser(drawingXML, &content)
twoCellAnchor := xdrCellAnchor{}
twoCellAnchor.EditAs = "oneCell"
twoCellAnchor.EditAs = formatSet.Positioning
from := xlsxFrom{}
from.Col = colStart
from.ColOff = formatSet.OffsetX * EMU
Expand Down
8 changes: 4 additions & 4 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func TestAddPicture(t *testing.T) {
if err != nil {
t.Log(err)
}
// Test add picture to worksheet with offset and external hyperlink.
err = xlsx.AddPicture("Sheet1", "F21", "./test/images/excel.png", `{"x_offset": 10, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External"}`)
// Test add picture to worksheet with offset, external hyperlink and positioning.
err = xlsx.AddPicture("Sheet1", "F21", "./test/images/excel.png", `{"x_offset": 10, "y_offset": 10, "hyperlink": "https://github.com/360EntSecGroup-Skylar/excelize", "hyperlink_type": "External", "positioning": "oneCell"}`)
if err != nil {
t.Log(err)
}
Expand Down Expand Up @@ -196,8 +196,8 @@ func TestNewFile(t *testing.T) {
xlsx.SetCellInt("XLSXSheet2", "A23", 56)
xlsx.SetCellStr("Sheet1", "B20", "42")
xlsx.SetActiveSheet(0)
// Test add picture to sheet with scaling.
err := xlsx.AddPicture("Sheet1", "H2", "./test/images/excel.gif", `{"x_scale": 0.5, "y_scale": 0.5}`)
// Test add picture to sheet with scaling and positioning.
err := xlsx.AddPicture("Sheet1", "H2", "./test/images/excel.gif", `{"x_scale": 0.5, "y_scale": 0.5, "positioning": "absolute"}`)
if err != nil {
t.Log(err)
}
Expand Down
11 changes: 8 additions & 3 deletions picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func parseFormatPictureSet(formatSet string) *formatPicture {
// if err != nil {
// fmt.Println(err)
// }
// // 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}`)
// // Insert a picture offset in the cell with external hyperlink, printing and positioning 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, "positioning": "oneCell"}`)
// if err != nil {
// fmt.Println(err)
// }
Expand All @@ -71,6 +71,11 @@ 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 "#".
//
// Positioning defines two types of the position of a picture in an Excel
// spreadsheet, "oneCell" (Move but don't size with cells) or "absolute"
// (Don't move or size with cells). If you don't set this parameter, default
// positioning is move and size with cells.
func (f *File) AddPicture(sheet, cell, picture, format string) error {
var err error
var drawingHyperlinkRID int
Expand Down Expand Up @@ -217,7 +222,7 @@ func (f *File) addDrawingPicture(sheet, drawingXML, cell, file string, width, he
content.Xdr = NameSpaceDrawingMLSpreadSheet
cNvPrID := f.drawingParser(drawingXML, &content)
twoCellAnchor := xdrCellAnchor{}
twoCellAnchor.EditAs = "oneCell"
twoCellAnchor.EditAs = formatSet.Positioning
from := xlsxFrom{}
from.Col = colStart
from.ColOff = formatSet.OffsetX * EMU
Expand Down
2 changes: 1 addition & 1 deletion shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func (f *File) addDrawingShape(sheet, drawingXML, cell string, formatSet *format
content.Xdr = NameSpaceDrawingMLSpreadSheet
cNvPrID := f.drawingParser(drawingXML, &content)
twoCellAnchor := xdrCellAnchor{}
twoCellAnchor.EditAs = "oneCell"
twoCellAnchor.EditAs = formatSet.Format.Positioning
from := xlsxFrom{}
from.Col = colStart
from.ColOff = formatSet.Format.OffsetX * EMU
Expand Down
6 changes: 3 additions & 3 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"unicode/utf8"
)

// NewSheet provides function to create a new sheet by given index, when
// creating a new XLSX file, the default sheet will be create, when you create
// a new file.
// NewSheet provides function to create a new sheet by given worksheet name,
// when creating a new XLSX file, the default sheet will be create, when you
// create a new file.
func (f *File) NewSheet(name string) int {
// Check if the worksheet already exists
if f.GetSheetIndex(name) != 0 {
Expand Down
1 change: 1 addition & 0 deletions xmlDrawing.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ type formatPicture struct {
YScale float64 `json:"y_scale"`
Hyperlink string `json:"hyperlink"`
HyperlinkType string `json:"hyperlink_type"`
Positioning string `json:"positioning"`
}

// formatShape directly maps the format settings of the shape.
Expand Down

0 comments on commit a9c7d66

Please sign in to comment.