/// <summary> /// Maps specified property to excel column by index. /// This can be combined with <see cref="MapByName{TProperty}"/>. /// When <see cref="FirstRowContainsColumnNames"/> is false, then all properties have to be mapped by index. /// </summary> /// <param name="propertyExpression">Expression for specifying the property which is intended for mapping</param> /// <param name="columnIndex">The column index from excel</param> /// <typeparam name="TProperty">The type of specified property from expression</typeparam> /// <exception cref="ArgumentException">When column index is less than 0</exception> public void MapByIndex <TProperty>(Expression <Func <T, TProperty> > propertyExpression, int columnIndex) { if (columnIndex < 0) { throw new ArgumentException(MessageDefaults.InvalidColumnIndex(columnIndex)); } var propertyMap = GetPropertyMapOrThrowException(propertyExpression); propertyMap.MapStrategy = ExcelIteratorPropertyMapStrategy.ByIndex; propertyMap.ColumnIndex = columnIndex; }
/// <summary> /// Maps specified property to excel column by index. /// Extremely useful when property name in a class cannot be mapped to an excel column by default. /// Works only when <see cref="FirstRowContainsColumnNames"/> is set to true. /// </summary> /// <param name="propertyExpression">Expression for specifying the property which is intended for mapping</param> /// <param name="columnName">Column name</param> /// <typeparam name="TProperty">The type of specified property from expression</typeparam> /// <exception cref="ArgumentException">When column name is empty</exception> public void MapByName <TProperty>(Expression <Func <T, TProperty> > propertyExpression, string columnName) { if (string.IsNullOrWhiteSpace(columnName)) { throw new ArgumentException(MessageDefaults.InvalidColumnName(columnName)); } var propertyMap = GetPropertyMapOrThrowException(propertyExpression); propertyMap.MapStrategy = ExcelIteratorPropertyMapStrategy.ByName; propertyMap.ColumnName = columnName; }
private ExcelIteratorPropertyMap GetPropertyMapOrThrowException <TProperty>(Expression <Func <T, TProperty> > propertyExpression) { var propertyName = (propertyExpression.Body as MemberExpression)?.Member.Name; if (string.IsNullOrWhiteSpace(propertyName)) { throw new ArgumentException(MessageDefaults.InvalidPropertyExpression, nameof(propertyExpression)); } var propertyMap = _propertyMaps.FirstOrDefault(p => p.Property.Name.Equals(propertyName)) ?? throw new ArgumentException(MessageDefaults.PropertyNotFound <T>(propertyName), nameof(propertyExpression)); return(propertyMap); }
private void EnsureCorrectSheetSelected() { if (string.IsNullOrWhiteSpace(_configuration.SheetName)) { return; } while (!_dataReader.Name.Equals(_configuration.SheetName, StringComparison.OrdinalIgnoreCase)) { if (!_dataReader.NextResult()) { throw new InvalidOperationException(MessageDefaults.SheetNotFound(_configuration.SheetName)); } } }
internal static bool TryValidPath(this Type targetType, ParsedPath parsedPath, out string segment, out string message) { var location = targetType; foreach (var parsedSegment in parsedPath.Segments) { if (!location.TryGetJsonProperty(parsedSegment, out var jsonProperty)) { segment = parsedSegment; message = MessageDefaults.PathSegmentNotFound(parsedSegment); return(false); } location = jsonProperty.PropertyType; } segment = null; message = null; return(true); }
private int ResolveIndexByName(ExcelIteratorPropertyMap excelIteratorPropertyMap) => _columnIndexAndNamePairs.TryGetValue(excelIteratorPropertyMap.ColumnName, out var columnIndex) ? columnIndex : throw new InvalidOperationException( MessageDefaults.CannotMapColumnByName(excelIteratorPropertyMap.ColumnName));