Skip to content

Commit

Permalink
refactor a parameter for SetCellUsing and SetPropertyUsing
Browse files Browse the repository at this point in the history
  • Loading branch information
andywu188 committed Aug 26, 2022
1 parent 006618e commit b04a360
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 10 deletions.
44 changes: 43 additions & 1 deletion ExcelMapper.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -1166,7 +1169,11 @@ public void GetterSetterTest()
{
var excel = new ExcelMapper(@"../../../xlsx/ProductsConvert.xlsx") { TrackObjects = true };

excel.AddMapping<GetterSetterProduct>("Name", p => p.Name);
excel.AddMapping<GetterSetterProduct>("Name", p => p.Name)
.SetPropertyUsing<GetterSetterProduct>((entity, value, cell) =>
{
return value;
});
excel.AddMapping<GetterSetterProduct>("Name", p => p.RedName)
.FromExcelOnly()
.SetPropertyUsing((v, c) =>
Expand Down Expand Up @@ -1207,6 +1214,41 @@ public void GetterSetterTest()
return dt.ToBinary();
});

excel.AddMapping<GetterSetterProduct>("Number", p => p.Number)
.SetCellUsing<GetterSetterProduct>((args) =>
{
args.Cell.SetCellValue((int)args.Value);
})
.SetPropertyUsing<GetterSetterProduct>((args) =>
{
return Convert.ToInt32(args.Value);
});

excel.AddMapping<GetterSetterProduct>("Price", p => p.Price)
.SetCellUsing<double>((cell, value) =>
{
cell.SetCellValue(value);
})
.SetPropertyUsing<GetterSetterProduct>((entity, value) =>
{
return Convert.ToDouble(value);
});

excel.AddMapping<GetterSetterProduct>("Value", p => p.Value)
.SetCellUsing<GetterSetterProduct>((args) =>
{
args.Cell.SetCellValue(args.Data.Number * args.Data.Price);
})
.SetPropertyUsing<GetterSetterProduct>((args) =>
{
var value = 0.0;
if(args.Cell.CellType == CellType.Formula)
{
value = args.Data.Number * args.Data.Price;
}
return value;
});

var products = excel.Fetch<GetterSetterProduct>().ToList();

Assert.Null(products[0].RedName);
Expand Down
34 changes: 26 additions & 8 deletions ExcelMapper/ColumnInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ColumnInfo
private PropertyInfo property;
private bool isSubType;
protected Action<ICell, object> defaultCellSetter;
protected Action<ICell, object> customCellSetter;
protected Action<object, ICell, object> customCellSetter;

/// <summary>
/// Sets the property type.
Expand Down Expand Up @@ -117,7 +117,7 @@ public bool IsSubType
/// <value>
/// The cell setter.
/// </value>
public Action<ICell, object> SetCell => customCellSetter ?? defaultCellSetter;
public Action<object, ICell, object> SetCell => customCellSetter != null ? customCellSetter : (entity, cell, value) => defaultCellSetter(cell, value);

/// <summary>
/// Gets or sets the property setter.
Expand Down Expand Up @@ -332,12 +332,21 @@ public virtual object GetProperty(object o)
return Property.GetValue(o, null);
}

/// <summary>Specifies a method to use when setting the cell value from an object.</summary>
/// <param name="setCell">The method to use when setting the cell value from an object.</param>
/// <returns>The <see cref="ColumnInfo"/> object.</returns>
public ColumnInfo SetCellUsing<TEntity>(Action<SetCellArgs<TEntity>> setCell) where TEntity : class
{
customCellSetter = (entity, cell, value) => setCell(new SetCellArgs<TEntity>(cell, (TEntity)entity, value));
return this;
}

/// <summary>Specifies a method to use when setting the cell value from an object.</summary>
/// <param name="setCell">The method to use when setting the cell value from an object.</param>
/// <returns>The <see cref="ColumnInfo"/> object.</returns>
public ColumnInfo SetCellUsing(Action<ICell, object> setCell)
{
customCellSetter = setCell;
customCellSetter = (entity, cell, value) => setCell(cell, value);
return this;
}

Expand All @@ -346,7 +355,16 @@ public ColumnInfo SetCellUsing(Action<ICell, object> setCell)
/// <returns>The <see cref="ColumnInfo"/> object.</returns>
public ColumnInfo SetCellUsing<T>(Action<ICell, T> setCell)
{
customCellSetter = (c, o) => setCell(c, (T)o);
customCellSetter = (entity, cell, value) => setCell(cell, (T)value);
return this;
}

/// <summary>Specifies a method to use when setting the property value from the cell value.</summary>
/// <param name="setProp">The method to use when setting the property value from the cell value.</param>
/// <returns>The <see cref="ColumnInfo"/> object.</returns>
public ColumnInfo SetPropertyUsing<TEntity>(Func<SetPropertyArgs<TEntity>, object> setProp) where TEntity : class
{
SetProp = (entity, value, cell) => setProp(new SetPropertyArgs<TEntity>(cell, (TEntity)entity, value));
return this;
}

Expand All @@ -355,7 +373,7 @@ public ColumnInfo SetCellUsing<T>(Action<ICell, T> setCell)
/// <returns>The <see cref="ColumnInfo"/> object.</returns>
public ColumnInfo SetPropertyUsing(Func<object, object> setProp)
{
SetProp = (o, v, c) => setProp(v);
SetProp = (entity, value, cell) => setProp(value);
return this;
}

Expand All @@ -364,7 +382,7 @@ public ColumnInfo SetPropertyUsing(Func<object, object> setProp)
/// <returns>The <see cref="ColumnInfo"/> object.</returns>
public ColumnInfo SetPropertyUsing(Func<object, ICell, object> setProp)
{
SetProp = (o, v, c) => setProp(v, c);
SetProp = (entity, value, cell) => setProp(value, cell);
return this;
}

Expand All @@ -382,7 +400,7 @@ public ColumnInfo SetPropertyUsing(Func<object, object, ICell, object> setProp)
/// <returns>The <see cref="ColumnInfo"/> object.</returns>
public ColumnInfo SetPropertyUsing<T>(Func<T, object, object> setProp)
{
SetProp = (o, v, c) => setProp((T)o, v);
SetProp = (entity, value, cell) => setProp((T)entity, value);
return this;
}

Expand All @@ -391,7 +409,7 @@ public ColumnInfo SetPropertyUsing<T>(Func<T, object, object> setProp)
/// <returns>The <see cref="ColumnInfo"/> object.</returns>
public ColumnInfo SetPropertyUsing<T>(Func<T, object, ICell, object> setProp)
{
SetProp = (o, v, c) => setProp((T)o, v, c);
SetProp = (entity, value, cell) => setProp((T)entity, value, cell);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion ExcelMapper/ExcelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ private static void SetCell<T>(Func<string, object, object> valueConverter, T ob
ci.ChangeSetterType(newType);
}
ci.SetCellStyle(cell);
ci.SetCell(cell, val);
ci.SetCell(objInstance, cell, val);
if (oldType != null)
ci.ChangeSetterType(oldType);
}
Expand Down
41 changes: 41 additions & 0 deletions ExcelMapper/SetCellArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using NPOI.SS.UserModel;

namespace Ganss.Excel
{
/// <summary>
/// set excell cell parameter
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public class SetCellArgs<TEntity> : EventArgs where TEntity : class
{
/// <summary>
/// excel cell
/// </summary>
public ICell Cell { get; }

/// <summary>
/// collection row data
/// </summary>
public TEntity Data { get; }

/// <summary>
/// property value
/// </summary>
public object Value { get; }

/// <summary>
/// set excell cell parameter
/// </summary>
/// <param name="cell">excel cell</param>
/// <param name="data">collection row data</param>
/// <param name="value">excel cell value</param>
public SetCellArgs(ICell cell, TEntity data, object value)
{
Cell = cell;
Data = data;
Value = value;
}
}
}
42 changes: 42 additions & 0 deletions ExcelMapper/SetPropertyArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using NPOI.SS.UserModel;

namespace Ganss.Excel
{

/// <summary>
/// set entity property value parameter
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public class SetPropertyArgs<TEntity> : EventArgs where TEntity : class
{
/// <summary>
/// excel cell
/// </summary>
public ICell Cell { get; }

/// <summary>
/// collection row data
/// </summary>
public TEntity Data { get; }

/// <summary>
/// excel cell value
/// </summary>
public object Value { get; }

/// <summary>
/// set entity property value parameter
/// </summary>
/// <param name="cell">excel cell</param>
/// <param name="data">collection row data</param>
/// <param name="value">excel cell value</param>
public SetPropertyArgs(ICell cell, TEntity data, object value)
{
Cell = cell;
Data = data;
Value = value;
}
}
}

0 comments on commit b04a360

Please sign in to comment.