Skip to content

Commit

Permalink
This closes qax-os#1012, support specify the formula in the data vali…
Browse files Browse the repository at this point in the history
…dation range, and update the documentation for the `AddPicture`
  • Loading branch information
xuri committed Aug 25, 2021
1 parent 21ddbba commit ef67172
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 13 deletions.
36 changes: 30 additions & 6 deletions datavalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<formula1>%d</formula1>", int(v))
case float64:
if math.Abs(float64(v)) > math.MaxFloat32 {
return ErrDataValidationRange
}
formula1 = fmt.Sprintf("<formula1>%.17g</formula1>", float64(v))
case string:
formula1 = fmt.Sprintf("<formula1>%s</formula1>", string(v))
default:
return ErrParameterInvalid
}
switch v := f2.(type) {
case int:
formula2 = fmt.Sprintf("<formula2>%d</formula2>", int(v))
case float64:
if math.Abs(float64(v)) > math.MaxFloat32 {
return ErrDataValidationRange
}
formula2 = fmt.Sprintf("<formula2>%.17g</formula2>", float64(v))
case string:
formula2 = fmt.Sprintf("<formula2>%s</formula2>", string(v))
default:
return ErrParameterInvalid
}
dd.Formula1 = fmt.Sprintf("<formula1>%.17g</formula1>", f1)
dd.Formula2 = fmt.Sprintf("<formula2>%.17g</formula2>", f2)
dd.Formula1, dd.Formula2 = formula1, formula2
dd.Type = convDataValidationType(t)
dd.Operator = convDataValidationOperatior(o)
return nil
Expand Down
11 changes: 11 additions & 0 deletions datavalidation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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")

Expand Down
42 changes: 35 additions & 7 deletions picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ef67172

Please sign in to comment.