resolve the issue corrupted xlsx after deleting formula of cell, reference #346
parent
e780e41e02
commit
0072bb7310
@ -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:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
@ -0,0 +1,28 @@
|
|||||||
|
// 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"
|
||||||
|
|
||||||
|
// xlsxCalcChain directly maps the calcChain element. This element represents the root of the calculation chain.
|
||||||
|
type xlsxCalcChain struct {
|
||||||
|
XMLName xml.Name `xml:"http://schemas.openxmlformats.org/spreadsheetml/2006/main calcChain"`
|
||||||
|
C []xlsxCalcChainC `xml:"c"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// xlsxCalcChainC directly maps the c element.
|
||||||
|
type xlsxCalcChainC struct {
|
||||||
|
R string `xml:"r,attr"`
|
||||||
|
I int `xml:"i,attr"`
|
||||||
|
L bool `xml:"l,attr,omitempty"`
|
||||||
|
S bool `xml:"s,attr,omitempty"`
|
||||||
|
T bool `xml:"t,attr,omitempty"`
|
||||||
|
A bool `xml:"a,attr,omitempty"`
|
||||||
|
}
|
Loading…
Reference in new issue