summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--picture_test.go20
-rw-r--r--sheet.go24
3 files changed, 36 insertions, 11 deletions
diff --git a/.gitignore b/.gitignore
index 29e0f2b..096dbdd 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,5 @@
~$*.xlsx
test/Test*.xlsx
+*.out
+test/image3.png
+*.test \ No newline at end of file
diff --git a/picture_test.go b/picture_test.go
new file mode 100644
index 0000000..97d3cd9
--- /dev/null
+++ b/picture_test.go
@@ -0,0 +1,20 @@
+package excelize
+
+import (
+ "fmt"
+ _ "image/png"
+ "io/ioutil"
+ "testing"
+)
+
+func BenchmarkAddPictureFromBytes(b *testing.B) {
+ f := NewFile()
+ imgFile, err := ioutil.ReadFile("logo.png")
+ if err != nil {
+ panic("unable to load image for benchmark")
+ }
+ b.ResetTimer()
+ for i := 1; i <= b.N; i++ {
+ f.AddPictureFromBytes("Sheet1", fmt.Sprint("A", i), "", "logo", ".png", imgFile)
+ }
+}
diff --git a/sheet.go b/sheet.go
index 768d0a8..ce1f241 100644
--- a/sheet.go
+++ b/sheet.go
@@ -778,18 +778,20 @@ func (f *File) UnprotectSheet(sheet string) {
// trimSheetName provides a function to trim invaild characters by given worksheet
// name.
func trimSheetName(name string) string {
- var r []rune
- for _, v := range name {
- switch v {
- case 58, 92, 47, 63, 42, 91, 93: // replace :\/?*[]
- continue
- default:
- r = append(r, v)
+ if strings.ContainsAny(name, ":\\/?*[]") || utf8.RuneCountInString(name) > 31 {
+ r := make([]rune, 0, 31)
+ for _, v := range name {
+ switch v {
+ case 58, 92, 47, 63, 42, 91, 93: // replace :\/?*[]
+ continue
+ default:
+ r = append(r, v)
+ }
+ if len(r) == 31 {
+ break
+ }
}
- }
- name = string(r)
- if utf8.RuneCountInString(name) > 31 {
- name = string([]rune(name)[0:31])
+ name = string(r)
}
return name
}