From 3c4ad28db75108dfd974b994df26ec7f33a69be7 Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Tue, 30 Aug 2016 21:54:28 +0800 Subject: [PATCH] - Get cell value support - Optimisation code use fmt package - Update README - Remove useless function --- README.md | 2 -- cell.go | 40 ++++++++++++++++++++++++++++++++++++++++ excelize.go | 4 ++-- excelize_test.go | 23 ++++++++++++++++------- sheet.go | 8 ++++---- xmlContentTypes.go | 28 ---------------------------- xmlSharedStrings.go | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 43 deletions(-) create mode 100644 cell.go create mode 100644 xmlSharedStrings.go diff --git a/README.md b/README.md index 0bd7c02..56b9951 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,6 @@ Excelize is a library written in pure Golang and providing a set of function tha ### Installation -Golang version requirements 1.6.0 or higher. - ``` go get github.com/Luxurioust/excelize ``` diff --git a/cell.go b/cell.go new file mode 100644 index 0000000..4ce9619 --- /dev/null +++ b/cell.go @@ -0,0 +1,40 @@ +package excelize + +import ( + "encoding/xml" + "strconv" + "strings" +) + +// Get value from cell by given sheet index and axis in XLSX file +func GetCellValue(file []FileList, sheet string, axis string) string { + axis = strings.ToUpper(axis) + var xlsx xlsxWorksheet + row := getRowIndex(axis) + xAxis := row - 1 + name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml` + xml.Unmarshal([]byte(readXml(file, name)), &xlsx) + rows := len(xlsx.SheetData.Row) + if rows <= xAxis { + return `` + } + for _, v := range xlsx.SheetData.Row[xAxis].C { + if xlsx.SheetData.Row[xAxis].R == row { + if axis == v.R { + switch v.T { + case "s": + shardStrings := xlsxSST{} + xlsxSI := 0 + xlsxSI, _ = strconv.Atoi(v.V) + xml.Unmarshal([]byte(readXml(file, `xl/sharedStrings.xml`)), &shardStrings) + return shardStrings.SI[xlsxSI].T + case "str": + return v.V + default: + return v.V + } + } + } + } + return `` +} diff --git a/excelize.go b/excelize.go index 3838231..9ee5e7c 100644 --- a/excelize.go +++ b/excelize.go @@ -34,7 +34,7 @@ func SetCellInt(file []FileList, sheet string, axis string, value int) []FileLis xAxis := row - 1 yAxis := titleToNumber(col) - name := fmt.Sprintf("xl/worksheets/%s.xml", strings.ToLower(sheet)) + name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml` xml.Unmarshal([]byte(readXml(file, name)), &xlsx) rows := xAxis + 1 @@ -65,7 +65,7 @@ func SetCellStr(file []FileList, sheet string, axis string, value string) []File xAxis := row - 1 yAxis := titleToNumber(col) - name := fmt.Sprintf("xl/worksheets/%s.xml", strings.ToLower(sheet)) + name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml` xml.Unmarshal([]byte(readXml(file, name)), &xlsx) rows := xAxis + 1 diff --git a/excelize_test.go b/excelize_test.go index b392264..5dbcdb4 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -1,7 +1,6 @@ package excelize import ( - "fmt" "strconv" "testing" ) @@ -10,7 +9,7 @@ func TestExcelize(t *testing.T) { // Test update a XLSX file file, err := OpenFile("./test/Workbook1.xlsx") if err != nil { - fmt.Println(err) + t.Error(err) } file = SetCellInt(file, "SHEET2", "B2", 100) file = SetCellStr(file, "SHEET2", "C11", "Knowns") @@ -19,21 +18,31 @@ func TestExcelize(t *testing.T) { file = SetCellStr(file, "SHEET3", "b230", "10") file = SetActiveSheet(file, 2) if err != nil { - fmt.Println(err) + t.Error(err) } for i := 1; i <= 300; i++ { - file = SetCellStr(file, "SHEET3", fmt.Sprintf("c%d", i), strconv.Itoa(i)) + file = SetCellStr(file, "SHEET3", "c"+strconv.Itoa(i), strconv.Itoa(i)) } err = Save(file, "./test/Workbook_2.xlsx") // Test create a XLSX file file2 := CreateFile() - file2 = NewSheet(file2, 2, "SHEETxxx") - file2 = NewSheet(file2, 3, "asd") + file2 = NewSheet(file2, 2, "TestSheet2") + file2 = NewSheet(file2, 3, "TestSheet3") file2 = SetCellInt(file2, "Sheet2", "A23", 10) file2 = SetCellStr(file2, "SHEET1", "B20", "10") err = Save(file2, "./test/Workbook_3.xlsx") if err != nil { - fmt.Println(err) + t.Error(err) } + + // Test read cell value + file, err = OpenFile("./test/Workbook1.xlsx") + if err != nil { + t.Error(err) + } + GetCellValue(file, "Sheet2", "a5") + GetCellValue(file, "Sheet2", "D11") + GetCellValue(file, "Sheet2", "D12") + GetCellValue(file, "Sheet2", "E12") } diff --git a/sheet.go b/sheet.go index 732b5c1..9a3cf15 100644 --- a/sheet.go +++ b/sheet.go @@ -29,7 +29,7 @@ func setContentTypes(file []FileList, index int) []FileList { var content xlsxTypes xml.Unmarshal([]byte(readXml(file, `[Content_Types].xml`)), &content) content.Overrides = append(content.Overrides, xlsxOverride{ - PartName: fmt.Sprintf("/xl/worksheets/sheet%d.xml", index), + PartName: `/xl/worksheets/sheet` + strconv.Itoa(index) + `.xml`, ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml", }) output, err := xml.MarshalIndent(content, "", "") @@ -50,7 +50,7 @@ func setSheet(file []FileList, index int) []FileList { if err != nil { fmt.Println(err) } - path := fmt.Sprintf("xl/worksheets/sheet%d.xml", index) + path := `xl/worksheets/sheet` + strconv.Itoa(index) + `.xml` return saveFileList(file, path, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output)))) } @@ -86,7 +86,7 @@ func addXlsxWorkbookRels(file []FileList, sheet int) []FileList { rId := len(content.Relationships) + 1 content.Relationships = append(content.Relationships, xlsxWorkbookRelation{ Id: "rId" + strconv.Itoa(rId), - Target: fmt.Sprintf("worksheets/sheet%d.xml", sheet), + Target: `worksheets/sheet` + strconv.Itoa(sheet) + `.xml`, Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet", }) output, err := xml.MarshalIndent(content, "", "") @@ -150,7 +150,7 @@ func SetActiveSheet(file []FileList, index int) []FileList { for i := 0; i < sheets; i++ { xlsx := xlsxWorksheet{} sheetIndex := i + 1 - path := fmt.Sprintf("xl/worksheets/sheet%d.xml", sheetIndex) + path := `xl/worksheets/sheet` + strconv.Itoa(sheetIndex) + `.xml` xml.Unmarshal([]byte(readXml(file, path)), &xlsx) if index == sheetIndex { if len(xlsx.SheetViews.SheetView) > 0 { diff --git a/xmlContentTypes.go b/xmlContentTypes.go index af42774..c9e03f6 100644 --- a/xmlContentTypes.go +++ b/xmlContentTypes.go @@ -19,31 +19,3 @@ type xlsxDefault struct { Extension string `xml:",attr"` ContentType string `xml:",attr"` } - -func MakeDefaultContentTypes() (types xlsxTypes) { - types.Overrides = make([]xlsxOverride, 8) - types.Defaults = make([]xlsxDefault, 2) - - types.Overrides[0].PartName = "/_rels/.rels" - types.Overrides[0].ContentType = "application/vnd.openxmlformats-package.relationships+xml" - types.Overrides[1].PartName = "/docProps/app.xml" - types.Overrides[1].ContentType = "application/vnd.openxmlformats-officedocument.extended-properties+xml" - types.Overrides[2].PartName = "/docProps/core.xml" - types.Overrides[2].ContentType = "application/vnd.openxmlformats-package.core-properties+xml" - types.Overrides[3].PartName = "/xl/_rels/workbook.xml.rels" - types.Overrides[3].ContentType = "application/vnd.openxmlformats-package.relationships+xml" - types.Overrides[4].PartName = "/xl/sharedStrings.xml" - types.Overrides[4].ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml" - types.Overrides[5].PartName = "/xl/styles.xml" - types.Overrides[5].ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml" - types.Overrides[6].PartName = "/xl/workbook.xml" - types.Overrides[6].ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml" - types.Overrides[7].PartName = "/xl/theme/theme1.xml" - types.Overrides[7].ContentType = "application/vnd.openxmlformats-officedocument.theme+xml" - - types.Defaults[0].Extension = "rels" - types.Defaults[0].ContentType = "application/vnd.openxmlformats-package.relationships+xml" - types.Defaults[1].Extension = "xml" - types.Defaults[1].ContentType = "application/xml" - return -} diff --git a/xmlSharedStrings.go b/xmlSharedStrings.go new file mode 100644 index 0000000..22401f0 --- /dev/null +++ b/xmlSharedStrings.go @@ -0,0 +1,32 @@ +package excelize + +import ( + "encoding/xml" +) + +// xlsxSST directly maps the sst element from the namespace +// http://schemas.openxmlformats.org/spreadsheetml/2006/main currently +// I have not checked this for completeness - it does as much as I need. +type xlsxSST struct { + XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main sst"` + Count int `xml:"count,attr"` + UniqueCount int `xml:"uniqueCount,attr"` + SI []xlsxSI `xml:"si"` +} + +// xlsxSI directly maps the si element from the namespace +// http://schemas.openxmlformats.org/spreadsheetml/2006/main - +// currently I have not checked this for completeness - it does as +// much as I need. +type xlsxSI struct { + T string `xml:"t"` + R []xlsxR `xml:"r"` +} + +// xlsxR directly maps the r element from the namespace +// http://schemas.openxmlformats.org/spreadsheetml/2006/main - +// currently I have not checked this for completeness - it does as +// much as I need. +type xlsxR struct { + T string `xml:"t"` +} -- GitLab