Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetCellRichText strconv.Atoi error because of null value #1999

Closed
liuwangchao opened this issue Sep 26, 2024 · 0 comments · Fixed by #2000
Closed

GetCellRichText strconv.Atoi error because of null value #1999

liuwangchao opened this issue Sep 26, 2024 · 0 comments · Fixed by #2000
Labels
bug Something isn't working

Comments

@liuwangchao
Copy link
Contributor

liuwangchao commented Sep 26, 2024

Description

大佬,我这有一个Excel文件:xml文件.zip
其中F29和G29单元格,格式为t="s",但是没有value:

-<row spans="1:7" r="29">
    <c r="A29" t="s" s="11"/>
    -<c r="B29" t="s" s="18">
        <v>103</v>
    </c>
    -<c r="C29" t="s" s="18">
        <v>104</v>
    </c>
    -<c r="D29" t="s" s="12">
        <v>105</v>
    </c>
    -<c r="E29" t="s" s="16">
        <v>106</v>
    </c>
    <c r="F29" t="s" s="16"/>
    <c r="G29" t="s" s="16"/>
</row>

这种异常情况在执行GetCellRichText方法时,

// GetCellRichText provides a function to get rich text of cell by given
// worksheet.
func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err error) {
	ws, err := f.workSheetReader(sheet)
	if err != nil {
		return
	}
	c, _, _, err := ws.prepareCell(cell)
	if err != nil {
		return
	}
	if c.T == "inlineStr" && c.IS != nil {
		runs = getCellRichText(c.IS)
		return
	}
	if c.T == "" {
		return
	}
	siIdx, err := strconv.Atoi(c.V)
	if err != nil || c.T != "s" {
		return
	}
	sst, err := f.sharedStringsReader()
	if err != nil {
		return
	}
	if len(sst.SI) <= siIdx || siIdx < 0 {
		return
	}
	runs = getCellRichText(&sst.SI[siIdx])
	return
}

会由于strconv.Atoi(c.V)传入了空的c.V,导致报错strconv.Atoi: parsing "": invalid syntax
这边建议是在执行strconv.Atoi(c.V)前补充判断

if c.T == "" {
	return
}
siIdx, err := strconv.Atoi(c.V)
	if err != nil || c.T != "s" {
		return
}

改为

if c.T != "s" {
	return
}
if c.V == "" {
	return
}
siIdx, err := strconv.Atoi(c.V)
	if err != nil {
		return
}

修改完的完整方法如下:

// GetCellRichText provides a function to get rich text of cell by given
// worksheet.
func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err error) {
	ws, err := f.workSheetReader(sheet)
	if err != nil {
		return
	}
	c, _, _, err := ws.prepareCell(cell)
	if err != nil {
		return
	}
	if c.T == "inlineStr" && c.IS != nil {
		runs = getCellRichText(c.IS)
		return
	}
	if c.T != "s" {
		return
	}
	if c.V == "" {
		return
	}
	siIdx, err := strconv.Atoi(c.V)
	if err != nil {
		return
	}
	sst, err := f.sharedStringsReader()
	if err != nil {
		return
	}
	if len(sst.SI) <= siIdx || siIdx < 0 {
		return
	}
	runs = getCellRichText(&sst.SI[siIdx])
	return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Status: Bugfix
2 participants