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

formula v1.1.0
Ri Xu 8 years ago
parent 77af25295e
commit 1ec2661dda
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7

@ -682,6 +682,10 @@ func TestCopySheet(t *testing.T) {
if err != nil { if err != nil {
t.Log(err) t.Log(err)
} }
xlsx.SetCellValue("Sheet4", "F1", "Hello")
if xlsx.GetCellValue("Sheet1", "F1") == "Hello" {
t.Error("Invalid value \"Hello\" in Sheet1")
}
err = xlsx.Save() err = xlsx.Save()
if err != nil { if err != nil {
t.Log(err) t.Log(err)

@ -3,6 +3,7 @@ package excelize
import ( import (
"archive/zip" "archive/zip"
"bytes" "bytes"
"encoding/gob"
"io" "io"
"log" "log"
"math" "math"
@ -104,3 +105,14 @@ func intOnlyMapF(rune rune) rune {
} }
return -1 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)
}

@ -435,7 +435,8 @@ func (f *File) CopySheet(from, to int) error {
// target worksheet index. // target worksheet index.
func (f *File) copySheet(from, to int) { func (f *File) copySheet(from, to int) {
sheet := f.workSheetReader("sheet" + strconv.Itoa(from)) sheet := f.workSheetReader("sheet" + strconv.Itoa(from))
worksheet := *sheet worksheet := xlsxWorksheet{}
deepCopy(&worksheet, &sheet)
path := "xl/worksheets/sheet" + strconv.Itoa(to) + ".xml" path := "xl/worksheets/sheet" + strconv.Itoa(to) + ".xml"
if len(worksheet.SheetViews.SheetView) > 0 { if len(worksheet.SheetViews.SheetView) > 0 {
worksheet.SheetViews.SheetView[0].TabSelected = false worksheet.SheetViews.SheetView[0].TabSelected = false

Loading…
Cancel
Save