CopySheet() using reflect instead of encoding/gob

Use github.com/mohae/deepcopy to deep copy worksheets instead of the
internal deepcopy function that was using encoding/gob serialization and
deserialization.

Rationale:
1/ using `encoding/gob` is much slower than
   [`mohae/deepcopy`](https://github.com/mohae/deepcopy/)
2/ When building an application this implementation of `deepcopy` drags
   the `encoding/gob` package into the binary. And this package is much
   bigger than `mohae/deepcopy` (which only depends on `time` and `reflect`).

```
$ LC_ALL=C stat -f "%6z %N" $(go env GOPATH)/pkg/$(go env GOOS)_$(go env GOARCH)/github.com/mohae/deepcopy.a $(go env GOROOT)/pkg/$(go env GOOS)_$(go env GOARCH)/encoding/gob.a
 10508 .../pkg/darwin_amd64/github.com/mohae/deepcopy.a
541818 .../pkg/darwin_amd64/encoding/gob.a
```
formula
Olivier Mengué 7 years ago
parent 9e463b4614
commit 4a1b406456

@ -88,7 +88,6 @@ func TestOpenFile(t *testing.T) {
xlsx.SetCellValue("Sheet2", "F16", true) xlsx.SetCellValue("Sheet2", "F16", true)
xlsx.SetCellValue("Sheet2", "F17", complex64(5+10i)) xlsx.SetCellValue("Sheet2", "F17", complex64(5+10i))
t.Log(letterOnlyMapF('x')) t.Log(letterOnlyMapF('x'))
t.Log(deepCopy(nil, nil))
shiftJulianToNoon(1, -0.6) shiftJulianToNoon(1, -0.6)
timeFromExcelTime(61, true) timeFromExcelTime(61, true)
timeFromExcelTime(62, true) timeFromExcelTime(62, true)

@ -3,7 +3,6 @@ package excelize
import ( import (
"archive/zip" "archive/zip"
"bytes" "bytes"
"encoding/gob"
"io" "io"
"log" "log"
"math" "math"
@ -115,17 +114,6 @@ 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)
}
// boolPtr returns a pointer to a bool with the given value. // boolPtr returns a pointer to a bool with the given value.
func boolPtr(b bool) *bool { return &b } func boolPtr(b bool) *bool { return &b }

@ -10,6 +10,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
"github.com/mohae/deepcopy"
) )
// NewSheet provides function to create a new sheet by given worksheet name, // NewSheet provides function to create a new sheet by given worksheet name,
@ -433,18 +435,15 @@ func (f *File) CopySheet(from, to int) error {
if from < 1 || to < 1 || from == to || f.GetSheetName(from) == "" || f.GetSheetName(to) == "" { if from < 1 || to < 1 || from == to || f.GetSheetName(from) == "" || f.GetSheetName(to) == "" {
return errors.New("Invalid worksheet index") return errors.New("Invalid worksheet index")
} }
return f.copySheet(from, to) f.copySheet(from, to)
return nil
} }
// copySheet provides function to duplicate a worksheet by gave source and // copySheet provides function to duplicate a worksheet by gave source and
// target worksheet name. // target worksheet name.
func (f *File) copySheet(from, to int) error { func (f *File) copySheet(from, to int) {
sheet := f.workSheetReader("sheet" + strconv.Itoa(from)) sheet := f.workSheetReader("sheet" + strconv.Itoa(from))
worksheet := xlsxWorksheet{} worksheet := deepcopy.Copy(sheet).(*xlsxWorksheet)
err := deepCopy(&worksheet, &sheet)
if err != nil {
return err
}
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
@ -452,14 +451,13 @@ func (f *File) copySheet(from, to int) error {
worksheet.Drawing = nil worksheet.Drawing = nil
worksheet.TableParts = nil worksheet.TableParts = nil
worksheet.PageSetUp = nil worksheet.PageSetUp = nil
f.Sheet[path] = &worksheet f.Sheet[path] = worksheet
toRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(to) + ".xml.rels" toRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(to) + ".xml.rels"
fromRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(from) + ".xml.rels" fromRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(from) + ".xml.rels"
_, ok := f.XLSX[fromRels] _, ok := f.XLSX[fromRels]
if ok { if ok {
f.XLSX[toRels] = f.XLSX[fromRels] f.XLSX[toRels] = f.XLSX[fromRels]
} }
return err
} }
// SetSheetVisible provides function to set worksheet visible by given worksheet // SetSheetVisible provides function to set worksheet visible by given worksheet

Loading…
Cancel
Save