forked from qax-os/excelize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.go
141 lines (132 loc) · 3.64 KB
/
cell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package excelize
import (
"encoding/xml"
"strconv"
"strings"
)
// 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.
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)
rows := len(xlsx.SheetData.Row)
if rows > 1 {
lastRow := xlsx.SheetData.Row[rows-1].R
if lastRow >= rows {
rows = lastRow
}
}
if rows < xAxis {
return ""
}
for _, v := range xlsx.SheetData.Row {
if v.R != row {
continue
}
for _, r := range v.C {
if axis != r.R {
continue
}
switch r.T {
case "s":
shardStrings := xlsxSST{}
xlsxSI := 0
xlsxSI, _ = strconv.Atoi(r.V)
xml.Unmarshal([]byte(f.readXML("xl/sharedStrings.xml")), &shardStrings)
return shardStrings.SI[xlsxSI].T
case "str":
return r.V
default:
return r.V
}
}
}
return ""
}
// GetCellFormula provides function to get formula from cell by given sheet
// index and axis in XLSX file.
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)
rows := len(xlsx.SheetData.Row)
if rows > 1 {
lastRow := xlsx.SheetData.Row[rows-1].R
if lastRow >= rows {
rows = lastRow
}
}
if rows < xAxis {
return ""
}
for _, v := range xlsx.SheetData.Row {
if v.R != row {
continue
}
for _, f := range v.C {
if axis != f.R {
continue
}
if f.F != nil {
return f.F.Content
}
}
}
return ""
}
// SetCellFormula provides function to set cell formula by given string and
// sheet index.
func (f *File) SetCellFormula(sheet, axis, formula string) {
axis = strings.ToUpper(axis)
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
xlsx = completeRow(xlsx, rows, cell)
xlsx = completeCol(xlsx, rows, cell)
if xlsx.SheetData.Row[xAxis].C[yAxis].F != nil {
xlsx.SheetData.Row[xAxis].C[yAxis].F.Content = formula
} else {
f := xlsxF{
Content: formula,
}
xlsx.SheetData.Row[xAxis].C[yAxis].F = &f
}
output, _ := xml.Marshal(xlsx)
f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
}
// SetCellHyperLink provides function to set cell hyperlink by given sheet index
// and link URL address. Only support external link currently.
func (f *File) SetCellHyperLink(sheet, axis, link string) {
axis = strings.ToUpper(axis)
var xlsx xlsxWorksheet
name := "xl/worksheets/" + strings.ToLower(sheet) + ".xml"
xml.Unmarshal([]byte(f.readXML(name)), &xlsx)
rID := f.addSheetRelationships(sheet, SourceRelationshipHyperLink, link, "External")
hyperlink := xlsxHyperlink{
Ref: axis,
RID: "rId" + strconv.Itoa(rID),
}
if xlsx.Hyperlinks != nil {
xlsx.Hyperlinks.Hyperlink = append(xlsx.Hyperlinks.Hyperlink, hyperlink)
} else {
hyperlinks := xlsxHyperlinks{}
hyperlinks.Hyperlink = append(hyperlinks.Hyperlink, hyperlink)
xlsx.Hyperlinks = &hyperlinks
}
output, _ := xml.Marshal(xlsx)
f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output)))
}