Skip to content

Commit

Permalink
Merge pull request #645 from tealeg/fix-issue-644
Browse files Browse the repository at this point in the history
Check currentRow is set on sheet before persisting it (#644)
  • Loading branch information
tealeg authored Nov 16, 2020
2 parents 6287849 + 84eaa90 commit b5282f0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
10 changes: 6 additions & 4 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,12 @@ func (f *File) MarshallParts(zipWriter *zip.Writer) error {
return wrap(err)
}
for _, sheet := range f.Sheets {
// Make sure we don't lose the current state!
err := sheet.cellStore.WriteRow(sheet.currentRow)
if err != nil {
return wrap(err)
if sheet.currentRow != nil {
// Make sure we don't lose the current state!
err := sheet.cellStore.WriteRow(sheet.currentRow)
if err != nil {
return wrap(err)
}
}

xSheetRels := sheet.makeXLSXSheetRelations()
Expand Down
30 changes: 30 additions & 0 deletions write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,33 @@ func TestBigWrite(t *testing.T) {
c.Assert(err, qt.Equals, nil)

}


func TestWriteFileWithUnvisitedSheets(t *testing.T) {
c := qt.New(t)

// Issue 644 occured because we were checking the currentRow
// on sheets that hadn't been visitied and thus had no current
// row set.
csRunO(c, "Test for panic", func(c *qt.C, opt FileOption) {
fileToOpen := filepath.Join("testdocs", "testfile.xlsx")
// open an existing file
wbFile, err := OpenFile(fileToOpen, opt)
c.Assert(err, qt.IsNil)

sheetName := "Tabelle1"
sh, ok := wbFile.Sheet[sheetName]
c.Assert(ok, qt.Equals, true)
colNum, rowNum, _ := GetCoordsFromCellIDString("DD3000")
cell, err := sh.Cell(rowNum, colNum)
c.Assert(err, qt.IsNil)
cell.SetInt64(39491)

testDir := c.Mkdir()
path := filepath.Join(testDir, "test.xlsx")

// With issue 644 this line would panic
err = wbFile.Save(path)
c.Assert(err, qt.IsNil)
})
}

0 comments on commit b5282f0

Please sign in to comment.