summaryrefslogtreecommitdiff
path: root/calcchain.go
diff options
context:
space:
mode:
authorxuri <xuri.me@gmail.com>2019-02-22 22:17:38 +0800
committerxuri <xuri.me@gmail.com>2019-02-22 22:17:38 +0800
commit0072bb731043f89ce978778b9d7fdc6160e29de0 (patch)
treec44726082c606dbbcf6fa5c1dfba31cd81993c84 /calcchain.go
parente780e41e0222517caa9c69030b5955ab2b458a49 (diff)
resolve the issue corrupted xlsx after deleting formula of cell, reference #346
Diffstat (limited to 'calcchain.go')
-rw-r--r--calcchain.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/calcchain.go b/calcchain.go
new file mode 100644
index 0000000..285a3e9
--- /dev/null
+++ b/calcchain.go
@@ -0,0 +1,55 @@
+// Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
+// this source code is governed by a BSD-style license that can be found in
+// the LICENSE file.
+//
+// Package excelize providing a set of functions that allow you to write to
+// and read from XLSX files. Support reads and writes XLSX file generated by
+// Microsoft Excelâ„¢ 2007 and later. Support save file without losing original
+// charts of XLSX. This library needs Go version 1.8 or later.
+
+package excelize
+
+import "encoding/xml"
+
+// calcChainReader provides a function to get the pointer to the structure
+// after deserialization of xl/calcChain.xml.
+func (f *File) calcChainReader() *xlsxCalcChain {
+ if f.CalcChain == nil {
+ var c xlsxCalcChain
+ _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML("xl/calcChain.xml")), &c)
+ f.CalcChain = &c
+ }
+ return f.CalcChain
+}
+
+// calcChainWriter provides a function to save xl/calcChain.xml after
+// serialize structure.
+func (f *File) calcChainWriter() {
+ if f.CalcChain != nil {
+ output, _ := xml.Marshal(f.CalcChain)
+ f.saveFileList("xl/calcChain.xml", output)
+ }
+}
+
+// deleteCalcChain provides a function to remove cell reference on the
+// calculation chain.
+func (f *File) deleteCalcChain(axis string) {
+ calc := f.calcChainReader()
+ if calc != nil {
+ for i, c := range calc.C {
+ if c.R == axis {
+ calc.C = append(calc.C[:i], calc.C[i+1:]...)
+ }
+ }
+ }
+ if len(calc.C) == 0 {
+ f.CalcChain = nil
+ delete(f.XLSX, "xl/calcChain.xml")
+ content := f.contentTypesReader()
+ for k, v := range content.Overrides {
+ if v.PartName == "/xl/calcChain.xml" {
+ content.Overrides = append(content.Overrides[:k], content.Overrides[k+1:]...)
+ }
+ }
+ }
+}