75
75
`&` , `&` ,
76
76
`<` , `<` ,
77
77
`>` , `>` ,
78
- `"` , `""` ,
78
+ )
79
+ formulaUnescaper = strings .NewReplacer (
80
+ `&` , `&` ,
81
+ `<` , `<` ,
82
+ `>` , `>` ,
79
83
)
80
84
// dataValidationTypeMap defined supported data validation types.
81
85
dataValidationTypeMap = map [DataValidationType ]string {
@@ -101,11 +105,6 @@ var (
101
105
}
102
106
)
103
107
104
- const (
105
- formula1Name = "formula1"
106
- formula2Name = "formula2"
107
- )
108
-
109
108
// NewDataValidation return data validation struct.
110
109
func NewDataValidation (allowBlank bool ) * DataValidation {
111
110
return & DataValidation {
@@ -151,36 +150,40 @@ func (dv *DataValidation) SetDropList(keys []string) error {
151
150
if MaxFieldLength < len (utf16 .Encode ([]rune (formula ))) {
152
151
return ErrDataValidationFormulaLength
153
152
}
154
- dv .Formula1 = fmt .Sprintf (`<%[2]s>"%[1]s"</%[2]s>` , formulaEscaper .Replace (formula ), formula1Name )
155
153
dv .Type = dataValidationTypeMap [DataValidationTypeList ]
154
+ if strings .HasPrefix (formula , "=" ) {
155
+ dv .Formula1 = formulaEscaper .Replace (formula )
156
+ return nil
157
+ }
158
+ dv .Formula1 = fmt .Sprintf (`"%s"` , strings .NewReplacer (`"` , `""` ).Replace (formulaEscaper .Replace (formula )))
156
159
return nil
157
160
}
158
161
159
162
// SetRange provides function to set data validation range in drop list, only
160
163
// accepts int, float64, string or []string data type formula argument.
161
164
func (dv * DataValidation ) SetRange (f1 , f2 interface {}, t DataValidationType , o DataValidationOperator ) error {
162
- genFormula := func (name string , val interface {}) (string , error ) {
165
+ genFormula := func (val interface {}) (string , error ) {
163
166
var formula string
164
167
switch v := val .(type ) {
165
168
case int :
166
- formula = fmt .Sprintf ("<%s>%d</%s> " , name , v , name )
169
+ formula = fmt .Sprintf ("%d " , v )
167
170
case float64 :
168
171
if math .Abs (v ) > math .MaxFloat32 {
169
172
return formula , ErrDataValidationRange
170
173
}
171
- formula = fmt .Sprintf ("<%s>% .17g</%s> " , name , v , name )
174
+ formula = fmt .Sprintf ("% .17g" , v )
172
175
case string :
173
- formula = fmt . Sprintf ( "<%s>%s</%s>" , name , v , name )
176
+ formula = v
174
177
default :
175
178
return formula , ErrParameterInvalid
176
179
}
177
180
return formula , nil
178
181
}
179
- formula1 , err := genFormula (formula1Name , f1 )
182
+ formula1 , err := genFormula (f1 )
180
183
if err != nil {
181
184
return err
182
185
}
183
- formula2 , err := genFormula (formula2Name , f2 )
186
+ formula2 , err := genFormula (f2 )
184
187
if err != nil {
185
188
return err
186
189
}
@@ -205,7 +208,7 @@ func (dv *DataValidation) SetRange(f1, f2 interface{}, t DataValidationType, o D
205
208
// dv.SetSqrefDropList("$E$1:$E$3")
206
209
// err := f.AddDataValidation("Sheet1", dv)
207
210
func (dv * DataValidation ) SetSqrefDropList (sqref string ) {
208
- dv .Formula1 = fmt . Sprintf ( "<formula1>%s</formula1>" , sqref )
211
+ dv .Formula1 = sqref
209
212
dv .Type = dataValidationTypeMap [DataValidationTypeList ]
210
213
}
211
214
@@ -256,7 +259,27 @@ func (f *File) AddDataValidation(sheet string, dv *DataValidation) error {
256
259
if nil == ws .DataValidations {
257
260
ws .DataValidations = new (xlsxDataValidations )
258
261
}
259
- ws .DataValidations .DataValidation = append (ws .DataValidations .DataValidation , dv )
262
+ dataValidation := & xlsxDataValidation {
263
+ AllowBlank : dv .AllowBlank ,
264
+ Error : dv .Error ,
265
+ ErrorStyle : dv .ErrorStyle ,
266
+ ErrorTitle : dv .ErrorTitle ,
267
+ Operator : dv .Operator ,
268
+ Prompt : dv .Prompt ,
269
+ PromptTitle : dv .PromptTitle ,
270
+ ShowDropDown : dv .ShowDropDown ,
271
+ ShowErrorMessage : dv .ShowErrorMessage ,
272
+ ShowInputMessage : dv .ShowInputMessage ,
273
+ Sqref : dv .Sqref ,
274
+ Type : dv .Type ,
275
+ }
276
+ if dv .Formula1 != "" {
277
+ dataValidation .Formula1 = & xlsxInnerXML {Content : dv .Formula1 }
278
+ }
279
+ if dv .Formula2 != "" {
280
+ dataValidation .Formula2 = & xlsxInnerXML {Content : dv .Formula2 }
281
+ }
282
+ ws .DataValidations .DataValidation = append (ws .DataValidations .DataValidation , dataValidation )
260
283
ws .DataValidations .Count = len (ws .DataValidations .DataValidation )
261
284
return err
262
285
}
@@ -270,7 +293,33 @@ func (f *File) GetDataValidations(sheet string) ([]*DataValidation, error) {
270
293
if ws .DataValidations == nil || len (ws .DataValidations .DataValidation ) == 0 {
271
294
return nil , err
272
295
}
273
- return ws .DataValidations .DataValidation , err
296
+ var dvs []* DataValidation
297
+ for _ , dv := range ws .DataValidations .DataValidation {
298
+ if dv != nil {
299
+ dataValidation := & DataValidation {
300
+ AllowBlank : dv .AllowBlank ,
301
+ Error : dv .Error ,
302
+ ErrorStyle : dv .ErrorStyle ,
303
+ ErrorTitle : dv .ErrorTitle ,
304
+ Operator : dv .Operator ,
305
+ Prompt : dv .Prompt ,
306
+ PromptTitle : dv .PromptTitle ,
307
+ ShowDropDown : dv .ShowDropDown ,
308
+ ShowErrorMessage : dv .ShowErrorMessage ,
309
+ ShowInputMessage : dv .ShowInputMessage ,
310
+ Sqref : dv .Sqref ,
311
+ Type : dv .Type ,
312
+ }
313
+ if dv .Formula1 != nil {
314
+ dataValidation .Formula1 = unescapeDataValidationFormula (dv .Formula1 .Content )
315
+ }
316
+ if dv .Formula2 != nil {
317
+ dataValidation .Formula2 = unescapeDataValidationFormula (dv .Formula2 .Content )
318
+ }
319
+ dvs = append (dvs , dataValidation )
320
+ }
321
+ }
322
+ return dvs , err
274
323
}
275
324
276
325
// DeleteDataValidation delete data validation by given worksheet name and
@@ -351,3 +400,11 @@ func (f *File) squashSqref(cells [][]int) []string {
351
400
}
352
401
return append (refs , ref )
353
402
}
403
+
404
+ // unescapeDataValidationFormula returns unescaped data validation formula.
405
+ func unescapeDataValidationFormula (val string ) string {
406
+ if strings .HasPrefix (val , "\" " ) { // Text detection
407
+ return strings .NewReplacer (`""` , `"` ).Replace (formulaUnescaper .Replace (val ))
408
+ }
409
+ return formulaUnescaper .Replace (val )
410
+ }
0 commit comments