add datavalidation test and fixed struct bug issue #240

This commit is contained in:
rentiansheng 2018-09-01 19:38:30 +08:00
parent 0548852190
commit 24a8d64f93
3 changed files with 48 additions and 20 deletions

View File

@ -33,9 +33,9 @@ type DataValidationErrorStyle int
// Data validation error styles // Data validation error styles
const ( const (
_ DataValidationErrorStyle = iota _ DataValidationErrorStyle = iota
DataValidationStyleStop DataValidationErrorStyleStop
DataValidationStyleWarning DataValidationErrorStyleWarning
DataValidationStyleInformation DataValidationErrorStyleInformation
) )
// Data validation error styles // Data validation error styles
@ -71,16 +71,16 @@ func NewDataValidation(allowBlank bool) *DataValidation {
} }
// SetError set error notice // SetError set error notice
func (dd *DataValidation) SetError(style DataValidationErrorStyle, title, msg *string) { func (dd *DataValidation) SetError(style DataValidationErrorStyle, title, msg string) {
dd.Error = msg dd.Error = &msg
dd.ErrorTitle = title dd.ErrorTitle = &title
strStyle := styleStop strStyle := styleStop
switch style { switch style {
case DataValidationStyleStop: case DataValidationErrorStyleStop:
strStyle = styleStop strStyle = styleStop
case DataValidationStyleWarning: case DataValidationErrorStyleWarning:
strStyle = styleWarning strStyle = styleWarning
case DataValidationStyleInformation: case DataValidationErrorStyleInformation:
strStyle = styleInformation strStyle = styleInformation
} }
@ -89,10 +89,10 @@ func (dd *DataValidation) SetError(style DataValidationErrorStyle, title, msg *s
} }
// SetInput set prompt notice // SetInput set prompt notice
func (dd *DataValidation) SetInput(title, msg *string) { func (dd *DataValidation) SetInput(title, msg string) {
dd.ShowInputMessage = convBoolToStr(true) dd.ShowInputMessage = convBoolToStr(true)
dd.PromptTitle = title dd.PromptTitle = &title
dd.Prompt = msg dd.Prompt = &msg
} }
// SetDropList data validation list // SetDropList data validation list
@ -109,7 +109,7 @@ func (dd *DataValidation) SetRange(f1, f2 int, t DataValidationType, o DataValid
if dataValidationFormulaStrLen < len(dd.Formula1) || dataValidationFormulaStrLen < len(dd.Formula2) { if dataValidationFormulaStrLen < len(dd.Formula1) || dataValidationFormulaStrLen < len(dd.Formula2) {
return fmt.Errorf(dataValidationFormulaStrLenErr) return fmt.Errorf(dataValidationFormulaStrLenErr)
} }
switch o { /*switch o {
case DataValidationOperatorBetween: case DataValidationOperatorBetween:
if f1 > f2 { if f1 > f2 {
tmp := formula1 tmp := formula1
@ -122,7 +122,7 @@ func (dd *DataValidation) SetRange(f1, f2 int, t DataValidationType, o DataValid
formula1 = formula2 formula1 = formula2
formula2 = tmp formula2 = tmp
} }
} }*/
dd.Formula1 = formula1 dd.Formula1 = formula1
dd.Formula2 = formula2 dd.Formula2 = formula2
@ -190,7 +190,3 @@ func (f *File) AddDataValidation(sheet string, dv *DataValidation) {
xlsx.DataValidations.DataValidation = append(xlsx.DataValidations.DataValidation, dv) xlsx.DataValidations.DataValidation = append(xlsx.DataValidations.DataValidation, dv)
xlsx.DataValidations.Count = len(xlsx.DataValidations.DataValidation) xlsx.DataValidations.Count = len(xlsx.DataValidations.DataValidation)
} }
func (f *File) GetDataValidation(sheet, sqref string) {
}

32
datavalidation_test.go Normal file
View File

@ -0,0 +1,32 @@
package excelize
import (
"testing"
)
func TestDataValidation(t *testing.T) {
xlsx := NewFile()
dvRange := NewDataValidation(true)
dvRange.Sqref = "A1:B2"
dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorBetween)
dvRange.SetError(DataValidationErrorStyleStop, "error title", "error body")
xlsx.AddDataValidation("Sheet1", dvRange)
dvRange = NewDataValidation(true)
dvRange.Sqref = "A3:B4"
dvRange.SetRange(10, 20, DataValidationTypeWhole, DataValidationOperatorGreaterThan)
dvRange.SetInput("input title", "input body")
xlsx.AddDataValidation("Sheet1", dvRange)
dvRange = NewDataValidation(true)
dvRange.Sqref = "A5:B6"
dvRange.SetDropList([]string{"1", "2", "3"})
xlsx.AddDataValidation("Sheet1", dvRange)
// Test write file to given path.
err := xlsx.SaveAs("./test/Bookdatavalition.xlsx")
if err != nil {
t.Error(err)
}
}

View File

@ -18,7 +18,7 @@ type xlsxWorksheet struct {
MergeCells *xlsxMergeCells `xml:"mergeCells"` MergeCells *xlsxMergeCells `xml:"mergeCells"`
PhoneticPr *xlsxPhoneticPr `xml:"phoneticPr"` PhoneticPr *xlsxPhoneticPr `xml:"phoneticPr"`
ConditionalFormatting []*xlsxConditionalFormatting `xml:"conditionalFormatting"` ConditionalFormatting []*xlsxConditionalFormatting `xml:"conditionalFormatting"`
DataValidations *xlsxDataValidations `xml:"dataValidations"` DataValidations *xlsxDataValidations `xml:"dataValidations,omitempty"`
Hyperlinks *xlsxHyperlinks `xml:"hyperlinks"` Hyperlinks *xlsxHyperlinks `xml:"hyperlinks"`
PrintOptions *xlsxPrintOptions `xml:"printOptions"` PrintOptions *xlsxPrintOptions `xml:"printOptions"`
PageMargins *xlsxPageMargins `xml:"pageMargins"` PageMargins *xlsxPageMargins `xml:"pageMargins"`
@ -298,7 +298,7 @@ type xlsxDataValidations struct {
DisablePrompts bool `xml:"disablePrompts,attr,omitempty"` DisablePrompts bool `xml:"disablePrompts,attr,omitempty"`
XWindow int `xml:"xWindow,attr,omitempty"` XWindow int `xml:"xWindow,attr,omitempty"`
YWindow int `xml:"yWindow,attr,omitempty"` YWindow int `xml:"yWindow,attr,omitempty"`
DataValidation []*DataValidation `xml:"dataValidation,innerxml"` DataValidation []*DataValidation `xml:"dataValidation"`
} }
type DataValidation struct { type DataValidation struct {