From 7d9b9275bd14556bfcaab7f1d3690b1e54ab75e8 Mon Sep 17 00:00:00 2001 From: xuri Date: Thu, 26 Aug 2021 00:48:18 +0800 Subject: [PATCH] This closes #1012, support specify the formula in the data validation range, and update the documentation for the `AddPicture` --- datavalidation.go | 36 ++++++++++++++++++++++++++++++------ datavalidation_test.go | 11 +++++++++++ picture.go | 42 +++++++++++++++++++++++++++++++++++------- 3 files changed, 76 insertions(+), 13 deletions(-) diff --git a/datavalidation.go b/datavalidation.go index e182ebe..047a53c 100644 --- a/datavalidation.go +++ b/datavalidation.go @@ -128,13 +128,37 @@ func (dd *DataValidation) SetDropList(keys []string) error { return nil } -// SetRange provides function to set data validation range in drop list. -func (dd *DataValidation) SetRange(f1, f2 float64, t DataValidationType, o DataValidationOperator) error { - if math.Abs(f1) > math.MaxFloat32 || math.Abs(f2) > math.MaxFloat32 { - return ErrDataValidationRange +// SetRange provides function to set data validation range in drop list, only +// accepts int, float64, or string data type formula argument. +func (dd *DataValidation) SetRange(f1, f2 interface{}, t DataValidationType, o DataValidationOperator) error { + var formula1, formula2 string + switch v := f1.(type) { + case int: + formula1 = fmt.Sprintf("%d", int(v)) + case float64: + if math.Abs(float64(v)) > math.MaxFloat32 { + return ErrDataValidationRange + } + formula1 = fmt.Sprintf("%.17g", float64(v)) + case string: + formula1 = fmt.Sprintf("%s", string(v)) + default: + return ErrParameterInvalid + } + switch v := f2.(type) { + case int: + formula2 = fmt.Sprintf("%d", int(v)) + case float64: + if math.Abs(float64(v)) > math.MaxFloat32 { + return ErrDataValidationRange + } + formula2 = fmt.Sprintf("%.17g", float64(v)) + case string: + formula2 = fmt.Sprintf("%s", string(v)) + default: + return ErrParameterInvalid } - dd.Formula1 = fmt.Sprintf("%.17g", f1) - dd.Formula2 = fmt.Sprintf("%.17g", f2) + dd.Formula1, dd.Formula2 = formula1, formula2 dd.Type = convDataValidationType(t) dd.Operator = convDataValidationOperatior(o) return nil diff --git a/datavalidation_test.go b/datavalidation_test.go index 0cb5929..5986375 100644 --- a/datavalidation_test.go +++ b/datavalidation_test.go @@ -41,6 +41,15 @@ func TestDataValidation(t *testing.T) { assert.NoError(t, f.AddDataValidation("Sheet1", dvRange)) assert.NoError(t, f.SaveAs(resultFile)) + f.NewSheet("Sheet2") + assert.NoError(t, f.SetSheetRow("Sheet2", "A2", &[]interface{}{"B2", 1})) + assert.NoError(t, f.SetSheetRow("Sheet2", "A3", &[]interface{}{"B3", 3})) + dvRange = NewDataValidation(true) + dvRange.Sqref = "A1:B1" + assert.NoError(t, dvRange.SetRange("INDIRECT($A$2)", "INDIRECT($A$3)", DataValidationTypeWhole, DataValidationOperatorBetween)) + dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body") + assert.NoError(t, f.AddDataValidation("Sheet2", dvRange)) + dvRange = NewDataValidation(true) dvRange.Sqref = "A5:B6" for _, listValid := range [][]string{ @@ -86,6 +95,8 @@ func TestDataValidationError(t *testing.T) { return } assert.EqualError(t, err, ErrDataValidationFormulaLenth.Error()) + assert.EqualError(t, dvRange.SetRange(nil, 20, DataValidationTypeWhole, DataValidationOperatorBetween), ErrParameterInvalid.Error()) + assert.EqualError(t, dvRange.SetRange(10, nil, DataValidationTypeWhole, DataValidationOperatorBetween), ErrParameterInvalid.Error()) assert.NoError(t, dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)) dvRange.SetSqref("A9:B10") diff --git a/picture.go b/picture.go index e3601dd..5f3a375 100644 --- a/picture.go +++ b/picture.go @@ -76,14 +76,42 @@ func parseFormatPictureSet(formatSet string) (*formatPicture, error) { // } // } // -// LinkType defines two types of hyperlink "External" for web site or -// "Location" for moving to one of cell in this workbook. When the -// "hyperlink_type" is "Location", coordinates need to start with "#". +// The optional parameter "autofit" specifies if make image size auto fits the +// cell, the default value of that is 'false'. +// +// The optional parameter "hyperlink" specifies the hyperlink of the image. +// +// The optional parameter "hyperlink_type" defines two types of +// hyperlink "External" for website or "Location" for moving to one of the +// cells in this workbook. When the "hyperlink_type" is "Location", +// coordinates need to start with "#". +// +// The optional parameter "positioning" defines two types of the position of a +// image in an Excel spreadsheet, "oneCell" (Move but don't size with +// cells) or "absolute" (Don't move or size with cells). If you don't set this +// parameter, the default positioning is move and size with cells. +// +// The optional parameter "print_obj" indicates whether the image is printed +// when the worksheet is printed, the default value of that is 'true'. +// +// The optional parameter "lock_aspect_ratio" indicates whether lock aspect +// ratio for the image, the default value of that is 'false'. +// +// The optional parameter "locked" indicates whether lock the image. Locking +// an object has no effect unless the sheet is protected. +// +// The optional parameter "x_offset" specifies the horizontal offset of the +// image with the cell, the default value of that is 0. +// +// The optional parameter "x_scale" specifies the horizontal scale of images, +// the default value of that is 1.0 which presents 100%. +// +// The optional parameter "y_offset" specifies the vertical offset of the +// image with the cell, the default value of that is 0. +// +// The optional parameter "y_scale" specifies the vertical scale of images, +// the default value of that is 1.0 which presents 100%. // -// Positioning defines two types of the position of a picture in an Excel -// spreadsheet, "oneCell" (Move but don't size with cells) or "absolute" -// (Don't move or size with cells). If you don't set this parameter, default -// positioning is move and size with cells. func (f *File) AddPicture(sheet, cell, picture, format string) error { var err error // Check picture exists first.