diff options
author | Olivier Mengué <dolmen@cpan.org> | 2017-11-17 12:56:58 +0100 |
---|---|---|
committer | Olivier Mengué <dolmen@cpan.org> | 2018-06-14 17:54:31 +0200 |
commit | 4a1b4064568189d6f819f5fe19ff9bf6d6e8de95 (patch) | |
tree | bfb04fe1249c52198d19b4c8f5ca6788730326eb /sheet.go | |
parent | 9e463b4614348b3ddc04b1fedd5d662845ce0fb9 (diff) |
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
```
Diffstat (limited to 'sheet.go')
-rw-r--r-- | sheet.go | 16 |
1 files changed, 7 insertions, 9 deletions
@@ -10,6 +10,8 @@ import ( "strconv" "strings" "unicode/utf8" + + "github.com/mohae/deepcopy" ) // 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) == "" { 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 // 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)) - worksheet := xlsxWorksheet{} - err := deepCopy(&worksheet, &sheet) - if err != nil { - return err - } + worksheet := deepcopy.Copy(sheet).(*xlsxWorksheet) path := "xl/worksheets/sheet" + strconv.Itoa(to) + ".xml" if len(worksheet.SheetViews.SheetView) > 0 { worksheet.SheetViews.SheetView[0].TabSelected = false @@ -452,14 +451,13 @@ func (f *File) copySheet(from, to int) error { worksheet.Drawing = nil worksheet.TableParts = nil worksheet.PageSetUp = nil - f.Sheet[path] = &worksheet + f.Sheet[path] = worksheet toRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(to) + ".xml.rels" fromRels := "xl/worksheets/_rels/sheet" + strconv.Itoa(from) + ".xml.rels" _, ok := f.XLSX[fromRels] if ok { f.XLSX[toRels] = f.XLSX[fromRels] } - return err } // SetSheetVisible provides function to set worksheet visible by given worksheet |