diff --git a/file.go b/file.go index 2cbf0a9c..986c5d36 100644 --- a/file.go +++ b/file.go @@ -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() diff --git a/write_test.go b/write_test.go index adfbec34..3160aa61 100644 --- a/write_test.go +++ b/write_test.go @@ -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) + }) +}