Recalculate and use the same shared string count and unique count to overwrite incorrect existing values

This commit is contained in:
xuri 2023-11-23 00:03:10 +08:00
parent 6251d493b3
commit 41259b474f
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
2 changed files with 20 additions and 3 deletions

View File

@ -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.

View File

@ -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) {