Skip to content

Commit

Permalink
Handle coordinate parse exception, relate issue qax-os#122.
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Sep 19, 2017
1 parent b7b937a commit e820388
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
35 changes: 28 additions & 7 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand All @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
25 changes: 21 additions & 4 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit e820388

Please sign in to comment.