Browse Source

Use conjunction with strings.Map to split Axis and update godoc.

Ri Xu 9 years ago
parent
commit
2e8fa2d39c
5 changed files with 34 additions and 37 deletions
  1. 1 1
      cell.go
  2. 8 8
      excelize.go
  3. 18 21
      lib.go
  4. 4 4
      sheet.go
  5. 3 3
      xmlWorkbook.go

+ 1 - 1
cell.go

@@ -10,7 +10,7 @@ import (
 func (f *File) GetCellValue(sheet string, axis string) string {
 	axis = strings.ToUpper(axis)
 	var xlsx xlsxWorksheet
-	row := getRowIndex(axis)
+	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
 	xAxis := row - 1
 	name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml`
 	xml.Unmarshal([]byte(f.readXML(name)), &xlsx)

+ 8 - 8
excelize.go

@@ -52,8 +52,8 @@ 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
-	col := getColIndex(axis)
-	row := getRowIndex(axis)
+	col := string(strings.Map(letterOnlyMapF, axis))
+	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
 	xAxis := row - 1
 	yAxis := titleToNumber(col)
 
@@ -77,8 +77,8 @@ func (f *File) SetCellInt(sheet string, axis string, value int) {
 func (f *File) SetCellStr(sheet string, axis string, value string) {
 	axis = strings.ToUpper(axis)
 	var xlsx xlsxWorksheet
-	col := getColIndex(axis)
-	row := getRowIndex(axis)
+	col := string(strings.Map(letterOnlyMapF, axis))
+	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
 	xAxis := row - 1
 	yAxis := titleToNumber(col)
 
@@ -196,8 +196,8 @@ func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
 		if lenCol < 1 {
 			continue
 		}
-		endR := getColIndex(v.C[lenCol-1].R)
-		endRow := getRowIndex(v.C[lenCol-1].R)
+		endR := string(strings.Map(letterOnlyMapF, v.C[lenCol-1].R))
+		endRow, _ := strconv.Atoi(strings.Map(intOnlyMapF, v.C[lenCol-1].R))
 		endCol := titleToNumber(endR) + 1
 		if lenCol < endCol {
 			oldRow := xlsx.SheetData.Row[k].C
@@ -213,7 +213,7 @@ func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
 			}
 			xlsx.SheetData.Row[k].C = tmp
 			for _, y := range oldRow {
-				colAxis := titleToNumber(getColIndex(y.R))
+				colAxis := titleToNumber(string(strings.Map(letterOnlyMapF, y.R)))
 				xlsx.SheetData.Row[k].C[colAxis] = y
 			}
 		}
@@ -223,7 +223,7 @@ func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
 
 // UpdateLinkedValue fix linked values within a spreadsheet are not updating in
 // Office Excel 2007 and 2010. This function will be remove value tag when met a
-//  cell have a linked value. Reference https://social.technet.microsoft.com/Forums/office/en-US/e16bae1f-6a2c-4325-8013-e989a3479066/excel-2010-linked-cells-not-updating?forum=excel
+// cell have a linked value. Reference https://social.technet.microsoft.com/Forums/office/en-US/e16bae1f-6a2c-4325-8013-e989a3479066/excel-2010-linked-cells-not-updating?forum=excel
 //
 // Notice: after open XLSX file Excel will be update linked value and generate
 // new value and will prompt save file or not.

+ 18 - 21
lib.go

@@ -7,8 +7,6 @@ import (
 	"io"
 	"log"
 	"math"
-	"regexp"
-	"strconv"
 	"strings"
 )
 
@@ -28,9 +26,9 @@ func ReadZipReader(r *zip.Reader) (map[string]string, int, error) {
 	for _, v := range r.File {
 		fileList[v.Name] = readFile(v)
 		if len(v.Name) > 18 {
-			if v.Name[0:19] == "xl/worksheets/sheet" {
+			if v.Name[0:19] == `xl/worksheets/sheet` {
 				var xlsx xlsxWorksheet
-				xml.Unmarshal([]byte(strings.Replace(fileList[v.Name], "<drawing r:id=", "<drawing rid=", -1)), &xlsx)
+				xml.Unmarshal([]byte(strings.Replace(fileList[v.Name], `<drawing r:id=`, `<drawing rid=`, -1)), &xlsx)
 				xlsx = checkRow(xlsx)
 				output, _ := xml.Marshal(xlsx)
 				fileList[v.Name] = replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output)))
@@ -44,7 +42,7 @@ func ReadZipReader(r *zip.Reader) (map[string]string, int, error) {
 // Read XML content as string and replace drawing property in XML namespace of sheet
 func (f *File) readXML(name string) string {
 	if content, ok := f.XLSX[name]; ok {
-		return strings.Replace(content, "<drawing r:id=", "<drawing rid=", -1)
+		return strings.Replace(content, `<drawing r:id=`, `<drawing rid=`, -1)
 	}
 	return ``
 }
@@ -91,24 +89,23 @@ func titleToNumber(s string) int {
 	return sum - 1
 }
 
-// Split Excel sheet column title to string and integer, return XAxis
-func getColIndex(axis string) string {
-	r, err := regexp.Compile(`[^\D]`)
-	if err != nil {
-		log.Fatal(err)
+// letterOnlyMapF is used in conjunction with strings.Map to return
+// only the characters A-Z and a-z in a string
+func letterOnlyMapF(rune rune) rune {
+	switch {
+	case 'A' <= rune && rune <= 'Z':
+		return rune
+	case 'a' <= rune && rune <= 'z':
+		return rune - 32
 	}
-	return string(r.ReplaceAll([]byte(axis), []byte("")))
+	return -1
 }
 
-// Split Excel sheet column title to string and integer, return YAxis
-func getRowIndex(axis string) int {
-	r, err := regexp.Compile(`[\D]`)
-	if err != nil {
-		log.Fatal(err)
-	}
-	row, err := strconv.Atoi(string(r.ReplaceAll([]byte(axis), []byte(""))))
-	if err != nil {
-		log.Fatal(err)
+// intOnlyMapF is used in conjunction with strings.Map to return only
+// the numeric portions of a string.
+func intOnlyMapF(rune rune) rune {
+	if rune >= 48 && rune < 58 {
+		return rune
 	}
-	return row
+	return -1
 }

+ 4 - 4
sheet.go

@@ -9,8 +9,8 @@ import (
 )
 
 // NewSheet provice function to greate a new sheet by given index, when
-//  creating a new XLSX file, the default sheet will be create, when you
-//  create a new file, you need to ensure that the index is continuous.
+// creating a new XLSX file, the default sheet will be create, when you
+// create a new file, you need to ensure that the index is continuous.
 func (f *File) NewSheet(index int, name string) {
 	// Update docProps/app.xml
 	f.setAppXML()
@@ -30,7 +30,7 @@ func (f *File) setContentTypes(index int) {
 	xml.Unmarshal([]byte(f.readXML(`[Content_Types].xml`)), &content)
 	content.Overrides = append(content.Overrides, xlsxOverride{
 		PartName:    `/xl/worksheets/sheet` + strconv.Itoa(index) + `.xml`,
-		ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
+		ContentType: `application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml`,
 	})
 	output, err := xml.Marshal(content)
 	if err != nil {
@@ -94,7 +94,7 @@ func (f *File) addXlsxWorkbookRels(sheet int) {
 	content.Relationships = append(content.Relationships, xlsxWorkbookRelation{
 		ID:     ID.String(),
 		Target: target.String(),
-		Type:   "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
+		Type:   `http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet`,
 	})
 	output, err := xml.Marshal(content)
 	if err != nil {

+ 3 - 3
xmlWorkbook.go

@@ -9,9 +9,9 @@ import (
 const (
 	// sheet state values as defined by
 	// http://msdn.microsoft.com/en-us/library/office/documentformat.openxml.spreadsheet.sheetstatevalues.aspx
-	sheetStateVisible    = "visible"
-	sheetStateHidden     = "hidden"
-	sheetStateVeryHidden = "veryHidden"
+	sheetStateVisible    = `visible`
+	sheetStateHidden     = `hidden`
+	sheetStateVeryHidden = `veryHidden`
 )
 
 // xmlxWorkbookRels contains xmlxWorkbookRelations