Skip to content

Commit bee487c

Browse files
committed
Leading space(s) character in cell value detection added. Related issue qax-os#32.
1 parent b625420 commit bee487c

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

excelize.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ func (f *File) SetCellStr(sheet, axis, value string) {
161161
completeRow(xlsx, rows, cell)
162162
completeCol(xlsx, rows, cell)
163163

164+
// Leading space(s) character detection.
165+
if len(value) > 0 {
166+
if value[0] == 32 {
167+
xlsx.SheetData.Row[xAxis].C[yAxis].XMLSpace = xml.Attr{
168+
Name: xml.Name{Space: NameSpaceXML, Local: "space"},
169+
Value: "preserve",
170+
}
171+
}
172+
}
164173
xlsx.SheetData.Row[xAxis].C[yAxis].T = "str"
165174
xlsx.SheetData.Row[xAxis].C[yAxis].V = value
166175
}

excelize_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestOpenFile(t *testing.T) {
5353
xlsx.GetCellValue("Sheet2", "D11")
5454
xlsx.GetCellValue("Sheet2", "D12")
5555
// Test SetCellValue function.
56-
xlsx.SetCellValue("Sheet2", "F1", "Hello")
56+
xlsx.SetCellValue("Sheet2", "F1", " Hello")
5757
xlsx.SetCellValue("Sheet2", "G1", []byte("World"))
5858
xlsx.SetCellValue("Sheet2", "F2", 42)
5959
xlsx.SetCellValue("Sheet2", "F2", int8(42))

xmlDrawing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const (
99
SourceRelationshipWorkSheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
1010
NameSpaceDrawingML = "http://schemas.openxmlformats.org/drawingml/2006/main"
1111
NameSpaceSpreadSheetDrawing = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
12+
NameSpaceXML = "http://www.w3.org/XML/1998/namespace"
1213
)
1314

1415
// xlsxCNvPr directly maps the cNvPr (Non-Visual Drawing Properties). This

xmlWorksheet.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,10 @@ type xlsxC struct {
302302
R string `xml:"r,attr"` // Cell ID, e.g. A1
303303
S int `xml:"s,attr,omitempty"` // Style reference.
304304
// Str string `xml:"str,attr,omitempty"` // Style reference.
305-
T string `xml:"t,attr,omitempty"` // Type.
306-
F *xlsxF `xml:"f,omitempty"` // Formula
307-
V string `xml:"v,omitempty"` // Value
305+
T string `xml:"t,attr,omitempty"` // Type.
306+
F *xlsxF `xml:"f,omitempty"` // Formula
307+
V string `xml:"v,omitempty"` // Value
308+
XMLSpace xml.Attr `xml:"space,attr,omitempty"`
308309
}
309310

310311
// xlsxF directly maps the f element in the namespace

0 commit comments

Comments
 (0)