2525using DynamicExcelProvider . Enums ;
2626using DynamicExcelProvider . Mapper ;
2727using DynamicExcelProvider . Models . Request . Configuration . Property ;
28+ using DynamicExcelProvider . Models . Request . Configuration . Template ;
2829using DynamicExcelProvider . WorkXCore . Extensions ;
30+ using System ;
2931using System . Collections . Generic ;
3032using System . Linq ;
3133using System . Reflection ;
3234
33- #endregion
34-
3535// ReSharper disable PossibleMultipleEnumeration
3636
37+ #endregion
38+
3739namespace DynamicExcelProvider . Helpers
3840{
3941 /// -------------------------------------------------------------------------------------------------
@@ -53,7 +55,9 @@ internal static class DataValidationsBuildHelper
5355 /// The DataValidations.
5456 /// </returns>
5557 /// =================================================================================================
56- internal static DataValidations BuildSheetDataValidations ( ref IEnumerable < PropertyInfo > properties , ref IReadOnlyCollection < PropTranslateModel > outputProps )
58+ internal static DataValidations BuildSheetDataValidations (
59+ ref IEnumerable < PropertyInfo > properties ,
60+ ref IReadOnlyCollection < PropTranslateModel > outputProps )
5761 {
5862 try
5963 {
@@ -92,11 +96,27 @@ internal static DataValidations BuildSheetDataValidations(ref IEnumerable<Proper
9296 dataValidation . ShowErrorMessage = new BooleanValue ( true ) ;
9397 }
9498
95- if ( validationAttribute . MinValue . IsNull ( ) . IsFalse ( ) )
96- dataValidation . Formula1 = new Formula1 ( $ "{ validationAttribute . MinValue } ") ;
99+ if ( validationAttribute . MinValue . IsNotNull ( ) )
100+ {
101+ if ( validationAttribute . ValidationType == ValidationType . Date )
102+ {
103+ var parseDate = Convert . ToDateTime ( validationAttribute . MinValue ) ;
104+ dataValidation . Formula1 = new Formula1 ( "DATE({0},{1},{2})" . FormatWith ( parseDate . Year , parseDate . Month , parseDate . Day ) ) ;
105+ }
106+ else
107+ dataValidation . Formula1 = new Formula1 ( $ "{ validationAttribute . MinValue } ") ;
108+ }
97109
98- if ( validationAttribute . MaxValue . IsNull ( ) . IsFalse ( ) )
99- dataValidation . Formula2 = new Formula2 ( $ "{ validationAttribute . MaxValue } ") ;
110+ if ( validationAttribute . MaxValue . IsNotNull ( ) )
111+ {
112+ if ( validationAttribute . ValidationType == ValidationType . Date )
113+ {
114+ var parseDate = Convert . ToDateTime ( validationAttribute . MaxValue ) ;
115+ dataValidation . Formula2 = new Formula2 ( "DATE({0},{1},{2})" . FormatWith ( parseDate . Year , parseDate . Month , parseDate . Day ) ) ;
116+ }
117+ else
118+ dataValidation . Formula2 = new Formula2 ( $ "{ validationAttribute . MaxValue } ") ;
119+ }
100120
101121 if ( validationAttribute . AllowedValues . IsNullOrEmptyEnumerable ( ) . IsFalse ( ) )
102122 dataValidation . Formula1 = new Formula1 ( $ "\" { validationAttribute . AllowedValues . ListToString ( "," ) } \" ") ;
@@ -115,5 +135,89 @@ internal static DataValidations BuildSheetDataValidations(ref IEnumerable<Proper
115135 return null ;
116136 }
117137 }
138+
139+ /// -------------------------------------------------------------------------------------------------
140+ /// <summary>
141+ /// Builds sheet data validations.
142+ /// </summary>
143+ /// <param name="sheetValidations">[in,out] The sheet validations.</param>
144+ /// <returns>
145+ /// The DataValidations.
146+ /// </returns>
147+ /// =================================================================================================
148+ internal static DataValidations BuildSheetDataValidations (
149+ ref IEnumerable < TemplateDataValidation > sheetValidations )
150+ {
151+ try
152+ {
153+ var dataValidations = new DataValidations ( ) ;
154+
155+ foreach ( var validation in sheetValidations )
156+ {
157+ var sheetColumnLetter = SpreadsheetExtensions . GetExcelColumnName ( validation . PropertyIndex ) . Response ;
158+
159+ var dataValidation = new DataValidation
160+ {
161+ SequenceOfReferences = new ListValue < StringValue >
162+ {
163+ InnerText = $ "{ sheetColumnLetter } 2:{ sheetColumnLetter } 1048576"
164+ } ,
165+ Type = new EnumValue < DataValidationValues > ( EnumMapper . MapValidation ( validation . ValidationType ) ) ,
166+ Operator = validation . ValidationType != ValidationType . List
167+ ? new EnumValue < DataValidationOperatorValues > ( EnumMapper . MapOperator ( validation . OperatorType ) )
168+ : null ,
169+ AllowBlank = new BooleanValue ( validation . AllowEmpty . IsTrue ( ) )
170+ } ;
171+
172+ if ( validation . PromptMessage . IsNullOrEmpty ( ) . IsFalse ( ) )
173+ {
174+ dataValidation . Prompt = validation . PromptMessage ;
175+ dataValidation . ShowInputMessage = new BooleanValue ( true ) ;
176+ }
177+
178+ if ( validation . ErrorMessage . IsNullOrEmpty ( ) . IsFalse ( ) )
179+ {
180+ dataValidation . Error = validation . ErrorMessage ;
181+ dataValidation . ShowErrorMessage = new BooleanValue ( true ) ;
182+ }
183+
184+ if ( validation . MinValue . IsNotNull ( ) )
185+ {
186+ if ( validation . ValidationType == ValidationType . Date )
187+ {
188+ var parseDate = Convert . ToDateTime ( validation . MinValue ) ;
189+ dataValidation . Formula1 = new Formula1 ( "DATE({0},{1},{2})" . FormatWith ( parseDate . Year , parseDate . Month , parseDate . Day ) ) ;
190+ }
191+ else
192+ dataValidation . Formula1 = new Formula1 ( $ "{ validation . MinValue } ") ;
193+ }
194+
195+ if ( validation . MaxValue . IsNotNull ( ) )
196+ {
197+ if ( validation . ValidationType == ValidationType . Date )
198+ {
199+ var parseDate = Convert . ToDateTime ( validation . MaxValue ) ;
200+ dataValidation . Formula2 = new Formula2 ( "DATE({0},{1},{2})" . FormatWith ( parseDate . Year , parseDate . Month , parseDate . Day ) ) ;
201+ }
202+ else
203+ dataValidation . Formula2 = new Formula2 ( $ "{ validation . MaxValue } ") ;
204+ }
205+
206+ if ( validation . AllowedValues . IsNullOrEmptyEnumerable ( ) . IsFalse ( ) )
207+ dataValidation . Formula1 = new Formula1 ( $ "\" { validation . AllowedValues . ListToString ( "," ) } \" ") ;
208+
209+ if ( validation . ShowListInDropDown . IsTrue ( ) )
210+ dataValidation . ShowDropDown = new BooleanValue ( true ) ;
211+
212+ dataValidations . Append ( dataValidation ) ;
213+ }
214+
215+ return dataValidations ;
216+ }
217+ catch
218+ {
219+ return null ;
220+ }
221+ }
118222 }
119223}
0 commit comments