Beispiel #1
0
        protected virtual void HandleAuditProperty(int rowIndex, TEntity row, PropertyInfo targetProperty, ExcelPropertyConfiguration columnConfiguration)
        {
            switch (columnConfiguration.AuditPropertyType)
            {
            case ExcelAuditProperties.RowId:
                if (targetProperty.PropertyType == typeof(int))
                {
                    targetProperty.SetValue(row, rowIndex);
                }
                return;

            default:
                throw new NotImplementedException($"Error in '{nameof(EPPlusDomainRepository<TEntity, TKey>)}'. " +
                                                  $"The method'{nameof(HandleAuditProperty)}' does not support the enum '{nameof(ExcelAuditProperties)}' value of '{columnConfiguration.AuditPropertyType}'.");
            }
        }
Beispiel #2
0
        protected virtual void ParseConfiguredColumn(int rowIndex, TEntity row, PropertyInfo targetProperty, ExcelPropertyConfiguration columnConfiguration)
        {
            //Excel attribute defined the target column
            var propertyColIndex = columnConfiguration.ColumnIndex;

            if (ExcelWorkSheet == null)
            {
                throw new NullReferenceException($"{nameof(EPPlusDomainRepository<TEntity, TKey>)}.{nameof(ExcelWorkSheet)} is null." +
                                                 $" Ensure calling method '{nameof(SetExcelDataSource)}' or passing a not null '{nameof(ExcelFileOptions)}.{nameof(ExcelFileOptions.ExcelFileStream)}'" +
                                                 $" before using data access methods");
            }

            if (ExcelWorkSheet.Cells[rowIndex, propertyColIndex].Value == null)
            {
                targetProperty.SetValue(row, columnConfiguration.DefaultValue);
            }
            else
            {
                if (columnConfiguration.CellParserFunction != null)
                {
                    //custom string parser was supplied
                    var stringValue = ExcelWorkSheet.Cells[rowIndex, propertyColIndex].Text;
                    var value       = columnConfiguration.CellParserFunction(stringValue);
                    targetProperty.SetValue(row, value ?? columnConfiguration.DefaultValue);
                }
                else
                {
                    try {
                        ParseGenericCellValue(row, rowIndex, propertyColIndex, targetProperty);
                    }
                    catch (Exception e) {
                        if (Context.Options.PropagageParsingError)
                        {
                            throw e;
                        }

                        _logger?.LogError(e, "Generic Parsing of Value was not possible. Default value will be applied");
                        targetProperty.SetValue(row, columnConfiguration.DefaultValue);
                    }
                }
            }
        }
        private static void DataPropertiesMustHaveUniqueColumnAdresses(ExcelEntityBuilder entityBuilder)
        {
            var adresses = entityBuilder.ColumnConfigurations.Values.Where(x => x.Configuration.AuditPropertyType == ExcelAuditProperties.NO_AUDIT_FIELD)
                           .Select(x => x.Configuration.ColumnIndex);

            if (adresses.Any(x => x == default))
            {
                throw new Exception($"Some columns where configured without a column index." +
                                    $" Use '{nameof(ExcelPropertyBuilder<object>)}.{nameof(ExcelPropertyBuilder<object>.Column)}' to set the column index.");
            }

            var duplicatedAddresses = adresses.CountDistinct().Where(x => x.Value > 1).Select(x => ExcelPropertyConfiguration.ColumnAdress(x.Key));

            if (duplicatedAddresses.Count() > 0)
            {
                throw new Exception($"The excel column adresses [{string.Join(",", duplicatedAddresses)}] where defined multiple times.");
            }
        }