From b04a3606cb9482a5511e0e6ef44489e473ae3ad5 Mon Sep 17 00:00:00 2001 From: andywu188 Date: Fri, 26 Aug 2022 15:17:07 +0800 Subject: [PATCH 1/2] refactor a parameter for SetCellUsing and SetPropertyUsing --- ExcelMapper.Tests/Tests.cs | 44 +++++++++++++++++++++++++++++++++- ExcelMapper/ColumnInfo.cs | 34 +++++++++++++++++++------- ExcelMapper/ExcelMapper.cs | 2 +- ExcelMapper/SetCellArgs.cs | 41 +++++++++++++++++++++++++++++++ ExcelMapper/SetPropertyArgs.cs | 42 ++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 10 deletions(-) create mode 100644 ExcelMapper/SetCellArgs.cs create mode 100644 ExcelMapper/SetPropertyArgs.cs diff --git a/ExcelMapper.Tests/Tests.cs b/ExcelMapper.Tests/Tests.cs index d060daf..5ca9eb0 100644 --- a/ExcelMapper.Tests/Tests.cs +++ b/ExcelMapper.Tests/Tests.cs @@ -1145,9 +1145,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) { @@ -1166,7 +1169,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) => @@ -1207,6 +1214,41 @@ public void GetterSetterTest() return dt.ToBinary(); }); + excel.AddMapping("Number", p => p.Number) + .SetCellUsing((args) => + { + args.Cell.SetCellValue((int)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.Null(products[0].RedName); diff --git a/ExcelMapper/ColumnInfo.cs b/ExcelMapper/ColumnInfo.cs index 29d3ccc..4fd485c 100644 --- a/ExcelMapper/ColumnInfo.cs +++ b/ExcelMapper/ColumnInfo.cs @@ -23,7 +23,7 @@ public class ColumnInfo private PropertyInfo property; private bool isSubType; protected Action defaultCellSetter; - protected Action customCellSetter; + protected Action customCellSetter; /// /// Sets the property type. @@ -117,7 +117,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. @@ -332,12 +332,21 @@ public virtual object GetProperty(object o) return Property.GetValue(o, null); } + /// 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) { - customCellSetter = setCell; + customCellSetter = (entity, cell, value) => setCell(cell, value); return this; } @@ -346,7 +355,16 @@ public ColumnInfo SetCellUsing(Action setCell) /// The object. 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; } @@ -355,7 +373,7 @@ public ColumnInfo SetCellUsing(Action setCell) /// The object. public ColumnInfo SetPropertyUsing(Func setProp) { - SetProp = (o, v, c) => setProp(v); + SetProp = (entity, value, cell) => setProp(value); return this; } @@ -364,7 +382,7 @@ public ColumnInfo SetPropertyUsing(Func setProp) /// The object. public ColumnInfo SetPropertyUsing(Func setProp) { - SetProp = (o, v, c) => setProp(v, c); + SetProp = (entity, value, cell) => setProp(value, cell); return this; } @@ -382,7 +400,7 @@ public ColumnInfo SetPropertyUsing(Func setProp) /// The object. public ColumnInfo SetPropertyUsing(Func setProp) { - SetProp = (o, v, c) => setProp((T)o, v); + SetProp = (entity, value, cell) => setProp((T)entity, value); return this; } @@ -391,7 +409,7 @@ public ColumnInfo SetPropertyUsing(Func setProp) /// The object. 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 6c4d90a..b67dd25 100644 --- a/ExcelMapper/ExcelMapper.cs +++ b/ExcelMapper/ExcelMapper.cs @@ -1126,7 +1126,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..78394cd --- /dev/null +++ b/ExcelMapper/SetCellArgs.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +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 object Value { get; } + + /// + /// set excell cell parameter + /// + /// excel cell + /// collection row data + /// excel cell value + public SetCellArgs(ICell cell, TEntity data, object value) + { + Cell = cell; + Data = data; + Value = value; + } + } +} diff --git a/ExcelMapper/SetPropertyArgs.cs b/ExcelMapper/SetPropertyArgs.cs new file mode 100644 index 0000000..185172e --- /dev/null +++ b/ExcelMapper/SetPropertyArgs.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +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; + } + } +} From a7b5b5758dcfd94bc6bbeeb9f919a3596fee44f2 Mon Sep 17 00:00:00 2001 From: andywu188 Date: Fri, 26 Aug 2022 15:17:07 +0800 Subject: [PATCH 2/2] refactor a parameter for SetCellUsing and SetPropertyUsing --- ExcelMapper.Tests/Tests.cs | 44 ++++++++++++++++++++++++- ExcelMapper/ColumnInfo.cs | 50 +++++++++++++++++++++++----- ExcelMapper/ExcelMapper.cs | 2 +- ExcelMapper/SetCellArgs.cs | 59 ++++++++++++++++++++++++++++++++++ ExcelMapper/SetPropertyArgs.cs | 40 +++++++++++++++++++++++ 5 files changed, 185 insertions(+), 10 deletions(-) create mode 100644 ExcelMapper/SetCellArgs.cs create mode 100644 ExcelMapper/SetPropertyArgs.cs diff --git a/ExcelMapper.Tests/Tests.cs b/ExcelMapper.Tests/Tests.cs index d060daf..b926818 100644 --- a/ExcelMapper.Tests/Tests.cs +++ b/ExcelMapper.Tests/Tests.cs @@ -1145,9 +1145,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) { @@ -1166,7 +1169,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) => @@ -1207,6 +1214,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.Null(products[0].RedName); diff --git a/ExcelMapper/ColumnInfo.cs b/ExcelMapper/ColumnInfo.cs index 29d3ccc..39ecdf3 100644 --- a/ExcelMapper/ColumnInfo.cs +++ b/ExcelMapper/ColumnInfo.cs @@ -23,7 +23,7 @@ public class ColumnInfo private PropertyInfo property; private bool isSubType; protected Action defaultCellSetter; - protected Action customCellSetter; + protected Action customCellSetter; /// /// Sets the property type. @@ -117,7 +117,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. @@ -335,42 +335,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; @@ -380,18 +412,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 6c4d90a..b67dd25 100644 --- a/ExcelMapper/ExcelMapper.cs +++ b/ExcelMapper/ExcelMapper.cs @@ -1126,7 +1126,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; + } + } +}