diff --git a/spreadsheet/sheet.go b/spreadsheet/sheet.go index ee6c2138..5796e63a 100644 --- a/spreadsheet/sheet.go +++ b/spreadsheet/sheet.go @@ -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 + } + } + } +} diff --git a/spreadsheet/sheet_test.go b/spreadsheet/sheet_test.go index 13d7e9b2..06383fc1 100644 --- a/spreadsheet/sheet_test.go +++ b/spreadsheet/sheet_test.go @@ -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") + } + +} diff --git a/spreadsheet/workbook.go b/spreadsheet/workbook.go index 883c4c2e..3de7d0c2 100644 --- a/spreadsheet/workbook.go +++ b/spreadsheet/workbook.go @@ -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() + } +}