Exemple #1
0
        public bool AddPropertiesFromRowValues <T>(object obj, int rowCount, IExcelToEnumerableOptions <T> options,
                                                   IList <Exception> exceptionsList)
        {
            _currentObject = obj;
            var           success = true;
            List <string> fieldsPresentInThisRow = (options.NotNullProperties != null && options.NotNullProperties.Any())
                ? new List <string>()
                : null;

            foreach (var item in RowValues)
            {
                var cellValue              = item.Value;
                var propertySetter         = _cellSettersDictionaryForRead[item.Key];
                var isRequiredAlreadyAdded = false;
                var invalidCast            = false;

                try
                {
                    cellValue = propertySetter.PropertyMapping != null
                        ? propertySetter.PropertyMapping(cellValue)
                        : ConvertType(cellValue, propertySetter.Type, propertySetter.RelaxedNumberMatching);

                    propertySetter.Setter(obj, cellValue);
                }
                catch (Exception e)
                {
                    if (propertySetter.PropertyMapping == null && !(e is InvalidCastException))
                    {
                        throw;
                    }

                    HandleException(cellValue, propertySetter.ColumnName, rowCount, item.Key, options,
                                    exceptionsList, e, null, "Value is invalid");
                    success     = false;
                    invalidCast = true;
                }

                if (propertySetter.Validators != null && !invalidCast)
                {
                    foreach (var validator in propertySetter.Validators)
                    {
                        if (!validator.Validator(cellValue))
                        {
                            HandleException(cellValue, propertySetter.ColumnName, rowCount, item.Key, options,
                                            exceptionsList, null,
                                            validator.ExcelToEnumerableValidationCode,
                                            validator.Message);
                            if (validator.ExcelToEnumerableValidationCode == ExcelToEnumerableValidationCode.Required)
                            {
                                isRequiredAlreadyAdded = true;
                            }

                            success = false;
                            break;
                        }
                    }
                }

                if (fieldsPresentInThisRow != null && !isRequiredAlreadyAdded)
                {
                    fieldsPresentInThisRow.Add(propertySetter.ColumnName);
                }
            }

            if (fieldsPresentInThisRow != null)
            {
                var result = CheckForMissingFields(options, fieldsPresentInThisRow, rowCount, exceptionsList);
                if (!result)
                {
                    success = false;
                }
            }

            if (options.RowNumberProperty != null)
            {
                var setter = Setters.First(x => x.PropertyName == options.RowNumberProperty);
                setter.Setter(obj, rowCount);
            }

            return(success);
        }