/// <summary> /// Map property to a column by specified column index(zero-based) and property selector. /// </summary> /// <typeparam name="T">The target object type.</typeparam> /// <param name="mapper">The <see cref="Mapper"/> object.</param> /// <param name="columnIndex">The column index.</param> /// <param name="propertySelector">Property selector.</param> /// <param name="exportedColumnName">The column name for export.</param> /// <param name="tryTake">The function try to import from cell value to the target object.</param> /// <param name="tryPut">The function try to export source object to the cell.</param> /// <returns>The mapper object.</returns> public static Mapper Map <T>(this Mapper mapper, ushort columnIndex, Expression <Func <T, object> > propertySelector, string exportedColumnName, Func <IColumnInfo, object, bool> tryTake = null, Func <IColumnInfo, object, bool> tryPut = null) { if (mapper == null) { throw new ArgumentNullException(nameof(mapper)); } var pi = MapHelper.GetPropertyInfoByExpression(propertySelector); if (pi == null) { throw new InvalidOperationException($"Cannot find the property specified by the selector."); } var columnAttribute = new ColumnAttribute { Property = pi, Index = columnIndex, Name = exportedColumnName, TryPut = tryPut, TryTake = tryTake, Ignored = false }; return(mapper.Map(columnAttribute)); }
/* * Removed this method in v3 since this is rarely used but just increased internal complexity. * /// <summary> /// Specify the built-in format. /// </summary> /// <typeparam name="T">The target object type.</typeparam> /// <param name="builtinFormat">The built-in format, see https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html for possible values.</param> /// <param name="propertySelector">Property selector.</param> /// <returns>The mapper object.</returns> [Obsolete("Builtin format will not be supported in next major release!")] public Mapper Format<T>(short builtinFormat, Expression<Func<T, object>> propertySelector) { var pi = MapHelper.GetPropertyInfoByExpression(propertySelector); if (pi == null) return this; new ColumnAttribute { Property = pi, BuiltinFormat = builtinFormat }.MergeTo(Attributes); return this; } */ /// <summary> /// Specify the custom format. /// </summary> /// <typeparam name="T">The target object type.</typeparam> /// <param name="customFormat">The custom format, see https://support.office.com/en-nz/article/Create-or-delete-a-custom-number-format-78f2a361-936b-4c03-8772-09fab54be7f4 for the syntax.</param> /// <param name="propertySelector">Property selector.</param> /// <returns>The mapper object.</returns> public Mapper Format<T>(string customFormat, Expression<Func<T, object>> propertySelector) { var pi = MapHelper.GetPropertyInfoByExpression(propertySelector); if (pi == null) return this; new ColumnAttribute { Property = pi, CustomFormat = customFormat }.MergeTo(Attributes); return this; }
/// <summary> /// Specify to ignore a property. Ignored property will not be mapped for import and export. /// </summary> /// <typeparam name="T">The target object type.</typeparam> /// <param name="propertySelector">Property selector.</param> /// <returns>The mapper object.</returns> public Mapper Ignore<T>(Expression<Func<T, object>> propertySelector) { var pi = MapHelper.GetPropertyInfoByExpression(propertySelector); if (pi == null) return this; new ColumnAttribute { Property = pi, Ignored = true }.MergeTo(Attributes); return this; }
/// <summary> /// Specify the built-in format. /// </summary> /// <typeparam name="T">The target object type.</typeparam> /// <param name="builtinFormat">The built-in format, see https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html for possible values.</param> /// <param name="propertySelector">Property selector.</param> /// <returns>The mapper object.</returns> public Mapper Format <T>(short builtinFormat, Expression <Func <T, object> > propertySelector) { var pi = MapHelper.GetPropertyInfoByExpression(propertySelector); if (pi == null) { return(this); } new ColumnAttribute { Property = pi, BuiltinFormat = builtinFormat }.MergeTo(Attributes); return(this); }
/// <summary> /// Specify to use last non-blank value from above cell for a property. /// Typically to address the blank cell issue in merged cells. /// </summary> /// <typeparam name="T">The target object type.</typeparam> /// <param name="propertySelector">Property selector.</param> /// <returns>The mapper object.</returns> public Mapper UseLastNonBlankValue <T>(Expression <Func <T, object> > propertySelector) { var pi = MapHelper.GetPropertyInfoByExpression(propertySelector); if (pi == null) { return(this); } new ColumnAttribute { Property = pi, UseLastNonBlankValue = true }.MergeTo(Attributes); return(this); }
/// <summary> /// Ignores all errors for the specified property. /// </summary> /// <param name="mapper">The <see cref="Mapper"/> object.</param> /// <param name="propertySelector">Property selector.</param> /// <returns>The mapper object.</returns> public static Mapper IgnoreErrorsFor <T>(this Mapper mapper, Expression <Func <T, object> > propertySelector) { var pi = MapHelper.GetPropertyInfoByExpression(propertySelector); if (pi == null) { throw new InvalidOperationException($"Cannot find the property specified by the selector."); } var columnAttribute = new ColumnAttribute { Property = pi, IgnoreErrors = true }; return(mapper.Map(columnAttribute)); }
/// <summary> /// Map property to a column by specified column index(zero-based) and property selector. /// </summary> /// <typeparam name="T">The target object type.</typeparam> /// <param name="mapper">The <see cref="Mapper"/> object.</param> /// <param name="columnIndex">The column index.</param> /// <param name="propertySelector">Property selector.</param> /// <param name="tryTake">The function try to import from cell value to the target object.</param> /// <param name="tryPut">The function try to export source object to the cell.</param> /// <returns>The mapper object.</returns> public static Mapper Map <T>(this Mapper mapper, ushort columnIndex, Expression <Func <T, object> > propertySelector, Func <IColumnInfo, object, bool> tryTake = null, Func <IColumnInfo, object, bool> tryPut = null) { if (mapper == null) { throw new ArgumentNullException(nameof(mapper)); } var pi = MapHelper.GetPropertyInfoByExpression(propertySelector); if (pi == null) { throw new InvalidOperationException($"Cannot find the property specified by the selector."); } return(mapper.Map(columnIndex, pi, tryTake, tryPut)); }
/// <summary> /// Map property to a column by specified column index(zero-based). /// </summary> /// <typeparam name="T">The target object type.</typeparam> /// <param name="columnIndex">The column index.</param> /// <param name="propertySelector">Property selector.</param> /// <param name="resolverType"> /// The type of custom header and cell resolver that derived from <see cref="IColumnResolver{TTarget}"/>. /// </param> /// <returns>The mapper object.</returns> public Mapper Map <T>(ushort columnIndex, Expression <Func <T, object> > propertySelector, Type resolverType = null) { var pi = MapHelper.GetPropertyInfoByExpression(propertySelector); if (pi == null) { return(this); } new ColumnAttribute { Property = pi, Index = columnIndex, ResolverType = resolverType, Ignored = false }.MergeTo(Attributes); return(this); }
/// <summary> /// Map property to a column by specified column name. /// </summary> /// <typeparam name="T">The target object type.</typeparam> /// <param name="columnName">The column name.</param> /// <param name="propertySelector">Property selector.</param> /// <param name="resolverType"> /// The type of custom header and cell resolver that derived from <see cref="IColumnResolver{TTarget}"/>. /// </param> /// <returns>The mapper object.</returns> public Mapper Map <T>(string columnName, Expression <Func <T, object> > propertySelector, Type resolverType = null) { if (columnName == null) { throw new ArgumentNullException(nameof(columnName)); } var pi = MapHelper.GetPropertyInfoByExpression(propertySelector); if (pi == null) { return(this); } new ColumnAttribute { Property = pi, Name = columnName, ResolverType = resolverType, Ignored = false }.MergeTo(Attributes); return(this); }