spreadsheet: add method to clear cached formula results

This commit is contained in:
Todd 2017-09-10 17:15:44 -05:00
parent f3eca9e68e
commit 9e6bfaf6e1
3 changed files with 38 additions and 0 deletions

View File

@ -505,3 +505,17 @@ func (s Sheet) AddDataValidation() DataValidation {
s.x.DataValidations.CountAttr = gooxml.Uint32(uint32(len(s.x.DataValidations.DataValidation)))
return DataValidation{dv}
}
// ClearCachedFormulaResults clears any computed formula values that are stored
// in the sheet. This may be required if you modify cells that are used as a
// formula input to force the formulas to be recomputed the next time the sheet
// is opened in Excel.
func (s *Sheet) ClearCachedFormulaResults() {
for _, r := range s.Rows() {
for _, c := range r.Cells() {
if c.X().F != nil {
c.X().V = nil
}
}
}
}

View File

@ -11,6 +11,7 @@ import (
"math/rand"
"testing"
"baliance.com/gooxml"
"baliance.com/gooxml/spreadsheet"
)
@ -186,3 +187,16 @@ func TestSheetExtents(t *testing.T) {
}
}
func TestSheetClearCachedFormula(t *testing.T) {
ss := spreadsheet.New()
sheet := ss.AddSheet()
cell := sheet.Cell("A1")
cell.SetFormulaRaw("foo")
cell.X().V = gooxml.String("cached-results")
sheet.ClearCachedFormulaResults()
if cell.X().V != nil {
t.Errorf("cached result not cleared")
}
}

View File

@ -414,3 +414,13 @@ func (wb *Workbook) DefinedNames() []DefinedName {
}
return ret
}
// ClearCachedFormulaResults clears any computed formula values that are stored
// in the sheet. This may be required if you modify cells that are used as a
// formula input to force the formulas to be recomputed the next time the sheet
// is opened in Excel.
func (wb *Workbook) ClearCachedFormulaResults() {
for _, s := range wb.Sheets() {
s.ClearCachedFormulaResults()
}
}