summaryrefslogtreecommitdiff
path: root/cell.go
diff options
context:
space:
mode:
authorRi Xu <xuri.me@gmail.com>2017-01-25 18:13:11 +0800
committerRi Xu <xuri.me@gmail.com>2017-01-25 18:13:11 +0800
commita1060e779ee295e9871e2fbe9148d8d9dc72a379 (patch)
tree2203416f0de8cf2dd3c5f0c7e09b52fdc8f593ea /cell.go
parentc0a3020886df623e76baedec04b97903573b6c0d (diff)
Make functions: `SetCellValue`, `SetCellInt`, `SetCellHyperLink`, `SetCellFormula`, `GetCellValue` and `GetCellFormula` to support the merged cells.
Diffstat (limited to 'cell.go')
-rw-r--r--cell.go48
1 files changed, 37 insertions, 11 deletions
diff --git a/cell.go b/cell.go
index fc555b5..e9933aa 100644
--- a/cell.go
+++ b/cell.go
@@ -7,15 +7,21 @@ import (
)
// GetCellValue provides function to get value from cell by given sheet index
-// and axis in XLSX file. The value of the merged cell is not available
-// currently.
+// and axis in XLSX file.
func (f *File) GetCellValue(sheet string, axis string) string {
axis = strings.ToUpper(axis)
var xlsx xlsxWorksheet
- row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
- xAxis := row - 1
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
+ if xlsx.MergeCells != nil {
+ for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
+ if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) {
+ axis = strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0]
+ }
+ }
+ }
+ row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+ xAxis := row - 1
rows := len(xlsx.SheetData.Row)
if rows > 1 {
lastRow := xlsx.SheetData.Row[rows-1].R
@@ -56,10 +62,17 @@ func (f *File) GetCellValue(sheet string, axis string) string {
func (f *File) GetCellFormula(sheet string, axis string) string {
axis = strings.ToUpper(axis)
var xlsx xlsxWorksheet
- row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
- xAxis := row - 1
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
+ if xlsx.MergeCells != nil {
+ for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
+ if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) {
+ axis = strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0]
+ }
+ }
+ }
+ row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+ xAxis := row - 1
rows := len(xlsx.SheetData.Row)
if rows > 1 {
lastRow := xlsx.SheetData.Row[rows-1].R
@@ -91,14 +104,20 @@ func (f *File) GetCellFormula(sheet string, axis string) string {
func (f *File) SetCellFormula(sheet, axis, formula string) {
axis = strings.ToUpper(axis)
var xlsx xlsxWorksheet
+ name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
+ xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
+ if xlsx.MergeCells != nil {
+ for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
+ if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) {
+ axis = strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0]
+ }
+ }
+ }
col := string(strings.Map(letterOnlyMapF, axis))
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
xAxis := row - 1
yAxis := titleToNumber(col)
- name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
- xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
-
rows := xAxis + 1
cell := yAxis + 1
@@ -124,6 +143,13 @@ func (f *File) SetCellHyperLink(sheet, axis, link string) {
var xlsx xlsxWorksheet
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
+ if xlsx.MergeCells != nil {
+ for i := 0; i < len(xlsx.MergeCells.Cells); i++ {
+ if checkCellInArea(axis, xlsx.MergeCells.Cells[i].Ref) {
+ axis = strings.Split(xlsx.MergeCells.Cells[i].Ref, ":")[0]
+ }
+ }
+ }
rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External")
hyperlink := xlsxHyperlink{
Ref: axis,
@@ -141,9 +167,9 @@ func (f *File) SetCellHyperLink(sheet, axis, link string) {
}
// MergeCell provides function to merge cells by given axis and sheet name.
-// For example create a merged cell of A1:B2 on Sheet1:
+// For example create a merged cell of D3:E9 on Sheet1:
//
-// xlsx.MergeCell("sheet1", "D9", "E9")
+// xlsx.MergeCell("sheet1", "D3", "E9")
//
// If you create a merged cell that overlaps with another existing merged cell,
// those merged cells that already exist will be removed.