From 1ec2661dda1ef16f58b2a3d614b11a2bcd0a2f2f Mon Sep 17 00:00:00 2001 From: Ri Xu Date: Sat, 19 Aug 2017 13:37:15 +0800 Subject: [PATCH] Bugfix: deep copy issue with function `CopySheet()`, relate PR #108. --- excelize_test.go | 4 ++++ lib.go | 12 ++++++++++++ sheet.go | 3 ++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/excelize_test.go b/excelize_test.go index b075cd0..29091cc 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -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) diff --git a/lib.go b/lib.go index 5649f65..1bcb8fc 100644 --- a/lib.go +++ b/lib.go @@ -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) +} diff --git a/sheet.go b/sheet.go index 80d7cd6..ac0969f 100644 --- a/sheet.go +++ b/sheet.go @@ -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