Skip to content

Commit 1ec2661

Browse files
committed
Bugfix: deep copy issue with function CopySheet(), relate PR qax-os#108.
1 parent 77af252 commit 1ec2661

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Diff for: excelize_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,10 @@ func TestCopySheet(t *testing.T) {
682682
if err != nil {
683683
t.Log(err)
684684
}
685+
xlsx.SetCellValue("Sheet4", "F1", "Hello")
686+
if xlsx.GetCellValue("Sheet1", "F1") == "Hello" {
687+
t.Error("Invalid value \"Hello\" in Sheet1")
688+
}
685689
err = xlsx.Save()
686690
if err != nil {
687691
t.Log(err)

Diff for: lib.go

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package excelize
33
import (
44
"archive/zip"
55
"bytes"
6+
"encoding/gob"
67
"io"
78
"log"
89
"math"
@@ -104,3 +105,14 @@ func intOnlyMapF(rune rune) rune {
104105
}
105106
return -1
106107
}
108+
109+
// deepCopy provides method to creates a deep copy of whatever is passed to it
110+
// and returns the copy in an interface. The returned value will need to be
111+
// asserted to the correct type.
112+
func deepCopy(dst, src interface{}) error {
113+
var buf bytes.Buffer
114+
if err := gob.NewEncoder(&buf).Encode(src); err != nil {
115+
return err
116+
}
117+
return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
118+
}

Diff for: sheet.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@ func (f *File) CopySheet(from, to int) error {
435435
// target worksheet index.
436436
func (f *File) copySheet(from, to int) {
437437
sheet := f.workSheetReader("sheet" + strconv.Itoa(from))
438-
worksheet := *sheet
438+
worksheet := xlsxWorksheet{}
439+
deepCopy(&worksheet, &sheet)
439440
path := "xl/worksheets/sheet" + strconv.Itoa(to) + ".xml"
440441
if len(worksheet.SheetViews.SheetView) > 0 {
441442
worksheet.SheetViews.SheetView[0].TabSelected = false

0 commit comments

Comments
 (0)