diff --git a/cell.go b/cell.go index 1a2ef8f029..80853eec94 100644 --- a/cell.go +++ b/cell.go @@ -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. diff --git a/cell_test.go b/cell_test.go index c41bf58410..c3a622ede8 100644 --- a/cell_test.go +++ b/cell_test.go @@ -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(`aa`, 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) {