Bugfix: deep copy issue with function `CopySheet()`, relate PR #108.

This commit is contained in:
Ri Xu 2017-08-19 13:37:15 +08:00
parent 77af25295e
commit 1ec2661dda
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 18 additions and 1 deletions

View File

@ -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)

12
lib.go
View File

@ -3,6 +3,7 @@ package excelize
import (
"archive/zip"
"bytes"
"encoding/gob"
"io"
"log"
"math"
@ -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)
}

View File

@ -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