diff --git a/README.md b/README.md index 9a7f058ad8b07188be864cebab3d57ff4a3a6d94..d9c70df634f67027211e8e2e1ed8c1b3a572ee15 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/README_zh.md b/README_zh.md index fc3651a798b6bff36bbff79ea1d0f94c337e67b8..ef149b0876124b2b77480f387a9cace887ef75df 100644 --- a/README_zh.md +++ b/README_zh.md @@ -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)。 ## 快速上手 diff --git a/chart.go b/chart.go index 4a2774cf1a12bb9d671b093f5d0f37bc37c5401e..6168553247e3da27ede0596f2c5cf0822fc11790 100644 --- a/chart.go +++ b/chart.go @@ -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 diff --git a/excelize_test.go b/excelize_test.go index b5fa0d1963cbfb66efbde70aa711e4230db64a9c..f2dd0d08ba0c9ce37ed372b9e0a0676e4b4069d6 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -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) } @@ -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) } diff --git a/picture.go b/picture.go index 1f556c47b1b8a30fa0cb847e1fddc5a50118cfec..a7f48d16bd61a28165bb4d199ef16da1c9264d24 100644 --- a/picture.go +++ b/picture.go @@ -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) // } @@ -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 @@ -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 diff --git a/shape.go b/shape.go index 1c9039b37266fbfd99818a84ad5736d2a0f691f0..f1328f7ca3876a57b051e1877c60a02f7a8e38f7 100644 --- a/shape.go +++ b/shape.go @@ -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 diff --git a/sheet.go b/sheet.go index bc7cb75adc8d86969edbf0a9cf90b07814054492..f2b6e173cf8da1aaec7c54f1f595c295913a32b8 100644 --- a/sheet.go +++ b/sheet.go @@ -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 { diff --git a/xmlDrawing.go b/xmlDrawing.go index 600d83afb2d3f3d7894da32ab46c56341e8447bd..beb6bc94a4d7adca3c42cc8fed1075dd5215ef6f 100644 --- a/xmlDrawing.go +++ b/xmlDrawing.go @@ -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.