Skip to content

Commit

Permalink
Merge pull request #819 from tealeg/issue-809
Browse files Browse the repository at this point in the history
Fixed issue #809, losing rows from cellstore when moving backwards
  • Loading branch information
tealeg authored Sep 14, 2024
2 parents 9590cda + 64f30db commit 7ebd282
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
18 changes: 11 additions & 7 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
49 changes: 48 additions & 1 deletion sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

}

0 comments on commit 7ebd282

Please sign in to comment.