Browse Source

Handle coordinate parse exception, relate issue #122.

Ri Xu 8 years ago
parent
commit
e820388d70
3 changed files with 57 additions and 13 deletions
  1. 28 7
      cell.go
  2. 21 4
      excelize_test.go
  3. 8 2
      styles.go

+ 28 - 7
cell.go

@@ -76,7 +76,10 @@ func (f *File) SetCellValue(sheet, axis string, value interface{}) {
 func (f *File) GetCellValue(sheet, axis string) string {
 	xlsx := f.workSheetReader(sheet)
 	axis = f.mergeCellsParser(xlsx, axis)
-	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	if err != nil {
+		return ""
+	}
 	xAxis := row - 1
 	rows := len(xlsx.SheetData.Row)
 	if rows > 1 {
@@ -124,7 +127,10 @@ func (f *File) GetCellStyle(sheet, axis string) int {
 	xlsx := f.workSheetReader(sheet)
 	axis = f.mergeCellsParser(xlsx, axis)
 	col := string(strings.Map(letterOnlyMapF, axis))
-	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	if err != nil {
+		return 0
+	}
 	xAxis := row - 1
 	yAxis := TitleToNumber(col)
 
@@ -142,7 +148,10 @@ func (f *File) GetCellStyle(sheet, axis string) int {
 func (f *File) GetCellFormula(sheet, axis string) string {
 	xlsx := f.workSheetReader(sheet)
 	axis = f.mergeCellsParser(xlsx, axis)
-	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	if err != nil {
+		return ""
+	}
 	xAxis := row - 1
 	rows := len(xlsx.SheetData.Row)
 	if rows > 1 {
@@ -176,7 +185,10 @@ func (f *File) SetCellFormula(sheet, axis, formula string) {
 	xlsx := f.workSheetReader(sheet)
 	axis = f.mergeCellsParser(xlsx, axis)
 	col := string(strings.Map(letterOnlyMapF, axis))
-	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	if err != nil {
+		return
+	}
 	xAxis := row - 1
 	yAxis := TitleToNumber(col)
 
@@ -326,7 +338,10 @@ func (f *File) SetCellInt(sheet, axis string, value int) {
 	xlsx := f.workSheetReader(sheet)
 	axis = f.mergeCellsParser(xlsx, axis)
 	col := string(strings.Map(letterOnlyMapF, axis))
-	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	if err != nil {
+		return
+	}
 	xAxis := row - 1
 	yAxis := TitleToNumber(col)
 
@@ -363,7 +378,10 @@ func (f *File) SetCellStr(sheet, axis, value string) {
 		value = value[0:32767]
 	}
 	col := string(strings.Map(letterOnlyMapF, axis))
-	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	if err != nil {
+		return
+	}
 	xAxis := row - 1
 	yAxis := TitleToNumber(col)
 
@@ -393,7 +411,10 @@ func (f *File) SetCellDefault(sheet, axis, value string) {
 	xlsx := f.workSheetReader(sheet)
 	axis = f.mergeCellsParser(xlsx, axis)
 	col := string(strings.Map(letterOnlyMapF, axis))
-	row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
+	if err != nil {
+		return
+	}
 	xAxis := row - 1
 	yAxis := TitleToNumber(col)
 

+ 21 - 4
excelize_test.go

@@ -31,9 +31,12 @@ func TestOpenFile(t *testing.T) {
 	xlsx.UpdateLinkedValue()
 	xlsx.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(float64(100.1588), 'f', -1, 32))
 	xlsx.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64))
+	// Test set cell value with illegal row number.
+	xlsx.SetCellDefault("Sheet2", "A", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64))
 	xlsx.SetCellInt("Sheet2", "A1", 100)
+	// Test set cell integer value with illegal row number.
+	xlsx.SetCellInt("Sheet2", "A", 100)
 	xlsx.SetCellStr("Sheet2", "C11", "Knowns")
-
 	// Test max characters in a cell.
 	xlsx.SetCellStr("Sheet2", "D11", strings.Repeat("c", 32769))
 	xlsx.NewSheet(":\\/?*[]Maximum 31 characters allowed in sheet title.")
@@ -42,12 +45,19 @@ func TestOpenFile(t *testing.T) {
 	xlsx.SetCellInt("Sheet3", "A23", 10)
 	xlsx.SetCellStr("Sheet3", "b230", "10")
 	xlsx.SetCellStr("Sheet10", "b230", "10")
+	// Test set cell string value with illegal row number.
+	xlsx.SetCellStr("Sheet10", "A", "10")
 	xlsx.SetActiveSheet(2)
-	xlsx.GetCellFormula("Sheet1", "B19") // Test get cell formula with given rows number.
-	xlsx.GetCellFormula("Sheet2", "B20") // Test get cell formula with illegal worksheet index.
-	xlsx.GetCellFormula("Sheet1", "B20") // Test get cell formula with illegal rows number.
+	// Test get cell formula with given rows number.
+	xlsx.GetCellFormula("Sheet1", "B19")
+	// Test get cell formula with illegal worksheet index.
+	xlsx.GetCellFormula("Sheet2", "B20")
+	// Test get cell formula with illegal rows number.
+	xlsx.GetCellFormula("Sheet1", "B20")
+	xlsx.GetCellFormula("Sheet1", "B")
 	// Test read cell value with given illegal rows number.
 	xlsx.GetCellValue("Sheet2", "a-1")
+	xlsx.GetCellValue("Sheet2", "A")
 	// Test read cell value with given lowercase column number.
 	xlsx.GetCellValue("Sheet2", "a5")
 	xlsx.GetCellValue("Sheet2", "C11")
@@ -245,6 +255,8 @@ func TestSetCellFormula(t *testing.T) {
 	}
 	xlsx.SetCellFormula("Sheet1", "B19", "SUM(Sheet2!D2,Sheet2!D11)")
 	xlsx.SetCellFormula("Sheet1", "C19", "SUM(Sheet2!D2,Sheet2!D9)")
+	// Test set cell formula with illegal rows number.
+	xlsx.SetCellFormula("Sheet1", "C", "SUM(Sheet2!D2,Sheet2!D9)")
 	err = xlsx.Save()
 	if err != nil {
 		t.Log(err)
@@ -315,6 +327,11 @@ func TestSetCellStyleAlignment(t *testing.T) {
 		t.Log(err)
 	}
 	xlsx.SetCellStyle("Sheet1", "A22", "A22", style)
+	// Test set cell style with given illegal rows number.
+	xlsx.SetCellStyle("Sheet1", "A", "A22", style)
+	xlsx.SetCellStyle("Sheet1", "A22", "A", style)
+	// Test get cell style with given illegal rows number.
+	xlsx.GetCellStyle("Sheet1", "A")
 	err = xlsx.Save()
 	if err != nil {
 		t.Log(err)

+ 8 - 2
styles.go

@@ -2279,12 +2279,18 @@ func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) {
 
 	// Coordinate conversion, convert C1:B3 to 2,0,1,2.
 	hcol := string(strings.Map(letterOnlyMapF, hcell))
-	hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
+	hrow, err := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
+	if err != nil {
+		return
+	}
 	hyAxis := hrow - 1
 	hxAxis := TitleToNumber(hcol)
 
 	vcol := string(strings.Map(letterOnlyMapF, vcell))
-	vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
+	vrow, err := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
+	if err != nil {
+		return
+	}
 	vyAxis := vrow - 1
 	vxAxis := TitleToNumber(vcol)