Skip to content

Commit

Permalink
Recalculate and use the same shared string count and unique count to …
Browse files Browse the repository at this point in the history
…overwrite incorrect existing values
  • Loading branch information
xuri committed Nov 22, 2023
1 parent 6251d49 commit 41259b4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,13 @@ func (f *File) setSharedString(val string) (int, error) {
}
sst.mu.Lock()
defer sst.mu.Unlock()
sst.Count++
sst.UniqueCount++
t := xlsxT{Val: val}
val, t.Space = trimCellValue(val, false)
sst.SI = append(sst.SI, xlsxSI{T: &t})
sst.Count = len(sst.SI)
sst.UniqueCount = sst.Count
f.sharedStringsMap[val] = sst.UniqueCount - 1
return len(sst.SI) - 1, nil
return sst.UniqueCount - 1, nil
}

// trimCellValue provides a function to set string type to cell.
Expand Down
17 changes: 17 additions & 0 deletions cell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,23 @@ func TestSetCellValue(t *testing.T) {
f.WorkBook = nil
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
assert.EqualError(t, f.SetCellValue("Sheet1", "A1", time.Now().UTC()), "XML syntax error on line 1: invalid UTF-8")
// Test set cell value with the shared string table's count not equal with unique count
f = NewFile()
f.SharedStrings = nil
f.Pkg.Store(defaultXMLPathSharedStrings, []byte(fmt.Sprintf(`<sst xmlns="%s" count="2" uniqueCount="1"><si><t>a</t></si><si><t>a</t></si></sst>`, NameSpaceSpreadSheet.Value)))
f.Sheet.Store("xl/worksheets/sheet1.xml", &xlsxWorksheet{
SheetData: xlsxSheetData{Row: []xlsxRow{
{R: intPtr(1), C: []xlsxC{{R: "A1", T: "str", V: "1"}}},
}},
})
assert.NoError(t, f.SetCellValue("Sheet1", "A1", "b"))
val, err := f.GetCellValue("Sheet1", "A1")
assert.NoError(t, err)
assert.Equal(t, "b", val)
assert.NoError(t, f.SetCellValue("Sheet1", "B1", "b"))
val, err = f.GetCellValue("Sheet1", "B1")
assert.NoError(t, err)
assert.Equal(t, "b", val)
}

func TestSetCellValues(t *testing.T) {
Expand Down

0 comments on commit 41259b4

Please sign in to comment.