@@ -663,6 +663,144 @@ public void AddDropdownListValidation(string cellReference, string[] options)
663663 dataValidations . AppendChild ( dataValidation ) ;
664664 }
665665
666+ public void ApplyValidation ( string cellReference , ValidationRule rule )
667+ {
668+ DataValidation dataValidation = CreateDataValidation ( cellReference , rule ) ;
669+ AddDataValidation ( dataValidation ) ;
670+ }
671+
672+ public ValidationRule GetValidationRule ( string cellReference )
673+ {
674+ if ( _worksheetPart == null )
675+ {
676+ throw new InvalidOperationException ( "Worksheet part is not loaded." ) ;
677+ }
678+
679+ var dataValidations = _worksheetPart . Worksheet . Descendants < DataValidation > ( ) ;
680+
681+ foreach ( var dataValidation in dataValidations )
682+ {
683+ if ( dataValidation . SequenceOfReferences . InnerText . Contains ( cellReference ) )
684+ {
685+ return CreateValidationRuleFromDataValidation ( dataValidation ) ;
686+ }
687+ }
688+
689+ return null ; // No validation rule found for the specified cell
690+ }
691+
692+ private ValidationRule CreateValidationRuleFromDataValidation ( DataValidation dataValidation )
693+ {
694+ // Determine the type of validation
695+ ValidationType type = DetermineValidationType ( dataValidation . Type ) ;
696+
697+ // Depending on the type, create a corresponding ValidationRule
698+ switch ( type )
699+ {
700+ case ValidationType . List :
701+ // Assuming list validations use a comma-separated list of options in Formula1
702+ var options = dataValidation . Formula1 . Text . Split ( ',' ) ;
703+ return new ValidationRule ( options ) ;
704+
705+ case ValidationType . CustomFormula :
706+ return new ValidationRule ( dataValidation . Formula1 . Text ) ;
707+
708+ // Add cases for other validation types (e.g., Date, WholeNumber, Decimal, TextLength)
709+ // ...
710+
711+ default :
712+ throw new NotImplementedException ( "Validation type not supported." ) ;
713+ }
714+ }
715+
716+ private ValidationType DetermineValidationType ( DataValidationValues openXmlType )
717+ {
718+ // Map the OpenXML DataValidationValues to your ValidationType enum
719+ // This mapping depends on how closely your ValidationType enum aligns with OpenXML's types
720+ // Example mapping:
721+ switch ( openXmlType )
722+ {
723+ case DataValidationValues . List :
724+ return ValidationType . List ;
725+ case DataValidationValues . Custom :
726+ return ValidationType . CustomFormula ;
727+ // Map other types...
728+ default :
729+ throw new NotImplementedException ( "Validation type not supported." ) ;
730+ }
731+ }
732+
733+
734+
735+ private DataValidation CreateDataValidation ( string cellReference , ValidationRule rule )
736+ {
737+ DataValidation dataValidation = new DataValidation
738+ {
739+ SequenceOfReferences = new ListValue < StringValue > { InnerText = cellReference } ,
740+ ShowErrorMessage = true ,
741+ ErrorTitle = rule . ErrorTitle ,
742+ Error = rule . ErrorMessage
743+ } ;
744+
745+ switch ( rule . Type )
746+ {
747+ case ValidationType . List :
748+ dataValidation . Type = DataValidationValues . List ;
749+ dataValidation . Formula1 = new Formula1 ( $ "\" { string . Join ( "," , rule . Options ) } \" ") ;
750+ break ;
751+
752+ case ValidationType . Date :
753+ dataValidation . Type = DataValidationValues . Date ;
754+ dataValidation . Operator = DataValidationOperatorValues . Between ;
755+ dataValidation . Formula1 = new Formula1 ( rule . MinValue . HasValue ? rule . MinValue . Value . ToString ( ) : "0" ) ;
756+ dataValidation . Formula2 = new Formula2 ( rule . MaxValue . HasValue ? rule . MaxValue . Value . ToString ( ) : "0" ) ;
757+ break ;
758+
759+ case ValidationType . WholeNumber :
760+ dataValidation . Type = DataValidationValues . Whole ;
761+ dataValidation . Operator = DataValidationOperatorValues . Between ;
762+ dataValidation . Formula1 = new Formula1 ( rule . MinValue . HasValue ? rule . MinValue . Value . ToString ( ) : "0" ) ;
763+ dataValidation . Formula2 = new Formula2 ( rule . MaxValue . HasValue ? rule . MaxValue . Value . ToString ( ) : "0" ) ;
764+ break ;
765+
766+ case ValidationType . Decimal :
767+ dataValidation . Type = DataValidationValues . Decimal ;
768+ dataValidation . Operator = DataValidationOperatorValues . Between ;
769+ dataValidation . Formula1 = new Formula1 ( rule . MinValue . HasValue ? rule . MinValue . Value . ToString ( ) : "0" ) ;
770+ dataValidation . Formula2 = new Formula2 ( rule . MaxValue . HasValue ? rule . MaxValue . Value . ToString ( ) : "0" ) ;
771+ break ;
772+
773+ case ValidationType . TextLength :
774+ dataValidation . Type = DataValidationValues . TextLength ;
775+ dataValidation . Operator = DataValidationOperatorValues . Between ;
776+ dataValidation . Formula1 = new Formula1 ( rule . MinValue . HasValue ? rule . MinValue . Value . ToString ( ) : "0" ) ;
777+ dataValidation . Formula2 = new Formula2 ( rule . MaxValue . HasValue ? rule . MaxValue . Value . ToString ( ) : "0" ) ;
778+ break ;
779+
780+ case ValidationType . CustomFormula :
781+ dataValidation . Type = DataValidationValues . Custom ;
782+ dataValidation . Formula1 = new Formula1 ( rule . CustomFormula ) ;
783+ break ;
784+
785+ default :
786+ throw new ArgumentException ( "Unsupported validation type." ) ;
787+ }
788+
789+ return dataValidation ;
790+ }
791+
792+
793+ private void AddDataValidation ( DataValidation dataValidation )
794+ {
795+ var dataValidations = _worksheetPart . Worksheet . GetFirstChild < DataValidations > ( ) ;
796+ if ( dataValidations == null )
797+ {
798+ dataValidations = new DataValidations ( ) ;
799+ _worksheetPart . Worksheet . AppendChild ( dataValidations ) ;
800+ }
801+ dataValidations . AppendChild ( dataValidation ) ;
802+ }
803+
666804
667805 private ( uint row , uint column ) ParseCellReference ( string cellReference )
668806 {
0 commit comments