Skip to content

Commit 841ff4a

Browse files
committed
Fix out of range panic when removing formula.
Fix file corruption issue when deleting a sheet containing a formula.
1 parent 4e7d93a commit 841ff4a

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

calcchain.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ func (f *File) calcChainWriter() {
3333

3434
// deleteCalcChain provides a function to remove cell reference on the
3535
// calculation chain.
36-
func (f *File) deleteCalcChain(axis string) {
36+
func (f *File) deleteCalcChain(index int, axis string) {
3737
calc := f.calcChainReader()
3838
if calc != nil {
39-
for i, c := range calc.C {
40-
if c.R == axis {
41-
calc.C = append(calc.C[:i], calc.C[i+1:]...)
42-
}
43-
}
39+
calc.C = xlsxCalcChainCollection(calc.C).Filter(func(c xlsxCalcChainC) bool {
40+
return !((c.I == index && c.R == axis) || (c.I == index && axis == ""))
41+
})
4442
}
4543
if len(calc.C) == 0 {
4644
f.CalcChain = nil
@@ -53,3 +51,15 @@ func (f *File) deleteCalcChain(axis string) {
5351
}
5452
}
5553
}
54+
55+
type xlsxCalcChainCollection []xlsxCalcChainC
56+
57+
func (c xlsxCalcChainCollection) Filter(fn func(v xlsxCalcChainC) bool) []xlsxCalcChainC {
58+
results := make([]xlsxCalcChainC, 0)
59+
for _, v := range c {
60+
if fn(v) {
61+
results = append(results, v)
62+
}
63+
}
64+
return results
65+
}

cell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (f *File) SetCellFormula(sheet, axis, formula string) error {
235235
}
236236
if formula == "" {
237237
cellData.F = nil
238-
f.deleteCalcChain(axis)
238+
f.deleteCalcChain(f.GetSheetIndex(sheet), axis)
239239
return err
240240
}
241241

sheet.go

+1
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ func (f *File) DeleteSheet(name string) {
403403
rels := "xl/worksheets/_rels/sheet" + strconv.Itoa(v.SheetID) + ".xml.rels"
404404
target := f.deleteSheetFromWorkbookRels(v.ID)
405405
f.deleteSheetFromContentTypes(target)
406+
f.deleteCalcChain(v.SheetID, "") // Delete CalcChain
406407
delete(f.sheetMap, name)
407408
delete(f.XLSX, sheet)
408409
delete(f.XLSX, rels)

0 commit comments

Comments
 (0)