Selaa lähdekoodia

new formula func CLEAN and TRIM, change import path to v2 (#747)

Zhang Zhipeng 5 vuotta sitten
vanhempi
commit
ad79505173
8 muutettua tiedostoa jossa 57 lisäystä ja 14 poistoa
  1. 4 4
      README.md
  2. 4 4
      README_zh.md
  3. 29 0
      calc.go
  4. 14 0
      calc_test.go
  5. 1 1
      cell.go
  6. 2 2
      chart.go
  7. 2 2
      picture.go
  8. 1 1
      pivotTable.go

+ 4 - 4
README.md

@@ -39,7 +39,7 @@ package main
 import (
     "fmt"
 
-    "github.com/360EntSecGroup-Skylar/excelize"
+    "github.com/360EntSecGroup-Skylar/excelize/v2"
 )
 
 func main() {
@@ -68,7 +68,7 @@ package main
 import (
     "fmt"
 
-    "github.com/360EntSecGroup-Skylar/excelize"
+    "github.com/360EntSecGroup-Skylar/excelize/v2"
 )
 
 func main() {
@@ -107,7 +107,7 @@ package main
 import (
     "fmt"
 
-    "github.com/360EntSecGroup-Skylar/excelize"
+    "github.com/360EntSecGroup-Skylar/excelize/v2"
 )
 
 func main() {
@@ -142,7 +142,7 @@ import (
     _ "image/jpeg"
     _ "image/png"
 
-    "github.com/360EntSecGroup-Skylar/excelize"
+    "github.com/360EntSecGroup-Skylar/excelize/v2"
 )
 
 func main() {

+ 4 - 4
README_zh.md

@@ -39,7 +39,7 @@ package main
 import (
     "fmt"
 
-    "github.com/360EntSecGroup-Skylar/excelize"
+    "github.com/360EntSecGroup-Skylar/excelize/v2"
 )
 
 func main() {
@@ -68,7 +68,7 @@ package main
 import (
     "fmt"
 
-    "github.com/360EntSecGroup-Skylar/excelize"
+    "github.com/360EntSecGroup-Skylar/excelize/v2"
 )
 
 func main() {
@@ -107,7 +107,7 @@ package main
 import (
     "fmt"
 
-    "github.com/360EntSecGroup-Skylar/excelize"
+    "github.com/360EntSecGroup-Skylar/excelize/v2"
 )
 
 func main() {
@@ -142,7 +142,7 @@ import (
     _ "image/jpeg"
     _ "image/png"
 
-    "github.com/360EntSecGroup-Skylar/excelize"
+    "github.com/360EntSecGroup-Skylar/excelize/v2"
 )
 
 func main() {

+ 29 - 0
calc.go

@@ -3365,3 +3365,32 @@ func makeDate(y int, m time.Month, d int) int64 {
 func daysBetween(startDate, endDate int64) float64 {
 	return float64(int(0.5 + float64((endDate-startDate)/86400)))
 }
+
+// Text Functions
+
+// CLEAN removes all non-printable characters from a supplied text string.
+func (fn *formulaFuncs) CLEAN(argsList *list.List) (result string, err error) {
+	if argsList.Len() != 1 {
+		err = errors.New("CLEAN requires 1 argument")
+		return
+	}
+	b := bytes.Buffer{}
+	for _, c := range argsList.Front().Value.(formulaArg).String {
+		if c > 31 {
+			b.WriteRune(c)
+		}
+	}
+	result = b.String()
+	return
+}
+
+// TRIM removes extra spaces (i.e. all spaces except for single spaces between
+// words or characters) from a supplied text string.
+func (fn *formulaFuncs) TRIM(argsList *list.List) (result string, err error) {
+	if argsList.Len() != 1 {
+		err = errors.New("TRIM requires 1 argument")
+		return
+	}
+	result = strings.TrimSpace(argsList.Front().Value.(formulaArg).String)
+	return
+}

+ 14 - 0
calc_test.go

@@ -463,6 +463,13 @@ func TestCalcCellValue(t *testing.T) {
 		// DATE
 		"=DATE(2020,10,21)": "2020-10-21 00:00:00 +0000 UTC",
 		"=DATE(1900,1,1)":   "1899-12-31 00:00:00 +0000 UTC",
+		// Text Functions
+		// CLEAN
+		"=CLEAN(\"\u0009clean text\")": "clean text",
+		"=CLEAN(0)":                    "0",
+		// TRIM
+		"=TRIM(\" trim text \")": "trim text",
+		"=TRIM(0)":               "0",
 	}
 	for formula, expected := range mathCalc {
 		f := prepareData()
@@ -779,6 +786,13 @@ func TestCalcCellValue(t *testing.T) {
 		`=DATE("text",10,21)`:   "DATE requires 3 number arguments",
 		`=DATE(2020,"text",21)`: "DATE requires 3 number arguments",
 		`=DATE(2020,10,"text")`: "DATE requires 3 number arguments",
+		// Text Functions
+		// CLEAN
+		"=CLEAN()":    "CLEAN requires 1 argument",
+		"=CLEAN(1,2)": "CLEAN requires 1 argument",
+		// TRIM
+		"=TRIM()":    "TRIM requires 1 argument",
+		"=TRIM(1,2)": "TRIM requires 1 argument",
 	}
 	for formula, expected := range mathCalcError {
 		f := prepareData()

+ 1 - 1
cell.go

@@ -502,7 +502,7 @@ func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) error {
 //    import (
 //        "fmt"
 //
-//        "github.com/360EntSecGroup-Skylar/excelize"
+//        "github.com/360EntSecGroup-Skylar/excelize/v2"
 //    )
 //
 //    func main() {

+ 2 - 2
chart.go

@@ -510,7 +510,7 @@ func parseFormatChartSet(formatSet string) (*formatChart, error) {
 //    import (
 //        "fmt"
 //
-//        "github.com/360EntSecGroup-Skylar/excelize"
+//        "github.com/360EntSecGroup-Skylar/excelize/v2"
 //    )
 //
 //    func main() {
@@ -708,7 +708,7 @@ func parseFormatChartSet(formatSet string) (*formatChart, error) {
 //    import (
 //        "fmt"
 //
-//        "github.com/360EntSecGroup-Skylar/excelize"
+//        "github.com/360EntSecGroup-Skylar/excelize/v2"
 //    )
 //
 //    func main() {

+ 2 - 2
picture.go

@@ -55,7 +55,7 @@ func parseFormatPictureSet(formatSet string) (*formatPicture, error) {
 //        _ "image/jpeg"
 //        _ "image/png"
 //
-//        "github.com/360EntSecGroup-Skylar/excelize"
+//        "github.com/360EntSecGroup-Skylar/excelize/v2"
 //    )
 //
 //    func main() {
@@ -111,7 +111,7 @@ func (f *File) AddPicture(sheet, cell, picture, format string) error {
 //        _ "image/jpeg"
 //        "io/ioutil"
 //
-//        "github.com/360EntSecGroup-Skylar/excelize"
+//        "github.com/360EntSecGroup-Skylar/excelize/v2"
 //    )
 //
 //    func main() {

+ 1 - 1
pivotTable.go

@@ -80,7 +80,7 @@ type PivotTableField struct {
 //        "fmt"
 //        "math/rand"
 //
-//        "github.com/360EntSecGroup-Skylar/excelize"
+//        "github.com/360EntSecGroup-Skylar/excelize/v2"
 //    )
 //
 //    func main() {