From f7bd0729c65fc82305328f7ac8fbaf329d1075c0 Mon Sep 17 00:00:00 2001 From: xuri Date: Wed, 1 Jul 2020 22:41:29 +0800 Subject: [PATCH] Resolve #32, fix missing leading/leading spaces when working with SST --- cell.go | 25 +++++++++++++------------ rows.go | 4 ++-- xmlSharedStrings.go | 9 ++++++--- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/cell.go b/cell.go index fa46007..0163c3b 100644 --- a/cell.go +++ b/cell.go @@ -15,7 +15,6 @@ import ( "encoding/xml" "errors" "fmt" - "html" "reflect" "strconv" "strings" @@ -274,23 +273,16 @@ func (f *File) SetCellStr(sheet, axis, value string) error { return err } cellData.S = f.prepareCellStyle(xlsx, col, cellData.S) - cellData.T, cellData.V, cellData.XMLSpace = f.setCellString(value) + cellData.T, cellData.V = f.setCellString(value) return err } // setCellString provides a function to set string type to shared string // table. -func (f *File) setCellString(value string) (t string, v string, ns xml.Attr) { +func (f *File) setCellString(value string) (t string, v string) { if len(value) > TotalCellChars { value = value[0:TotalCellChars] } - // Leading and ending space(s) character detection. - if len(value) > 0 && (value[0] == 32 || value[len(value)-1] == 32) { - ns = xml.Attr{ - Name: xml.Name{Space: NameSpaceXML, Local: "space"}, - Value: "preserve", - } - } t = "s" v = strconv.Itoa(f.setSharedString(value)) return @@ -304,7 +296,16 @@ func (f *File) setSharedString(val string) int { } sst.Count++ sst.UniqueCount++ - sst.SI = append(sst.SI, xlsxSI{T: val}) + t := xlsxT{Val: val} + // Leading and ending space(s) character detection. + if len(val) > 0 && (val[0] == 32 || val[len(val)-1] == 32) { + ns := xml.Attr{ + Name: xml.Name{Space: NameSpaceXML, Local: "space"}, + Value: "preserve", + } + t.Space = ns + } + sst.SI = append(sst.SI, xlsxSI{T: &t}) f.sharedStringsMap[val] = sst.UniqueCount - 1 return sst.UniqueCount - 1 } @@ -620,7 +621,7 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error { sst := f.sharedStringsReader() textRuns := []xlsxR{} for _, textRun := range runs { - run := xlsxR{T: &xlsxT{Val: html.EscapeString(textRun.Text)}} + run := xlsxR{T: &xlsxT{Val: textRun.Text}} if strings.ContainsAny(textRun.Text, "\r\n ") { run.T.Space = xml.Attr{Name: xml.Name{Space: NameSpaceXML, Local: "space"}, Value: "preserve"} } diff --git a/rows.go b/rows.go index b89d916..392cc5d 100644 --- a/rows.go +++ b/rows.go @@ -292,8 +292,8 @@ func (f *File) sharedStringsReader() *xlsxSST { } f.SharedStrings = &sharedStrings for i := range sharedStrings.SI { - if sharedStrings.SI[i].T != "" { - f.sharedStringsMap[sharedStrings.SI[i].T] = i + if sharedStrings.SI[i].T != nil { + f.sharedStringsMap[sharedStrings.SI[i].T.Val] = i } } f.addContentTypePart(0, "sharedStrings") diff --git a/xmlSharedStrings.go b/xmlSharedStrings.go index eac1672..d5fe4a7 100644 --- a/xmlSharedStrings.go +++ b/xmlSharedStrings.go @@ -38,7 +38,7 @@ type xlsxSST struct { // level - then the string item shall consist of multiple rich text runs which // collectively are used to express the string. type xlsxSI struct { - T string `xml:"t,omitempty"` + T *xlsxT `xml:"t,omitempty"` R []xlsxR `xml:"r"` } @@ -53,7 +53,10 @@ func (x xlsxSI) String() string { } return rows.String() } - return x.T + if x.T != nil { + return x.T.Val + } + return "" } // xlsxR represents a run of rich text. A rich text run is a region of text @@ -69,7 +72,7 @@ type xlsxR struct { type xlsxT struct { XMLName xml.Name `xml:"t"` Space xml.Attr `xml:"space,attr,omitempty"` - Val string `xml:",innerxml"` + Val string `xml:",chardata"` } // xlsxRPr (Run Properties) specifies a set of run properties which shall be