summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSharsie <Sharsie@users.noreply.github.com>2017-01-11 19:26:06 +0100
committerGitHub <noreply@github.com>2017-01-11 19:26:06 +0100
commit670a58791208d64d045a68f56dafab58f292c13e (patch)
tree9664dcb870eac44f9a4aa29c7d113ee15179a1a1
parent2497ccb8768d68ef5e5e3375f23265b9fee97e0d (diff)
Add setter for default cell value
SetCellStr escapes the value in a cell so the excel sheet cannot use the value in formulas. SetCellDefault accepts a string value and sets it to a cell as a raw value without escaping it
-rw-r--r--excelize.go29
1 files changed, 27 insertions, 2 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 {