diff options
author | Ri Xu <xuri.me@gmail.com> | 2017-01-12 09:44:10 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-12 09:44:10 +0800 |
commit | 53d8c4bb3aca921d7dd0a4ac3233887208513bfb (patch) | |
tree | c080f0c9ae957637db85a5c63e8104eb6b176dfa | |
parent | 2497ccb8768d68ef5e5e3375f23265b9fee97e0d (diff) | |
parent | 57fc2943ff6e1b2f062daafc6d03d9fc05bfe896 (diff) |
Merge pull request #15 from Sharsie/feature/DefaultCellVal
Setter function to set raw values into a cell
-rw-r--r-- | excelize.go | 29 | ||||
-rw-r--r-- | excelize_test.go | 6 |
2 files changed, 32 insertions, 3 deletions
diff --git a/excelize.go b/excelize.go index 61391dc..ca76c09 100644 --- a/excelize.go +++ b/excelize.go @@ -48,9 +48,9 @@ func (f *File) SetCellValue(sheet string, axis string, value interface{}) { case int64: f.SetCellInt(sheet, axis, int(value.(int64))) case float32: - f.SetCellInt(sheet, axis, int(value.(float32))) + f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(float32)), 'f', -1, 32)) case float64: - f.SetCellInt(sheet, axis, int(value.(float64))) + f.SetCellDefault(sheet, axis, strconv.FormatFloat(float64(value.(float64)), 'f', -1, 64)) case string: f.SetCellStr(sheet, axis, t) case []byte: @@ -110,6 +110,31 @@ func (f *File) SetCellStr(sheet string, axis string, value string) { f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output))) } +// SetCellDefault provides function to set string type value of a cell as default format without escaping the cell +func (f *File) SetCellDefault(sheet string, axis string, value string) { + axis = strings.ToUpper(axis) + var xlsx xlsxWorksheet + col := string(strings.Map(letterOnlyMapF, axis)) + row, _ := strconv.Atoi(strings.Map(intOnlyMapF, axis)) + xAxis := row - 1 + yAxis := titleToNumber(col) + + name := `xl/worksheets/` + strings.ToLower(sheet) + `.xml` + xml.Unmarshal([]byte(f.readXML(name)), &xlsx) + + rows := xAxis + 1 + cell := yAxis + 1 + + xlsx = completeRow(xlsx, rows, cell) + xlsx = completeCol(xlsx, rows, cell) + + xlsx.SheetData.Row[xAxis].C[yAxis].T = "" + xlsx.SheetData.Row[xAxis].C[yAxis].V = value + + output, _ := xml.Marshal(xlsx) + f.saveFileList(name, replaceWorkSheetsRelationshipsNameSpace(string(output))) +} + // Completion column element tags of XML in a sheet. func completeCol(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet { if len(xlsx.SheetData.Row) < cell { diff --git a/excelize_test.go b/excelize_test.go index 7848ed9..cd03cff 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -21,7 +21,9 @@ func TestExcelize(t *testing.T) { } t.Log("\r\n") } - f1.UpdateLinkedValue() + f1.UpdateLinkedValue() + f1.SetCellDefault("SHEET2", "A1", strconv.FormatFloat(float64(100.1588), 'f', -1, 32)) + f1.SetCellDefault("SHEET2", "A1", strconv.FormatFloat(float64(-100.1588), 'f', -1, 64)) f1.SetCellInt("SHEET2", "A1", 100) f1.SetCellStr("SHEET2", "C11", "Knowns") f1.NewSheet(3, "Maximum 31 characters allowed in sheet title.") @@ -47,6 +49,8 @@ func TestExcelize(t *testing.T) { f1.SetCellValue("Sheet2", "F2", int16(42)) f1.SetCellValue("Sheet2", "F2", int32(42)) f1.SetCellValue("Sheet2", "F2", int64(42)) + f1.SetCellValue("Sheet2", "F2", float32(42.65418)) + f1.SetCellValue("Sheet2", "F2", float64(-42.65418)) f1.SetCellValue("Sheet2", "F2", float32(42)) f1.SetCellValue("Sheet2", "F2", float64(42)) f1.SetCellValue("Sheet2", "G2", nil) |