summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRi Xu <xuri.me@gmail.com>2016-09-09 19:39:41 +0800
committerRi Xu <xuri.me@gmail.com>2016-09-09 19:39:41 +0800
commitcbfd6577536304f9b0b93edb7748475d8266fe19 (patch)
tree6cc38b2f18b9510553915a139aebd93c8e05602d
parent9c14741a6beaab9048d107f2ac47d79416d51c62 (diff)
New function SetCellValue added and update godoc example.
-rw-r--r--excelize.go79
-rw-r--r--excelize_test.go5
2 files changed, 48 insertions, 36 deletions
diff --git a/excelize.go b/excelize.go
index 258411e..12eac1c 100644
--- a/excelize.go
+++ b/excelize.go
@@ -4,7 +4,6 @@ import (
"archive/zip"
"bytes"
"encoding/xml"
- "fmt"
"strconv"
"strings"
)
@@ -35,6 +34,20 @@ func OpenFile(filename string) (*File, error) {
}, nil
}
+// SetCellValue provide function to set int or string type value of a cell
+func (f *File) SetCellValue(sheet string, axis string, value interface{}) {
+ switch t := value.(type) {
+ case int, int8, int16, int32, int64, float32, float64:
+ f.SetCellInt(sheet, axis, value.(int))
+ case string:
+ f.SetCellStr(sheet, axis, t)
+ case []byte:
+ f.SetCellStr(sheet, axis, string(t))
+ default:
+ f.SetCellStr(sheet, axis, ``)
+ }
+}
+
// SetCellInt provide function to set int type value of a cell
func (f *File) SetCellInt(sheet string, axis string, value int) {
axis = strings.ToUpper(axis)
@@ -56,10 +69,7 @@ func (f *File) SetCellInt(sheet string, axis string, value int) {
xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
xlsx.SheetData.Row[xAxis].C[yAxis].V = strconv.Itoa(value)
- output, err := xml.Marshal(xlsx)
- if err != nil {
- fmt.Println(err)
- }
+ output, _ := xml.Marshal(xlsx)
f.saveFileList(name, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output))))
}
@@ -81,13 +91,10 @@ func (f *File) SetCellStr(sheet string, axis string, value string) {
xlsx = completeRow(xlsx, rows, cell)
xlsx = completeCol(xlsx, rows, cell)
- xlsx.SheetData.Row[xAxis].C[yAxis].T = "str"
+ xlsx.SheetData.Row[xAxis].C[yAxis].T = `str`
xlsx.SheetData.Row[xAxis].C[yAxis].V = value
- output, err := xml.Marshal(xlsx)
- if err != nil {
- fmt.Println(err)
- }
+ output, _ := xml.Marshal(xlsx)
f.saveFileList(name, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output))))
}
@@ -162,24 +169,24 @@ func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string {
// Check XML tags and fix discontinuous case, for example:
//
-// <row r="15" spans="1:22" x14ac:dyDescent="0.2">
-// <c r="A15" s="2" />
-// <c r="B15" s="2" />
-// <c r="F15" s="1" />
-// <c r="G15" s="1" />
-// </row>
+// <row r="15" spans="1:22" x14ac:dyDescent="0.2">
+// <c r="A15" s="2" />
+// <c r="B15" s="2" />
+// <c r="F15" s="1" />
+// <c r="G15" s="1" />
+// </row>
//
// in this case, we should to change it to
//
-// <row r="15" spans="1:22" x14ac:dyDescent="0.2">
-// <c r="A15" s="2" />
-// <c r="B15" s="2" />
-// <c r="C15" s="2" />
-// <c r="D15" s="2" />
-// <c r="E15" s="2" />
-// <c r="F15" s="1" />
-// <c r="G15" s="1" />
-// </row>
+// <row r="15" spans="1:22" x14ac:dyDescent="0.2">
+// <c r="A15" s="2" />
+// <c r="B15" s="2" />
+// <c r="C15" s="2" />
+// <c r="D15" s="2" />
+// <c r="E15" s="2" />
+// <c r="F15" s="1" />
+// <c r="G15" s="1" />
+// </row>
//
// Noteice: this method could be very slow for large spreadsheets (more than 3000 rows one sheet).
func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
@@ -223,20 +230,20 @@ func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
//
// For example:
//
-// <row r="19" spans="2:2">
-// <c r="B19">
-// <f>SUM(Sheet2!D2,Sheet2!D11)</f>
-// <v>100</v>
-// </c>
-// </row>
+// <row r="19" spans="2:2">
+// <c r="B19">
+// <f>SUM(Sheet2!D2,Sheet2!D11)</f>
+// <v>100</v>
+// </c>
+// </row>
//
// to
//
-// <row r="19" spans="2:2">
-// <c r="B19">
-// <f>SUM(Sheet2!D2,Sheet2!D11)</f>
-// </c>
-// </row>
+// <row r="19" spans="2:2">
+// <c r="B19">
+// <f>SUM(Sheet2!D2,Sheet2!D11)</f>
+// </c>
+// </row>
func (f *File) UpdateLinkedValue() {
for i := 1; i <= f.SheetCount; i++ {
var xlsx xlsxWorksheet
diff --git a/excelize_test.go b/excelize_test.go
index 7d37a6a..09bfef1 100644
--- a/excelize_test.go
+++ b/excelize_test.go
@@ -26,6 +26,11 @@ func TestExcelize(t *testing.T) {
file.GetCellValue("Sheet2", "C11")
file.GetCellValue("Sheet2", "D11")
file.GetCellValue("Sheet2", "D12")
+ // Test SetCellValue function
+ file.SetCellValue("Sheet2", "F1", "Hello")
+ file.SetCellValue("Sheet2", "G1", []byte("World"))
+ file.SetCellValue("Sheet2", "F2", 42)
+ file.SetCellValue("Sheet2", "G2", nil)
// Test read cell value with given axis large than exists row
file.GetCellValue("Sheet2", "E13")