Skip to content

Commit 65fc25e

Browse files
committed
Ref qax-os#1533, this made number format text handler just handle text tokens
- Fix race conditions for concurrency read and write shared string table - Unit tests has been updated
1 parent 612f6f1 commit 65fc25e

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

Diff for: cell.go

+4
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,8 @@ func (f *File) setSharedString(val string) (int, error) {
460460
if i, ok := f.sharedStringsMap[val]; ok {
461461
return i, nil
462462
}
463+
sst.mu.Lock()
464+
defer sst.mu.Unlock()
463465
sst.Count++
464466
sst.UniqueCount++
465467
t := xlsxT{Val: val}
@@ -581,6 +583,8 @@ func (c *xlsxC) getValueFrom(f *File, d *xlsxSST, raw bool) (string, error) {
581583
if _, ok := f.tempFiles.Load(defaultXMLPathSharedStrings); ok {
582584
return f.formattedValue(&xlsxC{S: c.S, V: f.getFromStringItem(xlsxSI)}, raw, CellTypeSharedString)
583585
}
586+
d.mu.Lock()
587+
defer d.mu.Unlock()
584588
if len(d.SI) > xlsxSI {
585589
return f.formattedValue(&xlsxC{S: c.S, V: d.SI[xlsxSI].String()}, raw, CellTypeSharedString)
586590
}

Diff for: numfmt.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -948,13 +948,10 @@ func (nf *numberFormat) zeroHandler() string {
948948
// textHandler will be handling text selection for a number format expression.
949949
func (nf *numberFormat) textHandler() (result string) {
950950
for _, token := range nf.section[nf.sectionIdx].Items {
951-
if inStrSlice([]string{nfp.TokenTypeDateTimes, nfp.TokenTypeElapsedDateTimes}, token.TType, false) != -1 {
952-
return nf.value
953-
}
954951
if token.TType == nfp.TokenTypeLiteral {
955952
result += token.TValue
956953
}
957-
if token.TType == nfp.TokenTypeGeneral || token.TType == nfp.TokenTypeTextPlaceHolder || token.TType == nfp.TokenTypeZeroPlaceHolder {
954+
if token.TType == nfp.TokenTypeTextPlaceHolder || token.TType == nfp.TokenTypeZeroPlaceHolder {
958955
result += nf.value
959956
}
960957
}
@@ -964,6 +961,9 @@ func (nf *numberFormat) textHandler() (result string) {
964961
// getValueSectionType returns its applicable number format expression section
965962
// based on the given value.
966963
func (nf *numberFormat) getValueSectionType(value string) (float64, string) {
964+
if nf.cellType != CellTypeNumber && nf.cellType != CellTypeDate {
965+
return 0, nfp.TokenSectionText
966+
}
967967
isNum, _, _ := isNumeric(value)
968968
if !isNum {
969969
return 0, nfp.TokenSectionText

Diff for: numfmt_test.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -1008,12 +1008,17 @@ func TestNumFmt(t *testing.T) {
10081008
result := format(item[0], item[1], false, CellTypeNumber)
10091009
assert.Equal(t, item[2], result, item)
10101010
}
1011-
for _, item := range [][]string{
1012-
{"1234.5678", "General", "1234.5678"},
1013-
{"1234.5678", "yyyy\"\"m\"\"d\"\";@", "1234.5678"},
1014-
{"1234.5678", "0_);[Red]\\(0\\)", "1234.5678"},
1015-
} {
1016-
result := format(item[0], item[1], false, CellTypeSharedString)
1017-
assert.Equal(t, item[2], result, item)
1011+
for _, cellType := range []CellType{CellTypeSharedString, CellTypeInlineString} {
1012+
for _, item := range [][]string{
1013+
{"1234.5678", "General", "1234.5678"},
1014+
{"1234.5678", "yyyy\"\"m\"\"d\"\";@", "1234.5678"},
1015+
{"1234.5678", "h\"\"mm\"\"ss\"\";@", "1234.5678"},
1016+
{"1234.5678", "\"¥\"#,##0.00_);\\(\"¥\"#,##0.00\\)", "1234.5678"},
1017+
{"1234.5678", "0_);[Red]\\(0\\)", "1234.5678"},
1018+
{"1234.5678", "\"text\"@", "text1234.5678"},
1019+
} {
1020+
result := format(item[0], item[1], false, cellType)
1021+
assert.Equal(t, item[2], result, item)
1022+
}
10181023
}
10191024
}

Diff for: xmlSharedStrings.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111

1212
package excelize
1313

14-
import "encoding/xml"
14+
import (
15+
"encoding/xml"
16+
"sync"
17+
)
1518

1619
// xlsxSST directly maps the sst element from the namespace
1720
// http://schemas.openxmlformats.org/spreadsheetml/2006/main. String values may
@@ -21,6 +24,7 @@ import "encoding/xml"
2124
// is an indexed list of string values, shared across the workbook, which allows
2225
// implementations to store values only once.
2326
type xlsxSST struct {
27+
mu sync.Mutex
2428
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main sst"`
2529
Count int `xml:"count,attr"`
2630
UniqueCount int `xml:"uniqueCount,attr"`

0 commit comments

Comments
 (0)