Make functions: `SetCellValue`, `SetCellInt`, `SetCellHyperLink`, `SetCellFormula`, `GetCellValue` and `GetCellFormula` to support the merged cells.
This commit is contained in:
parent
c0a3020886
commit
a1060e779e
48
cell.go
48
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.
|
||||
|
|
38
excelize.go
38
excelize.go
|
@ -64,14 +64,20 @@ func (f *File) SetCellValue(sheet string, axis string, value interface{}) {
|
|||
func (f *File) SetCellInt(sheet string, axis string, value int) {
|
||||
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
|
||||
|
||||
|
@ -89,18 +95,24 @@ func (f *File) SetCellInt(sheet string, axis string, value int) {
|
|||
// of characters that a cell can contain 32767 characters.
|
||||
func (f *File) SetCellStr(sheet string, axis string, value 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]
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(value) > 32767 {
|
||||
value = value[0:32767]
|
||||
}
|
||||
var xlsx xlsxWorksheet
|
||||
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
|
||||
|
||||
|
@ -119,14 +131,20 @@ func (f *File) SetCellStr(sheet string, axis string, value string) {
|
|||
func (f *File) SetCellDefault(sheet string, axis string, value 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
|
||||
|
||||
|
|
|
@ -241,14 +241,21 @@ func TestSMergeCell(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
xlsx.MergeCell("sheet1", "D9", "D9")
|
||||
xlsx.MergeCell("sheet1", "D9", "E9")
|
||||
xlsx.MergeCell("sheet1", "H14", "G13")
|
||||
xlsx.MergeCell("sheet1", "C9", "D8")
|
||||
xlsx.MergeCell("sheet1", "F11", "G13")
|
||||
xlsx.MergeCell("sheet1", "H7", "B15")
|
||||
xlsx.MergeCell("sheet1", "D11", "F13")
|
||||
xlsx.MergeCell("sheet1", "G10", "I11")
|
||||
xlsx.MergeCell("Sheet1", "D9", "D9")
|
||||
xlsx.MergeCell("Sheet1", "D9", "E9")
|
||||
xlsx.MergeCell("Sheet1", "H14", "G13")
|
||||
xlsx.MergeCell("Sheet1", "C9", "D8")
|
||||
xlsx.MergeCell("Sheet1", "F11", "G13")
|
||||
xlsx.MergeCell("Sheet1", "H7", "B15")
|
||||
xlsx.MergeCell("Sheet1", "D11", "F13")
|
||||
xlsx.MergeCell("Sheet1", "G10", "K12")
|
||||
xlsx.SetCellValue("Sheet1", "G11", "set value in merged cell")
|
||||
xlsx.SetCellInt("Sheet1", "H11", 100)
|
||||
xlsx.SetCellValue("Sheet1", "I11", float64(0.5))
|
||||
xlsx.SetCellHyperLink("Sheet1", "J11", "https://github.com/Luxurioust/excelize")
|
||||
xlsx.SetCellFormula("Sheet1", "G12", "SUM(Sheet1!B19,Sheet1!C19)")
|
||||
xlsx.GetCellValue("Sheet1", "H11")
|
||||
xlsx.GetCellFormula("Sheet1", "G12")
|
||||
err = xlsx.Save()
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
|
|
Loading…
Reference in New Issue