From 3280e1b68664e12143cbd2b3a408f9f494a72897 Mon Sep 17 00:00:00 2001 From: Christian Fiedler Date: Sun, 22 Sep 2019 14:52:01 +0200 Subject: Allow access to more formula attributes in SetCellFormula (#484) * Allow access to more formula attributes in SetCellFormula Make SetCellFormula variadic to not break API. The new arguments are option arguments in which the type of the formula and the ref attribute may be set. These need to be set for an array formula to work. * Add TestWriteArrayFormula to test optional parameters of SetCellFormula TestWriteArrayFormula writes a document to the test directory that contains array formulas that are used to calculate standard deviations. The file also contains values calculated by the Go testcase, so the results can be verified. It should be tested, if the array formula works (i.e. shows a number, not an error) and that the values calculated by the formula and those calculated by Go are the same. --- cell.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'cell.go') diff --git a/cell.go b/cell.go index 1da46aa..f9868de 100644 --- a/cell.go +++ b/cell.go @@ -273,9 +273,15 @@ func (f *File) GetCellFormula(sheet, axis string) (string, error) { }) } +// FormulaOpts can be passed to SetCellFormula to use other formula types. +type FormulaOpts struct { + Type *string // Formula type + Ref *string // Shared formula ref +} + // SetCellFormula provides a function to set cell formula by given string and // worksheet name. -func (f *File) SetCellFormula(sheet, axis, formula string) error { +func (f *File) SetCellFormula(sheet, axis, formula string, opts ...FormulaOpts) error { xlsx, err := f.workSheetReader(sheet) if err != nil { return err @@ -295,6 +301,17 @@ func (f *File) SetCellFormula(sheet, axis, formula string) error { } else { cellData.F = &xlsxF{Content: formula} } + + for _, o := range opts { + if o.Type != nil { + cellData.F.T = *o.Type + } + + if o.Ref != nil { + cellData.F.Ref = *o.Ref + } + } + return err } -- cgit v1.2.1