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

nil pointer dereference when writing file #638

Closed
fabiang opened this issue Oct 22, 2020 · 3 comments · Fixed by #643
Closed

nil pointer dereference when writing file #638

fabiang opened this issue Oct 22, 2020 · 3 comments · Fixed by #643
Assignees
Milestone

Comments

@fabiang
Copy link

fabiang commented Oct 22, 2020

With the following code:

excelFile, err := xlsx.OpenFile(filename)
if err != nil {
    return
}

excelSheet, ok := excelFile.Sheet[sheetName]
if !ok {
	err = fmt.Errorf("Sheet '%s' does not exist in file '%s'", sheetName, excelConfig.Template)
	return
}

excelSheet.Close()

var b bytes.Buffer
writer := bufio.NewWriter(&b)
excelFile.Write(writer)
content = b.Bytes()

return

I've got a nil pointer dereference:

*/vendor/github.com/tealeg/xlsx.(*File).MarshallParts(0xc000410680, 0xc00037b260, 0x1, 0x40)
	*/vendor/github.com/tealeg/xlsx/file.go:445 +0x31c
*/vendor/github.com/tealeg/xlsx.(*File).Write(0xc000410680, 0xce4aa0, 0xc000496280, 0xc0003a7000, 0xc000372578)
	*/vendor/github.com/tealeg/xlsx/file.go:158 +0xf8

The erroneous Excel file:
template.xlsx

@fabiang fabiang changed the title nil pointer dereference nil pointer dereference when writing file Oct 22, 2020
@Dexus
Copy link

Dexus commented Nov 8, 2020

I have the same Problem
I found out that the

sheet.cellStore is nil on an opened file.

But I'm not sure how to fix it. Looks like the are some more bugs in the "Save/Write" function and the other related functions.

@Dexus
Copy link

Dexus commented Nov 8, 2020

cc @tealeg

can you please have a look?

@tealeg
Copy link
Owner

tealeg commented Nov 14, 2020

This isn't a bug per se - you're calling Close on the sheet before trying to write it's output. Writing implies traversing all the cells in the sheet, and with the cellstore closed there's nothing to traverse.

I've taken two actions in a branch I'll land shortly:

    1. give a more meaningful panic message when you attempt to use a closed sheet
    1. update the documentation on the Close function to make it clearer when it's valid to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants