Skip to content

Commit 7d9b927

Browse files
committed
This closes qax-os#1012, support specify the formula in the data validation range, and update the documentation for the AddPicture
1 parent cd030d4 commit 7d9b927

File tree

3 files changed

+76
-13
lines changed

3 files changed

+76
-13
lines changed

datavalidation.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,37 @@ func (dd *DataValidation) SetDropList(keys []string) error {
128128
return nil
129129
}
130130

131-
// SetRange provides function to set data validation range in drop list.
132-
func (dd *DataValidation) SetRange(f1, f2 float64, t DataValidationType, o DataValidationOperator) error {
133-
if math.Abs(f1) > math.MaxFloat32 || math.Abs(f2) > math.MaxFloat32 {
134-
return ErrDataValidationRange
131+
// SetRange provides function to set data validation range in drop list, only
132+
// accepts int, float64, or string data type formula argument.
133+
func (dd *DataValidation) SetRange(f1, f2 interface{}, t DataValidationType, o DataValidationOperator) error {
134+
var formula1, formula2 string
135+
switch v := f1.(type) {
136+
case int:
137+
formula1 = fmt.Sprintf("<formula1>%d</formula1>", int(v))
138+
case float64:
139+
if math.Abs(float64(v)) > math.MaxFloat32 {
140+
return ErrDataValidationRange
141+
}
142+
formula1 = fmt.Sprintf("<formula1>%.17g</formula1>", float64(v))
143+
case string:
144+
formula1 = fmt.Sprintf("<formula1>%s</formula1>", string(v))
145+
default:
146+
return ErrParameterInvalid
147+
}
148+
switch v := f2.(type) {
149+
case int:
150+
formula2 = fmt.Sprintf("<formula2>%d</formula2>", int(v))
151+
case float64:
152+
if math.Abs(float64(v)) > math.MaxFloat32 {
153+
return ErrDataValidationRange
154+
}
155+
formula2 = fmt.Sprintf("<formula2>%.17g</formula2>", float64(v))
156+
case string:
157+
formula2 = fmt.Sprintf("<formula2>%s</formula2>", string(v))
158+
default:
159+
return ErrParameterInvalid
135160
}
136-
dd.Formula1 = fmt.Sprintf("<formula1>%.17g</formula1>", f1)
137-
dd.Formula2 = fmt.Sprintf("<formula2>%.17g</formula2>", f2)
161+
dd.Formula1, dd.Formula2 = formula1, formula2
138162
dd.Type = convDataValidationType(t)
139163
dd.Operator = convDataValidationOperatior(o)
140164
return nil

datavalidation_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ func TestDataValidation(t *testing.T) {
4141
assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))
4242
assert.NoError(t, f.SaveAs(resultFile))
4343

44+
f.NewSheet("Sheet2")
45+
assert.NoError(t, f.SetSheetRow("Sheet2", "A2", &[]interface{}{"B2", 1}))
46+
assert.NoError(t, f.SetSheetRow("Sheet2", "A3", &[]interface{}{"B3", 3}))
47+
dvRange = NewDataValidation(true)
48+
dvRange.Sqref = "A1:B1"
49+
assert.NoError(t, dvRange.SetRange("INDIRECT($A$2)", "INDIRECT($A$3)", DataValidationTypeWhole, DataValidationOperatorBetween))
50+
dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body")
51+
assert.NoError(t, f.AddDataValidation("Sheet2", dvRange))
52+
4453
dvRange = NewDataValidation(true)
4554
dvRange.Sqref = "A5:B6"
4655
for _, listValid := range [][]string{
@@ -86,6 +95,8 @@ func TestDataValidationError(t *testing.T) {
8695
return
8796
}
8897
assert.EqualError(t, err, ErrDataValidationFormulaLenth.Error())
98+
assert.EqualError(t, dvRange.SetRange(nil, 20, DataValidationTypeWhole, DataValidationOperatorBetween), ErrParameterInvalid.Error())
99+
assert.EqualError(t, dvRange.SetRange(10, nil, DataValidationTypeWhole, DataValidationOperatorBetween), ErrParameterInvalid.Error())
89100
assert.NoError(t, dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan))
90101
dvRange.SetSqref("A9:B10")
91102

picture.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,42 @@ func parseFormatPictureSet(formatSet string) (*formatPicture, error) {
7676
// }
7777
// }
7878
//
79-
// LinkType defines two types of hyperlink "External" for web site or
80-
// "Location" for moving to one of cell in this workbook. When the
81-
// "hyperlink_type" is "Location", coordinates need to start with "#".
79+
// The optional parameter "autofit" specifies if make image size auto fits the
80+
// cell, the default value of that is 'false'.
81+
//
82+
// The optional parameter "hyperlink" specifies the hyperlink of the image.
83+
//
84+
// The optional parameter "hyperlink_type" defines two types of
85+
// hyperlink "External" for website or "Location" for moving to one of the
86+
// cells in this workbook. When the "hyperlink_type" is "Location",
87+
// coordinates need to start with "#".
88+
//
89+
// The optional parameter "positioning" defines two types of the position of a
90+
// image in an Excel spreadsheet, "oneCell" (Move but don't size with
91+
// cells) or "absolute" (Don't move or size with cells). If you don't set this
92+
// parameter, the default positioning is move and size with cells.
93+
//
94+
// The optional parameter "print_obj" indicates whether the image is printed
95+
// when the worksheet is printed, the default value of that is 'true'.
96+
//
97+
// The optional parameter "lock_aspect_ratio" indicates whether lock aspect
98+
// ratio for the image, the default value of that is 'false'.
99+
//
100+
// The optional parameter "locked" indicates whether lock the image. Locking
101+
// an object has no effect unless the sheet is protected.
102+
//
103+
// The optional parameter "x_offset" specifies the horizontal offset of the
104+
// image with the cell, the default value of that is 0.
105+
//
106+
// The optional parameter "x_scale" specifies the horizontal scale of images,
107+
// the default value of that is 1.0 which presents 100%.
108+
//
109+
// The optional parameter "y_offset" specifies the vertical offset of the
110+
// image with the cell, the default value of that is 0.
111+
//
112+
// The optional parameter "y_scale" specifies the vertical scale of images,
113+
// the default value of that is 1.0 which presents 100%.
82114
//
83-
// Positioning defines two types of the position of a picture in an Excel
84-
// spreadsheet, "oneCell" (Move but don't size with cells) or "absolute"
85-
// (Don't move or size with cells). If you don't set this parameter, default
86-
// positioning is move and size with cells.
87115
func (f *File) AddPicture(sheet, cell, picture, format string) error {
88116
var err error
89117
// Check picture exists first.

0 commit comments

Comments
 (0)