diff --git a/sheet.go b/sheet.go index de0ffe22..c07b6db5 100644 --- a/sheet.go +++ b/sheet.go @@ -30,8 +30,8 @@ type Sheet struct { cellStore CellStore currentRow *Row cellStoreName string // The first part of the key used in - // the cellStore. This name is stable, - // unlike the Name, which can change + // the cellStore. This name is stable, + // unlike the Name, which can change } // NewSheet constructs a Sheet with the default CellStore and returns @@ -47,8 +47,8 @@ func NewSheetWithCellStore(name string, constructor CellStoreConstructor) (*Shee return nil, fmt.Errorf("sheet name is invalid: %w", err) } sheet := &Sheet{ - Name: name, - Cols: &ColStore{}, + Name: name, + Cols: &ColStore{}, cellStoreName: name, } var err error @@ -298,10 +298,15 @@ func (s *Sheet) maybeAddRow(rowCount int) { // Make sure we always have as many Rows as we do cells. func (s *Sheet) Row(idx int) (*Row, error) { s.mustBeOpen() + s.maybeAddRow(idx + 1) - if s.currentRow != nil && idx == s.currentRow.num { - return s.currentRow, nil + if s.currentRow != nil { + if idx == s.currentRow.num { + return s.currentRow, nil + } + s.cellStore.WriteRow(s.currentRow) } + r, err := s.cellStore.ReadRow(makeRowKey(s, idx), s) if err != nil { if _, ok := err.(*RowNotFoundError); !ok { @@ -949,7 +954,6 @@ func handleNumFmtIdForXLSX(NumFmtId int, styles *xlsxStyleSheet) (XfId int) { return } - func IsSaneSheetName(sheetName string) error { runeLength := utf8.RuneCountInString(sheetName) if runeLength > 31 || runeLength == 0 { diff --git a/sheet_test.go b/sheet_test.go index e7223f6d..39c4e109 100644 --- a/sheet_test.go +++ b/sheet_test.go @@ -737,6 +737,54 @@ func TestMakeXLSXSheet(t *testing.T) { }) } +func TestIssue809(t *testing.T) { + c := qt.New(t) + + csRunO(c, "Issue809", func(c *qt.C, option FileOption) { + outfile := filepath.Join(t.TempDir(), "bug809.xlsx") + f := NewFile(option) + sh, err := f.AddSheet("Sheet1") + c.Assert(err, qt.IsNil) + + // Write Colors (y-axis). + colors := []string{"Red", "Green", "Blue"} + for i, clr := range colors { + cell, _ := sh.Cell(i+1, 0) + cell.SetString(clr) + } + + // Write Numbers (x-axis). + numbers := []string{"1", "2", "3"} + for i, num := range numbers { + cell, _ := sh.Cell(0, i+1) + cell.SetString(num) + } + + c.Assert(f.Save(outfile), qt.IsNil) + + f = nil + + f2, err := OpenFile(outfile, option) + c.Assert(err, qt.IsNil) + + sh2, ok := f2.Sheet["Sheet1"] + c.Assert(ok, qt.IsTrue) + + for i, clr := range colors { + cell, err := sh2.Cell(i+1, 0) + c.Assert(err, qt.IsNil) + t.Logf("%q == %q?\n", cell.String(), clr) + c.Assert(cell.String(), qt.Equals, clr) + } + + for i, num := range numbers { + cell, err := sh2.Cell(0, i+1) + c.Assert(err, qt.IsNil) + c.Assert(cell.String(), qt.Equals, num) + } + }) +} + func TestTemp(t *testing.T) { c := qt.New(t) option := UseDiskVCellStore @@ -774,5 +822,4 @@ func TestTemp(t *testing.T) { xSI := xSST.SI[0] c.Assert(xSI.T.Text, qt.Equals, "A cell!") c.Assert(xSI.R, qt.HasLen, 0) - }