Skip to content

Commit f7bd072

Browse files
committed
Resolve qax-os#32, fix missing leading/leading spaces when working with SST
1 parent 1cbb05d commit f7bd072

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

cell.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"encoding/xml"
1616
"errors"
1717
"fmt"
18-
"html"
1918
"reflect"
2019
"strconv"
2120
"strings"
@@ -274,23 +273,16 @@ func (f *File) SetCellStr(sheet, axis, value string) error {
274273
return err
275274
}
276275
cellData.S = f.prepareCellStyle(xlsx, col, cellData.S)
277-
cellData.T, cellData.V, cellData.XMLSpace = f.setCellString(value)
276+
cellData.T, cellData.V = f.setCellString(value)
278277
return err
279278
}
280279

281280
// setCellString provides a function to set string type to shared string
282281
// table.
283-
func (f *File) setCellString(value string) (t string, v string, ns xml.Attr) {
282+
func (f *File) setCellString(value string) (t string, v string) {
284283
if len(value) > TotalCellChars {
285284
value = value[0:TotalCellChars]
286285
}
287-
// Leading and ending space(s) character detection.
288-
if len(value) > 0 && (value[0] == 32 || value[len(value)-1] == 32) {
289-
ns = xml.Attr{
290-
Name: xml.Name{Space: NameSpaceXML, Local: "space"},
291-
Value: "preserve",
292-
}
293-
}
294286
t = "s"
295287
v = strconv.Itoa(f.setSharedString(value))
296288
return
@@ -304,7 +296,16 @@ func (f *File) setSharedString(val string) int {
304296
}
305297
sst.Count++
306298
sst.UniqueCount++
307-
sst.SI = append(sst.SI, xlsxSI{T: val})
299+
t := xlsxT{Val: val}
300+
// Leading and ending space(s) character detection.
301+
if len(val) > 0 && (val[0] == 32 || val[len(val)-1] == 32) {
302+
ns := xml.Attr{
303+
Name: xml.Name{Space: NameSpaceXML, Local: "space"},
304+
Value: "preserve",
305+
}
306+
t.Space = ns
307+
}
308+
sst.SI = append(sst.SI, xlsxSI{T: &t})
308309
f.sharedStringsMap[val] = sst.UniqueCount - 1
309310
return sst.UniqueCount - 1
310311
}
@@ -620,7 +621,7 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error {
620621
sst := f.sharedStringsReader()
621622
textRuns := []xlsxR{}
622623
for _, textRun := range runs {
623-
run := xlsxR{T: &xlsxT{Val: html.EscapeString(textRun.Text)}}
624+
run := xlsxR{T: &xlsxT{Val: textRun.Text}}
624625
if strings.ContainsAny(textRun.Text, "\r\n ") {
625626
run.T.Space = xml.Attr{Name: xml.Name{Space: NameSpaceXML, Local: "space"}, Value: "preserve"}
626627
}

rows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ func (f *File) sharedStringsReader() *xlsxSST {
292292
}
293293
f.SharedStrings = &sharedStrings
294294
for i := range sharedStrings.SI {
295-
if sharedStrings.SI[i].T != "" {
296-
f.sharedStringsMap[sharedStrings.SI[i].T] = i
295+
if sharedStrings.SI[i].T != nil {
296+
f.sharedStringsMap[sharedStrings.SI[i].T.Val] = i
297297
}
298298
}
299299
f.addContentTypePart(0, "sharedStrings")

xmlSharedStrings.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type xlsxSST struct {
3838
// level - then the string item shall consist of multiple rich text runs which
3939
// collectively are used to express the string.
4040
type xlsxSI struct {
41-
T string `xml:"t,omitempty"`
41+
T *xlsxT `xml:"t,omitempty"`
4242
R []xlsxR `xml:"r"`
4343
}
4444

@@ -53,7 +53,10 @@ func (x xlsxSI) String() string {
5353
}
5454
return rows.String()
5555
}
56-
return x.T
56+
if x.T != nil {
57+
return x.T.Val
58+
}
59+
return ""
5760
}
5861

5962
// xlsxR represents a run of rich text. A rich text run is a region of text
@@ -69,7 +72,7 @@ type xlsxR struct {
6972
type xlsxT struct {
7073
XMLName xml.Name `xml:"t"`
7174
Space xml.Attr `xml:"space,attr,omitempty"`
72-
Val string `xml:",innerxml"`
75+
Val string `xml:",chardata"`
7376
}
7477

7578
// xlsxRPr (Run Properties) specifies a set of run properties which shall be

0 commit comments

Comments
 (0)