Skip to content
This repository was archived by the owner on Aug 13, 2025. It is now read-only.

Commit 7ebd282

Browse files
authored
Merge pull request #819 from tealeg/issue-809
Fixed issue #809, losing rows from cellstore when moving backwards
2 parents 9590cda + 64f30db commit 7ebd282

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

sheet.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ type Sheet struct {
3030
cellStore CellStore
3131
currentRow *Row
3232
cellStoreName string // The first part of the key used in
33-
// the cellStore. This name is stable,
34-
// unlike the Name, which can change
33+
// the cellStore. This name is stable,
34+
// unlike the Name, which can change
3535
}
3636

3737
// NewSheet constructs a Sheet with the default CellStore and returns
@@ -47,8 +47,8 @@ func NewSheetWithCellStore(name string, constructor CellStoreConstructor) (*Shee
4747
return nil, fmt.Errorf("sheet name is invalid: %w", err)
4848
}
4949
sheet := &Sheet{
50-
Name: name,
51-
Cols: &ColStore{},
50+
Name: name,
51+
Cols: &ColStore{},
5252
cellStoreName: name,
5353
}
5454
var err error
@@ -298,10 +298,15 @@ func (s *Sheet) maybeAddRow(rowCount int) {
298298
// Make sure we always have as many Rows as we do cells.
299299
func (s *Sheet) Row(idx int) (*Row, error) {
300300
s.mustBeOpen()
301+
301302
s.maybeAddRow(idx + 1)
302-
if s.currentRow != nil && idx == s.currentRow.num {
303-
return s.currentRow, nil
303+
if s.currentRow != nil {
304+
if idx == s.currentRow.num {
305+
return s.currentRow, nil
306+
}
307+
s.cellStore.WriteRow(s.currentRow)
304308
}
309+
305310
r, err := s.cellStore.ReadRow(makeRowKey(s, idx), s)
306311
if err != nil {
307312
if _, ok := err.(*RowNotFoundError); !ok {
@@ -949,7 +954,6 @@ func handleNumFmtIdForXLSX(NumFmtId int, styles *xlsxStyleSheet) (XfId int) {
949954
return
950955
}
951956

952-
953957
func IsSaneSheetName(sheetName string) error {
954958
runeLength := utf8.RuneCountInString(sheetName)
955959
if runeLength > 31 || runeLength == 0 {

sheet_test.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,54 @@ func TestMakeXLSXSheet(t *testing.T) {
737737
})
738738
}
739739

740+
func TestIssue809(t *testing.T) {
741+
c := qt.New(t)
742+
743+
csRunO(c, "Issue809", func(c *qt.C, option FileOption) {
744+
outfile := filepath.Join(t.TempDir(), "bug809.xlsx")
745+
f := NewFile(option)
746+
sh, err := f.AddSheet("Sheet1")
747+
c.Assert(err, qt.IsNil)
748+
749+
// Write Colors (y-axis).
750+
colors := []string{"Red", "Green", "Blue"}
751+
for i, clr := range colors {
752+
cell, _ := sh.Cell(i+1, 0)
753+
cell.SetString(clr)
754+
}
755+
756+
// Write Numbers (x-axis).
757+
numbers := []string{"1", "2", "3"}
758+
for i, num := range numbers {
759+
cell, _ := sh.Cell(0, i+1)
760+
cell.SetString(num)
761+
}
762+
763+
c.Assert(f.Save(outfile), qt.IsNil)
764+
765+
f = nil
766+
767+
f2, err := OpenFile(outfile, option)
768+
c.Assert(err, qt.IsNil)
769+
770+
sh2, ok := f2.Sheet["Sheet1"]
771+
c.Assert(ok, qt.IsTrue)
772+
773+
for i, clr := range colors {
774+
cell, err := sh2.Cell(i+1, 0)
775+
c.Assert(err, qt.IsNil)
776+
t.Logf("%q == %q?\n", cell.String(), clr)
777+
c.Assert(cell.String(), qt.Equals, clr)
778+
}
779+
780+
for i, num := range numbers {
781+
cell, err := sh2.Cell(0, i+1)
782+
c.Assert(err, qt.IsNil)
783+
c.Assert(cell.String(), qt.Equals, num)
784+
}
785+
})
786+
}
787+
740788
func TestTemp(t *testing.T) {
741789
c := qt.New(t)
742790
option := UseDiskVCellStore
@@ -774,5 +822,4 @@ func TestTemp(t *testing.T) {
774822
xSI := xSST.SI[0]
775823
c.Assert(xSI.T.Text, qt.Equals, "A cell!")
776824
c.Assert(xSI.R, qt.HasLen, 0)
777-
778825
}

0 commit comments

Comments
 (0)