-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.go
More file actions
121 lines (106 loc) · 3.5 KB
/
validation.go
File metadata and controls
121 lines (106 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package gospreadsheet
// ValidationType represents the type of data validation.
type ValidationType string
const (
ValidationNone ValidationType = "none"
ValidationWhole ValidationType = "whole"
ValidationDecimal ValidationType = "decimal"
ValidationList ValidationType = "list"
ValidationDate ValidationType = "date"
ValidationTime ValidationType = "time"
ValidationTextLength ValidationType = "textLength"
ValidationCustom ValidationType = "custom"
)
// ValidationErrorStyle represents the error alert style.
type ValidationErrorStyle string
const (
ErrorStyleStop ValidationErrorStyle = "stop"
ErrorStyleWarning ValidationErrorStyle = "warning"
ErrorStyleInformation ValidationErrorStyle = "information"
)
// ValidationOperator represents the comparison operator for validation.
type ValidationOperator string
const (
ValOperatorBetween ValidationOperator = "between"
ValOperatorNotBetween ValidationOperator = "notBetween"
ValOperatorEqual ValidationOperator = "equal"
ValOperatorNotEqual ValidationOperator = "notEqual"
ValOperatorGreaterThan ValidationOperator = "greaterThan"
ValOperatorLessThan ValidationOperator = "lessThan"
ValOperatorGreaterThanOrEqual ValidationOperator = "greaterThanOrEqual"
ValOperatorLessThanOrEqual ValidationOperator = "lessThanOrEqual"
)
// DataValidation represents a data validation rule for a cell range.
type DataValidation struct {
Range string // e.g., "A1:A100"
Type ValidationType
Operator ValidationOperator
Formula1 string
Formula2 string // used for between/notBetween
AllowBlank bool
ShowInputMsg bool
ShowErrorMsg bool
ErrorStyle ValidationErrorStyle
ErrorTitle string
ErrorMessage string
PromptTitle string
PromptMessage string
}
// NewDataValidation creates a new data validation for a range.
func NewDataValidation(rangeStr string) *DataValidation {
return &DataValidation{
Range: rangeStr,
Type: ValidationNone,
AllowBlank: true,
ShowInputMsg: true,
ShowErrorMsg: true,
ErrorStyle: ErrorStyleStop,
}
}
// SetType sets the validation type and returns for chaining.
func (dv *DataValidation) SetType(t ValidationType) *DataValidation {
dv.Type = t
return dv
}
// SetOperator sets the operator and returns for chaining.
func (dv *DataValidation) SetOperator(op ValidationOperator) *DataValidation {
dv.Operator = op
return dv
}
// SetFormula1 sets the first formula/value.
func (dv *DataValidation) SetFormula1(f string) *DataValidation {
dv.Formula1 = f
return dv
}
// SetFormula2 sets the second formula/value (for between/notBetween).
func (dv *DataValidation) SetFormula2(f string) *DataValidation {
dv.Formula2 = f
return dv
}
// SetErrorMessage sets the error alert message.
func (dv *DataValidation) SetErrorMessage(title, message string) *DataValidation {
dv.ErrorTitle = title
dv.ErrorMessage = message
dv.ShowErrorMsg = true
return dv
}
// SetPromptMessage sets the input prompt message.
func (dv *DataValidation) SetPromptMessage(title, message string) *DataValidation {
dv.PromptTitle = title
dv.PromptMessage = message
dv.ShowInputMsg = true
return dv
}
// SetListValues is a convenience method for list validation with explicit values.
func (dv *DataValidation) SetListValues(values []string) *DataValidation {
dv.Type = ValidationList
joined := ""
for i, v := range values {
if i > 0 {
joined += ","
}
joined += `"` + v + `"`
}
dv.Formula1 = joined
return dv
}