forked from p30928647/excelize
Resolve #32, fix missing leading/leading spaces when working with SST
This commit is contained in:
parent
1cbb05d497
commit
f7bd0729c6
25
cell.go
25
cell.go
|
@ -15,7 +15,6 @@ import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -274,23 +273,16 @@ func (f *File) SetCellStr(sheet, axis, value string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cellData.S = f.prepareCellStyle(xlsx, col, cellData.S)
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// setCellString provides a function to set string type to shared string
|
// setCellString provides a function to set string type to shared string
|
||||||
// table.
|
// 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 {
|
if len(value) > TotalCellChars {
|
||||||
value = value[0: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"
|
t = "s"
|
||||||
v = strconv.Itoa(f.setSharedString(value))
|
v = strconv.Itoa(f.setSharedString(value))
|
||||||
return
|
return
|
||||||
|
@ -304,7 +296,16 @@ func (f *File) setSharedString(val string) int {
|
||||||
}
|
}
|
||||||
sst.Count++
|
sst.Count++
|
||||||
sst.UniqueCount++
|
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
|
f.sharedStringsMap[val] = sst.UniqueCount - 1
|
||||||
return sst.UniqueCount - 1
|
return sst.UniqueCount - 1
|
||||||
}
|
}
|
||||||
|
@ -620,7 +621,7 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error {
|
||||||
sst := f.sharedStringsReader()
|
sst := f.sharedStringsReader()
|
||||||
textRuns := []xlsxR{}
|
textRuns := []xlsxR{}
|
||||||
for _, textRun := range runs {
|
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 ") {
|
if strings.ContainsAny(textRun.Text, "\r\n ") {
|
||||||
run.T.Space = xml.Attr{Name: xml.Name{Space: NameSpaceXML, Local: "space"}, Value: "preserve"}
|
run.T.Space = xml.Attr{Name: xml.Name{Space: NameSpaceXML, Local: "space"}, Value: "preserve"}
|
||||||
}
|
}
|
||||||
|
|
4
rows.go
4
rows.go
|
@ -292,8 +292,8 @@ func (f *File) sharedStringsReader() *xlsxSST {
|
||||||
}
|
}
|
||||||
f.SharedStrings = &sharedStrings
|
f.SharedStrings = &sharedStrings
|
||||||
for i := range sharedStrings.SI {
|
for i := range sharedStrings.SI {
|
||||||
if sharedStrings.SI[i].T != "" {
|
if sharedStrings.SI[i].T != nil {
|
||||||
f.sharedStringsMap[sharedStrings.SI[i].T] = i
|
f.sharedStringsMap[sharedStrings.SI[i].T.Val] = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
f.addContentTypePart(0, "sharedStrings")
|
f.addContentTypePart(0, "sharedStrings")
|
||||||
|
|
|
@ -38,7 +38,7 @@ type xlsxSST struct {
|
||||||
// level - then the string item shall consist of multiple rich text runs which
|
// level - then the string item shall consist of multiple rich text runs which
|
||||||
// collectively are used to express the string.
|
// collectively are used to express the string.
|
||||||
type xlsxSI struct {
|
type xlsxSI struct {
|
||||||
T string `xml:"t,omitempty"`
|
T *xlsxT `xml:"t,omitempty"`
|
||||||
R []xlsxR `xml:"r"`
|
R []xlsxR `xml:"r"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,10 @@ func (x xlsxSI) String() string {
|
||||||
}
|
}
|
||||||
return rows.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
|
// 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 {
|
type xlsxT struct {
|
||||||
XMLName xml.Name `xml:"t"`
|
XMLName xml.Name `xml:"t"`
|
||||||
Space xml.Attr `xml:"space,attr,omitempty"`
|
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
|
// xlsxRPr (Run Properties) specifies a set of run properties which shall be
|
||||||
|
|
Loading…
Reference in New Issue