Skip to content

Commit e820388

Browse files
committed
Handle coordinate parse exception, relate issue qax-os#122.
1 parent b7b937a commit e820388

File tree

3 files changed

+57
-13
lines changed

3 files changed

+57
-13
lines changed

Diff for: cell.go

+28-7
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ func (f *File) SetCellValue(sheet, axis string, value interface{}) {
7676
func (f *File) GetCellValue(sheet, axis string) string {
7777
xlsx := f.workSheetReader(sheet)
7878
axis = f.mergeCellsParser(xlsx, axis)
79-
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
79+
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
80+
if err != nil {
81+
return ""
82+
}
8083
xAxis := row - 1
8184
rows := len(xlsx.SheetData.Row)
8285
if rows > 1 {
@@ -124,7 +127,10 @@ func (f *File) GetCellStyle(sheet, axis string) int {
124127
xlsx := f.workSheetReader(sheet)
125128
axis = f.mergeCellsParser(xlsx, axis)
126129
col := string(strings.Map(letterOnlyMapF, axis))
127-
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
130+
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
131+
if err != nil {
132+
return 0
133+
}
128134
xAxis := row - 1
129135
yAxis := TitleToNumber(col)
130136

@@ -142,7 +148,10 @@ func (f *File) GetCellStyle(sheet, axis string) int {
142148
func (f *File) GetCellFormula(sheet, axis string) string {
143149
xlsx := f.workSheetReader(sheet)
144150
axis = f.mergeCellsParser(xlsx, axis)
145-
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
151+
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
152+
if err != nil {
153+
return ""
154+
}
146155
xAxis := row - 1
147156
rows := len(xlsx.SheetData.Row)
148157
if rows > 1 {
@@ -176,7 +185,10 @@ func (f *File) SetCellFormula(sheet, axis, formula string) {
176185
xlsx := f.workSheetReader(sheet)
177186
axis = f.mergeCellsParser(xlsx, axis)
178187
col := string(strings.Map(letterOnlyMapF, axis))
179-
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
188+
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
189+
if err != nil {
190+
return
191+
}
180192
xAxis := row - 1
181193
yAxis := TitleToNumber(col)
182194

@@ -326,7 +338,10 @@ func (f *File) SetCellInt(sheet, axis string, value int) {
326338
xlsx := f.workSheetReader(sheet)
327339
axis = f.mergeCellsParser(xlsx, axis)
328340
col := string(strings.Map(letterOnlyMapF, axis))
329-
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
341+
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
342+
if err != nil {
343+
return
344+
}
330345
xAxis := row - 1
331346
yAxis := TitleToNumber(col)
332347

@@ -363,7 +378,10 @@ func (f *File) SetCellStr(sheet, axis, value string) {
363378
value = value[0:32767]
364379
}
365380
col := string(strings.Map(letterOnlyMapF, axis))
366-
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
381+
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
382+
if err != nil {
383+
return
384+
}
367385
xAxis := row - 1
368386
yAxis := TitleToNumber(col)
369387

@@ -393,7 +411,10 @@ func (f *File) SetCellDefault(sheet, axis, value string) {
393411
xlsx := f.workSheetReader(sheet)
394412
axis = f.mergeCellsParser(xlsx, axis)
395413
col := string(strings.Map(letterOnlyMapF, axis))
396-
row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis))
414+
row, err := strconv.Atoi(strings.Map(intOnlyMapF, axis))
415+
if err != nil {
416+
return
417+
}
397418
xAxis := row - 1
398419
yAxis := TitleToNumber(col)
399420

Diff for: excelize_test.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ func TestOpenFile(t *testing.T) {
3131
xlsx.UpdateLinkedValue()
3232
xlsx.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(float64(100.1588), 'f', -1, 32))
3333
xlsx.SetCellDefault("Sheet2", "A1", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64))
34+
// Test set cell value with illegal row number.
35+
xlsx.SetCellDefault("Sheet2", "A", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64))
3436
xlsx.SetCellInt("Sheet2", "A1", 100)
37+
// Test set cell integer value with illegal row number.
38+
xlsx.SetCellInt("Sheet2", "A", 100)
3539
xlsx.SetCellStr("Sheet2", "C11", "Knowns")
36-
3740
// Test max characters in a cell.
3841
xlsx.SetCellStr("Sheet2", "D11", strings.Repeat("c", 32769))
3942
xlsx.NewSheet(":\\/?*[]Maximum 31 characters allowed in sheet title.")
@@ -42,12 +45,19 @@ func TestOpenFile(t *testing.T) {
4245
xlsx.SetCellInt("Sheet3", "A23", 10)
4346
xlsx.SetCellStr("Sheet3", "b230", "10")
4447
xlsx.SetCellStr("Sheet10", "b230", "10")
48+
// Test set cell string value with illegal row number.
49+
xlsx.SetCellStr("Sheet10", "A", "10")
4550
xlsx.SetActiveSheet(2)
46-
xlsx.GetCellFormula("Sheet1", "B19") // Test get cell formula with given rows number.
47-
xlsx.GetCellFormula("Sheet2", "B20") // Test get cell formula with illegal worksheet index.
48-
xlsx.GetCellFormula("Sheet1", "B20") // Test get cell formula with illegal rows number.
51+
// Test get cell formula with given rows number.
52+
xlsx.GetCellFormula("Sheet1", "B19")
53+
// Test get cell formula with illegal worksheet index.
54+
xlsx.GetCellFormula("Sheet2", "B20")
55+
// Test get cell formula with illegal rows number.
56+
xlsx.GetCellFormula("Sheet1", "B20")
57+
xlsx.GetCellFormula("Sheet1", "B")
4958
// Test read cell value with given illegal rows number.
5059
xlsx.GetCellValue("Sheet2", "a-1")
60+
xlsx.GetCellValue("Sheet2", "A")
5161
// Test read cell value with given lowercase column number.
5262
xlsx.GetCellValue("Sheet2", "a5")
5363
xlsx.GetCellValue("Sheet2", "C11")
@@ -245,6 +255,8 @@ func TestSetCellFormula(t *testing.T) {
245255
}
246256
xlsx.SetCellFormula("Sheet1", "B19", "SUM(Sheet2!D2,Sheet2!D11)")
247257
xlsx.SetCellFormula("Sheet1", "C19", "SUM(Sheet2!D2,Sheet2!D9)")
258+
// Test set cell formula with illegal rows number.
259+
xlsx.SetCellFormula("Sheet1", "C", "SUM(Sheet2!D2,Sheet2!D9)")
248260
err = xlsx.Save()
249261
if err != nil {
250262
t.Log(err)
@@ -315,6 +327,11 @@ func TestSetCellStyleAlignment(t *testing.T) {
315327
t.Log(err)
316328
}
317329
xlsx.SetCellStyle("Sheet1", "A22", "A22", style)
330+
// Test set cell style with given illegal rows number.
331+
xlsx.SetCellStyle("Sheet1", "A", "A22", style)
332+
xlsx.SetCellStyle("Sheet1", "A22", "A", style)
333+
// Test get cell style with given illegal rows number.
334+
xlsx.GetCellStyle("Sheet1", "A")
318335
err = xlsx.Save()
319336
if err != nil {
320337
t.Log(err)

Diff for: styles.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -2279,12 +2279,18 @@ func (f *File) SetCellStyle(sheet, hcell, vcell string, styleID int) {
22792279

22802280
// Coordinate conversion, convert C1:B3 to 2,0,1,2.
22812281
hcol := string(strings.Map(letterOnlyMapF, hcell))
2282-
hrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
2282+
hrow, err := strconv.Atoi(strings.Map(intOnlyMapF, hcell))
2283+
if err != nil {
2284+
return
2285+
}
22832286
hyAxis := hrow - 1
22842287
hxAxis := TitleToNumber(hcol)
22852288

22862289
vcol := string(strings.Map(letterOnlyMapF, vcell))
2287-
vrow, _ := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
2290+
vrow, err := strconv.Atoi(strings.Map(intOnlyMapF, vcell))
2291+
if err != nil {
2292+
return
2293+
}
22882294
vyAxis := vrow - 1
22892295
vxAxis := TitleToNumber(vcol)
22902296

0 commit comments

Comments
 (0)