Skip to content

Commit

Permalink
Bugfix: deep copy issue with function CopySheet(), relate PR #108.
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Aug 19, 2017
1 parent 77af252 commit 1ec2661
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,10 @@ func TestCopySheet(t *testing.T) {
if err != nil {
t.Log(err)
}
xlsx.SetCellValue("Sheet4", "F1", "Hello")
if xlsx.GetCellValue("Sheet1", "F1") == "Hello" {
t.Error("Invalid value \"Hello\" in Sheet1")
}
err = xlsx.Save()
if err != nil {
t.Log(err)
Expand Down
12 changes: 12 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package excelize
import (
"archive/zip"
"bytes"
"encoding/gob"
"io"
"log"
"math"
Expand Down Expand Up @@ -104,3 +105,14 @@ func intOnlyMapF(rune rune) rune {
}
return -1
}

// deepCopy provides method to creates a deep copy of whatever is passed to it
// and returns the copy in an interface. The returned value will need to be
// asserted to the correct type.
func deepCopy(dst, src interface{}) error {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(src); err != nil {
return err
}
return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
}
3 changes: 2 additions & 1 deletion sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ func (f *File) CopySheet(from, to int) error {
// target worksheet index.
func (f *File) copySheet(from, to int) {
sheet := f.workSheetReader("sheet" + strconv.Itoa(from))
worksheet := *sheet
worksheet := xlsxWorksheet{}
deepCopy(&worksheet, &sheet)
path := "xl/worksheets/sheet" + strconv.Itoa(to) + ".xml"
if len(worksheet.SheetViews.SheetView) > 0 {
worksheet.SheetViews.SheetView[0].TabSelected = false
Expand Down

0 comments on commit 1ec2661

Please sign in to comment.