Browse Source

Leading space(s) character in cell value detection added. Related issue #32.

Ri Xu 8 years ago
parent
commit
bee487c445
4 changed files with 15 additions and 4 deletions
  1. 9 0
      excelize.go
  2. 1 1
      excelize_test.go
  3. 1 0
      xmlDrawing.go
  4. 4 3
      xmlWorksheet.go

+ 9 - 0
excelize.go

@@ -161,6 +161,15 @@ func (f *File) SetCellStr(sheet, axis, value string) {
 	completeRow(xlsx, rows, cell)
 	completeCol(xlsx, rows, cell)
 
+	// Leading space(s) character detection.
+	if len(value) > 0 {
+		if value[0] == 32 {
+			xlsx.SheetData.Row[xAxis].C[yAxis].XMLSpace = xml.Attr{
+				Name:  xml.Name{Space: NameSpaceXML, Local: "space"},
+				Value: "preserve",
+			}
+		}
+	}
 	xlsx.SheetData.Row[xAxis].C[yAxis].T = "str"
 	xlsx.SheetData.Row[xAxis].C[yAxis].V = value
 }

+ 1 - 1
excelize_test.go

@@ -53,7 +53,7 @@ func TestOpenFile(t *testing.T) {
 	xlsx.GetCellValue("Sheet2", "D11")
 	xlsx.GetCellValue("Sheet2", "D12")
 	// Test SetCellValue function.
-	xlsx.SetCellValue("Sheet2", "F1", "Hello")
+	xlsx.SetCellValue("Sheet2", "F1", " Hello")
 	xlsx.SetCellValue("Sheet2", "G1", []byte("World"))
 	xlsx.SetCellValue("Sheet2", "F2", 42)
 	xlsx.SetCellValue("Sheet2", "F2", int8(42))

+ 1 - 0
xmlDrawing.go

@@ -9,6 +9,7 @@ const (
 	SourceRelationshipWorkSheet = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"
 	NameSpaceDrawingML          = "http://schemas.openxmlformats.org/drawingml/2006/main"
 	NameSpaceSpreadSheetDrawing = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
+	NameSpaceXML                = "http://www.w3.org/XML/1998/namespace"
 )
 
 // xlsxCNvPr directly maps the cNvPr (Non-Visual Drawing Properties). This

+ 4 - 3
xmlWorksheet.go

@@ -302,9 +302,10 @@ type xlsxC struct {
 	R string `xml:"r,attr"`           // Cell ID, e.g. A1
 	S int    `xml:"s,attr,omitempty"` // Style reference.
 	// Str string `xml:"str,attr,omitempty"` // Style reference.
-	T string `xml:"t,attr,omitempty"` // Type.
-	F *xlsxF `xml:"f,omitempty"`      // Formula
-	V string `xml:"v,omitempty"`      // Value
+	T        string   `xml:"t,attr,omitempty"` // Type.
+	F        *xlsxF   `xml:"f,omitempty"`      // Formula
+	V        string   `xml:"v,omitempty"`      // Value
+	XMLSpace xml.Attr `xml:"space,attr,omitempty"`
 }
 
 // xlsxF directly maps the f element in the namespace