diff --git a/ExcelMapper.Tests/Tests.cs b/ExcelMapper.Tests/Tests.cs index aa9fe19..ef6ff6f 100644 --- a/ExcelMapper.Tests/Tests.cs +++ b/ExcelMapper.Tests/Tests.cs @@ -1199,9 +1199,12 @@ private class GetterSetterProduct { public string Name { get; set; } public string RedName { get; set; } + public int Number { get; set; } + public double Price { get; set; } public DateTime? OfferEnd { get; set; } public string OfferEndToString { get; set; } public long OfferEndToLong { get; set; } + public double Value { get; set; } public override bool Equals(object obj) { @@ -1220,7 +1223,11 @@ public void GetterSetterTest() { var excel = new ExcelMapper(@"../../../xlsx/ProductsConvert.xlsx") { TrackObjects = true }; - excel.AddMapping("Name", p => p.Name); + excel.AddMapping("Name", p => p.Name) + .SetPropertyUsing((entity, value, cell) => + { + return value; + }); excel.AddMapping("Name", p => p.RedName) .FromExcelOnly() .SetPropertyUsing((v, c) => @@ -1261,6 +1268,41 @@ public void GetterSetterTest() return dt.ToBinary(); }); + excel.AddMapping("Number", p => p.Number) + .SetCellUsing((args) => + { + args.Cell.SetCellValue(args.Value); + }) + .SetPropertyUsing((args) => + { + return Convert.ToInt32(args.Value); + }); + + excel.AddMapping("Price", p => p.Price) + .SetCellUsing((cell, value) => + { + cell.SetCellValue(value); + }) + .SetPropertyUsing((entity, value) => + { + return Convert.ToDouble(value); + }); + + excel.AddMapping("Value", p => p.Value) + .SetCellUsing((args) => + { + args.Cell.SetCellValue(args.Data.Number * args.Data.Price); + }) + .SetPropertyUsing((args) => + { + var value = 0.0; + if(args.Cell.CellType == CellType.Formula) + { + value = args.Data.Number * args.Data.Price; + } + return value; + }); + var products = excel.Fetch().ToList(); Assert.That(products[0].RedName, Is.Null); diff --git a/ExcelMapper/ColumnInfo.cs b/ExcelMapper/ColumnInfo.cs index 77a9532..13cfcc3 100644 --- a/ExcelMapper/ColumnInfo.cs +++ b/ExcelMapper/ColumnInfo.cs @@ -27,11 +27,7 @@ public class ColumnInfo /// Gets or sets the default cell setter. /// protected Action defaultCellSetter; - - /// - /// Gets or sets the custom cell setter. - /// - protected Action customCellSetter; + protected Action customCellSetter; /// /// Sets the property type. @@ -125,7 +121,7 @@ public bool IsSubType /// /// The cell setter. /// - public Action SetCell => customCellSetter ?? defaultCellSetter; + public Action SetCell => customCellSetter != null ? customCellSetter : (entity, cell, value) => defaultCellSetter(cell, value); /// /// Gets or sets the property setter. @@ -379,42 +375,74 @@ public virtual object GetProperty(object o) /// Specifies a method to use when setting the cell value from an object. /// The method to use when setting the cell value from an object. /// The object. + public ColumnInfo SetCellUsing(Action> setCell) where TEntity : class + { + customCellSetter = (entity, cell, value) => setCell(new SetCellArgs(cell, (TEntity)entity, value)); + return this; + } + + /// Specifies a method to use when setting the cell value from an object. + /// The method to use when setting the cell value from an object. + /// The object. + public ColumnInfo SetCellUsing(Action> setCell) where TEntity : class + { + customCellSetter = (entity, cell, value) => setCell(new SetCellArgs(cell, (TEntity)entity, (TValue)value)); + return this; + } + + /// Specifies a method to use when setting the cell value from an object. + /// The method to use when setting the cell value from an object. + /// The object. + [Obsolete("This method is obsolete. Use SetCellUsing(Action>) instead.")] public ColumnInfo SetCellUsing(Action setCell) { - customCellSetter = setCell; + customCellSetter = (entity, cell, value) => setCell(cell, value); return this; } /// Specifies a method to use when setting the cell value from an object. /// The method to use when setting the cell value from an object. /// The object. + [Obsolete("This method is obsolete. Use SetCellUsing(Action>) instead.")] public ColumnInfo SetCellUsing(Action setCell) { - customCellSetter = (c, o) => setCell(c, (T)o); + customCellSetter = (entity, cell, value) => setCell(cell, (T)value); + return this; + } + + /// Specifies a method to use when setting the property value from the cell value. + /// The method to use when setting the property value from the cell value. + /// The object. + public ColumnInfo SetPropertyUsing(Func, object> setProp) where TEntity : class + { + SetProp = (entity, value, cell) => setProp(new SetPropertyArgs(cell, (TEntity)entity, value)); return this; } /// Specifies a method to use when setting the property value from the cell value. /// The method to use when setting the property value from the cell value. /// The object. + [Obsolete("This method is obsolete. Use SetPropertyUsing(Func, object>) instead.")] public ColumnInfo SetPropertyUsing(Func setProp) { - SetProp = (o, v, c) => setProp(v); + SetProp = (entity, value, cell) => setProp(value); return this; } /// Specifies a method to use when setting the property value from the cell value. /// The method to use when setting the property value from the cell value. /// The object. + [Obsolete("This method is obsolete. Use SetPropertyUsing(Func, object>) instead.")] public ColumnInfo SetPropertyUsing(Func setProp) { - SetProp = (o, v, c) => setProp(v, c); + SetProp = (entity, value, cell) => setProp(value, cell); return this; } /// Specifies a method to use when setting the property value from the cell value. /// The method to use when setting the property value from the cell value. /// The object. + [Obsolete("This method is obsolete. Use SetPropertyUsing(Func, object>) instead.")] public ColumnInfo SetPropertyUsing(Func setProp) { SetProp = setProp; @@ -424,18 +452,20 @@ public ColumnInfo SetPropertyUsing(Func setProp) /// Specifies a method to use when setting the property value from the cell value. /// The method to use when setting the property value from the cell value. /// The object. + [Obsolete("This method is obsolete. Use SetPropertyUsing(Func, object>) instead.")] public ColumnInfo SetPropertyUsing(Func setProp) { - SetProp = (o, v, c) => setProp((T)o, v); + SetProp = (entity, value, cell) => setProp((T)entity, value); return this; } /// Specifies a method to use when setting the property value from the cell value. /// The method to use when setting the property value from the cell value. /// The object. + [Obsolete("This method is obsolete. Use SetPropertyUsing(Func, object>) instead.")] public ColumnInfo SetPropertyUsing(Func setProp) { - SetProp = (o, v, c) => setProp((T)o, v, c); + SetProp = (entity, value, cell) => setProp((T)entity, value, cell); return this; } diff --git a/ExcelMapper/ExcelMapper.cs b/ExcelMapper/ExcelMapper.cs index 9aebb07..357cde0 100644 --- a/ExcelMapper/ExcelMapper.cs +++ b/ExcelMapper/ExcelMapper.cs @@ -1136,7 +1136,7 @@ private static void SetCell(Func valueConverter, T ob ci.ChangeSetterType(newType); } ci.SetCellStyle(cell); - ci.SetCell(cell, val); + ci.SetCell(objInstance, cell, val); if (oldType != null) ci.ChangeSetterType(oldType); } diff --git a/ExcelMapper/SetCellArgs.cs b/ExcelMapper/SetCellArgs.cs new file mode 100644 index 0000000..05638a8 --- /dev/null +++ b/ExcelMapper/SetCellArgs.cs @@ -0,0 +1,59 @@ +using System; +using NPOI.SS.UserModel; + +namespace Ganss.Excel +{ + /// + /// set excell cell parameter + /// + /// + /// + public class SetCellArgs : EventArgs where TEntity : class + { + /// + /// excel cell + /// + public ICell Cell { get; } + + /// + /// collection row data + /// + public TEntity Data { get; } + + /// + /// property value + /// + public TValue Value { get; } + + /// + /// set excell cell parameter + /// + /// excel cell + /// collection row data + /// excel cell value + public SetCellArgs(ICell cell, TEntity data, TValue value) + { + Cell = cell; + Data = data; + Value = value; + } + } + + /// + /// set excell cell parameter + /// + /// + public class SetCellArgs : SetCellArgs where TEntity : class + { + + /// + /// set excell cell parameter + /// + /// excel cell + /// collection row data + /// excel cell value + public SetCellArgs(ICell cell, TEntity data, object value) : base(cell, data, value) + { + } + } +} diff --git a/ExcelMapper/SetPropertyArgs.cs b/ExcelMapper/SetPropertyArgs.cs new file mode 100644 index 0000000..3141f33 --- /dev/null +++ b/ExcelMapper/SetPropertyArgs.cs @@ -0,0 +1,40 @@ +using System; +using NPOI.SS.UserModel; + +namespace Ganss.Excel +{ + /// + /// set entity property value parameter + /// + /// + public class SetPropertyArgs : EventArgs where TEntity : class + { + /// + /// excel cell + /// + public ICell Cell { get; } + + /// + /// collection row data + /// + public TEntity Data { get; } + + /// + /// excel cell value + /// + public object Value { get; } + + /// + /// set entity property value parameter + /// + /// excel cell + /// collection row data + /// excel cell value + public SetPropertyArgs(ICell cell, TEntity data, object value) + { + Cell = cell; + Data = data; + Value = value; + } + } +}