Skip to content

Commit 3c4ad28

Browse files
committed
- Get cell value support
- Optimisation code use fmt package - Update README - Remove useless function
1 parent 0d60020 commit 3c4ad28

7 files changed

+94
-43
lines changed

Diff for: README.md

-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ Excelize is a library written in pure Golang and providing a set of function tha
1616

1717
### Installation
1818

19-
Golang version requirements 1.6.0 or higher.
20-
2119
```
2220
go get github.com/Luxurioust/excelize
2321
```

Diff for: cell.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package excelize
2+
3+
import (
4+
"encoding/xml"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
// Get value from cell by given sheet index and axis in XLSX file
10+
func GetCellValue(file []FileList, sheet string, axis string) string {
11+
axis = strings.ToUpper(axis)
12+
var xlsx xlsxWorksheet
13+
row := getRowIndex(axis)
14+
xAxis := row - 1
15+
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
16+
xml.Unmarshal([]byte(readXml(file, name)), &xlsx)
17+
rows := len(xlsx.SheetData.Row)
18+
if rows <= xAxis {
19+
return ``
20+
}
21+
for _, v := range xlsx.SheetData.Row[xAxis].C {
22+
if xlsx.SheetData.Row[xAxis].R == row {
23+
if axis == v.R {
24+
switch v.T {
25+
case "s":
26+
shardStrings := xlsxSST{}
27+
xlsxSI := 0
28+
xlsxSI, _ = strconv.Atoi(v.V)
29+
xml.Unmarshal([]byte(readXml(file, `xl/sharedStrings.xml`)), &shardStrings)
30+
return shardStrings.SI[xlsxSI].T
31+
case "str":
32+
return v.V
33+
default:
34+
return v.V
35+
}
36+
}
37+
}
38+
}
39+
return ``
40+
}

Diff for: excelize.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func SetCellInt(file []FileList, sheet string, axis string, value int) []FileLis
3434
xAxis := row - 1
3535
yAxis := titleToNumber(col)
3636

37-
name := fmt.Sprintf("xl/worksheets/%s.xml", strings.ToLower(sheet))
37+
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
3838
xml.Unmarshal([]byte(readXml(file, name)), &xlsx)
3939

4040
rows := xAxis + 1
@@ -65,7 +65,7 @@ func SetCellStr(file []FileList, sheet string, axis string, value string) []File
6565
xAxis := row - 1
6666
yAxis := titleToNumber(col)
6767

68-
name := fmt.Sprintf("xl/worksheets/%s.xml", strings.ToLower(sheet))
68+
name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
6969
xml.Unmarshal([]byte(readXml(file, name)), &xlsx)
7070

7171
rows := xAxis + 1

Diff for: excelize_test.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package excelize
22

33
import (
4-
"fmt"
54
"strconv"
65
"testing"
76
)
@@ -10,7 +9,7 @@ func TestExcelize(t *testing.T) {
109
// Test update a XLSX file
1110
file, err := OpenFile("./test/Workbook1.xlsx")
1211
if err != nil {
13-
fmt.Println(err)
12+
t.Error(err)
1413
}
1514
file = SetCellInt(file, "SHEET2", "B2", 100)
1615
file = SetCellStr(file, "SHEET2", "C11", "Knowns")
@@ -19,21 +18,31 @@ func TestExcelize(t *testing.T) {
1918
file = SetCellStr(file, "SHEET3", "b230", "10")
2019
file = SetActiveSheet(file, 2)
2120
if err != nil {
22-
fmt.Println(err)
21+
t.Error(err)
2322
}
2423
for i := 1; i <= 300; i++ {
25-
file = SetCellStr(file, "SHEET3", fmt.Sprintf("c%d", i), strconv.Itoa(i))
24+
file = SetCellStr(file, "SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i))
2625
}
2726
err = Save(file, "./test/Workbook_2.xlsx")
2827

2928
// Test create a XLSX file
3029
file2 := CreateFile()
31-
file2 = NewSheet(file2, 2, "SHEETxxx")
32-
file2 = NewSheet(file2, 3, "asd")
30+
file2 = NewSheet(file2, 2, "TestSheet2")
31+
file2 = NewSheet(file2, 3, "TestSheet3")
3332
file2 = SetCellInt(file2, "Sheet2", "A23", 10)
3433
file2 = SetCellStr(file2, "SHEET1", "B20", "10")
3534
err = Save(file2, "./test/Workbook_3.xlsx")
3635
if err != nil {
37-
fmt.Println(err)
36+
t.Error(err)
3837
}
38+
39+
// Test read cell value
40+
file, err = OpenFile("./test/Workbook1.xlsx")
41+
if err != nil {
42+
t.Error(err)
43+
}
44+
GetCellValue(file, "Sheet2", "a5")
45+
GetCellValue(file, "Sheet2", "D11")
46+
GetCellValue(file, "Sheet2", "D12")
47+
GetCellValue(file, "Sheet2", "E12")
3948
}

Diff for: sheet.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func setContentTypes(file []FileList, index int) []FileList {
2929
var content xlsxTypes
3030
xml.Unmarshal([]byte(readXml(file, `[Content_Types].xml`)), &content)
3131
content.Overrides = append(content.Overrides, xlsxOverride{
32-
PartName: fmt.Sprintf("/xl/worksheets/sheet%d.xml", index),
32+
PartName: `/xl/worksheets/sheet` + strconv.Itoa(index) + `.xml`,
3333
ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
3434
})
3535
output, err := xml.MarshalIndent(content, "", "")
@@ -50,7 +50,7 @@ func setSheet(file []FileList, index int) []FileList {
5050
if err != nil {
5151
fmt.Println(err)
5252
}
53-
path := fmt.Sprintf("xl/worksheets/sheet%d.xml", index)
53+
path := `xl/worksheets/sheet` + strconv.Itoa(index) + `.xml`
5454
return saveFileList(file, path, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output))))
5555
}
5656

@@ -86,7 +86,7 @@ func addXlsxWorkbookRels(file []FileList, sheet int) []FileList {
8686
rId := len(content.Relationships) + 1
8787
content.Relationships = append(content.Relationships, xlsxWorkbookRelation{
8888
Id: "rId" + strconv.Itoa(rId),
89-
Target: fmt.Sprintf("worksheets/sheet%d.xml", sheet),
89+
Target: `worksheets/sheet` + strconv.Itoa(sheet) + `.xml`,
9090
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
9191
})
9292
output, err := xml.MarshalIndent(content, "", "")
@@ -150,7 +150,7 @@ func SetActiveSheet(file []FileList, index int) []FileList {
150150
for i := 0; i < sheets; i++ {
151151
xlsx := xlsxWorksheet{}
152152
sheetIndex := i + 1
153-
path := fmt.Sprintf("xl/worksheets/sheet%d.xml", sheetIndex)
153+
path := `xl/worksheets/sheet` + strconv.Itoa(sheetIndex) + `.xml`
154154
xml.Unmarshal([]byte(readXml(file, path)), &xlsx)
155155
if index == sheetIndex {
156156
if len(xlsx.SheetViews.SheetView) > 0 {

Diff for: xmlContentTypes.go

-28
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,3 @@ type xlsxDefault struct {
1919
Extension string `xml:",attr"`
2020
ContentType string `xml:",attr"`
2121
}
22-
23-
func MakeDefaultContentTypes() (types xlsxTypes) {
24-
types.Overrides = make([]xlsxOverride, 8)
25-
types.Defaults = make([]xlsxDefault, 2)
26-
27-
types.Overrides[0].PartName = "/_rels/.rels"
28-
types.Overrides[0].ContentType = "application/vnd.openxmlformats-package.relationships+xml"
29-
types.Overrides[1].PartName = "/docProps/app.xml"
30-
types.Overrides[1].ContentType = "application/vnd.openxmlformats-officedocument.extended-properties+xml"
31-
types.Overrides[2].PartName = "/docProps/core.xml"
32-
types.Overrides[2].ContentType = "application/vnd.openxmlformats-package.core-properties+xml"
33-
types.Overrides[3].PartName = "/xl/_rels/workbook.xml.rels"
34-
types.Overrides[3].ContentType = "application/vnd.openxmlformats-package.relationships+xml"
35-
types.Overrides[4].PartName = "/xl/sharedStrings.xml"
36-
types.Overrides[4].ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"
37-
types.Overrides[5].PartName = "/xl/styles.xml"
38-
types.Overrides[5].ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"
39-
types.Overrides[6].PartName = "/xl/workbook.xml"
40-
types.Overrides[6].ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"
41-
types.Overrides[7].PartName = "/xl/theme/theme1.xml"
42-
types.Overrides[7].ContentType = "application/vnd.openxmlformats-officedocument.theme+xml"
43-
44-
types.Defaults[0].Extension = "rels"
45-
types.Defaults[0].ContentType = "application/vnd.openxmlformats-package.relationships+xml"
46-
types.Defaults[1].Extension = "xml"
47-
types.Defaults[1].ContentType = "application/xml"
48-
return
49-
}

Diff for: xmlSharedStrings.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package excelize
2+
3+
import (
4+
"encoding/xml"
5+
)
6+
7+
// xlsxSST directly maps the sst element from the namespace
8+
// http://schemas.openxmlformats.org/spreadsheetml/2006/main currently
9+
// I have not checked this for completeness - it does as much as I need.
10+
type xlsxSST struct {
11+
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main sst"`
12+
Count int `xml:"count,attr"`
13+
UniqueCount int `xml:"uniqueCount,attr"`
14+
SI []xlsxSI `xml:"si"`
15+
}
16+
17+
// xlsxSI directly maps the si element from the namespace
18+
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
19+
// currently I have not checked this for completeness - it does as
20+
// much as I need.
21+
type xlsxSI struct {
22+
T string `xml:"t"`
23+
R []xlsxR `xml:"r"`
24+
}
25+
26+
// xlsxR directly maps the r element from the namespace
27+
// http://schemas.openxmlformats.org/spreadsheetml/2006/main -
28+
// currently I have not checked this for completeness - it does as
29+
// much as I need.
30+
type xlsxR struct {
31+
T string `xml:"t"`
32+
}

0 commit comments

Comments
 (0)