Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor a parameter for SetCellUsing and SetPropertyUsing #213

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion ExcelMapper.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -1220,7 +1223,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 @@ -1261,6 +1268,41 @@ public void GetterSetterTest()
return dt.ToBinary();
});

excel.AddMapping<GetterSetterProduct>("Number", p => p.Number)
.SetCellUsing<GetterSetterProduct, int>((args) =>
{
args.Cell.SetCellValue(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.That(products[0].RedName, Is.Null);
Expand Down
54 changes: 42 additions & 12 deletions ExcelMapper/ColumnInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ public class ColumnInfo
/// Gets or sets the default cell setter.
/// </summary>
protected Action<ICell, object> defaultCellSetter;

/// <summary>
/// Gets or sets the custom cell setter.
/// </summary>
protected Action<ICell, object> customCellSetter;
protected Action<object, ICell, object> customCellSetter;

/// <summary>
/// Sets the property type.
Expand Down Expand Up @@ -125,7 +121,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 @@ -379,42 +375,74 @@ public virtual object GetProperty(object o)
/// <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<TEntity, TValue>(Action<SetCellArgs<TEntity, TValue>> setCell) where TEntity : class
{
customCellSetter = (entity, cell, value) => setCell(new SetCellArgs<TEntity, TValue>(cell, (TEntity)entity, (TValue)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>
[Obsolete("This method is obsolete. Use SetCellUsing<TEntity>(Action<SetCellArgs<TEntity>>) instead.")]
public ColumnInfo SetCellUsing(Action<ICell, object> setCell)
{
customCellSetter = setCell;
customCellSetter = (entity, cell, value) => setCell(cell, 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>
[Obsolete("This method is obsolete. Use SetCellUsing<TEntity, TValue>(Action<SetCellArgs<TEntity, TValue>>) instead.")]
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;
}

/// <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>
[Obsolete("This method is obsolete. Use SetPropertyUsing<TEntity>(Func<SetPropertyArgs<TEntity>, object>) instead.")]
public ColumnInfo SetPropertyUsing(Func<object, object> setProp)
{
SetProp = (o, v, c) => setProp(v);
SetProp = (entity, value, cell) => setProp(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>
[Obsolete("This method is obsolete. Use SetPropertyUsing<TEntity>(Func<SetPropertyArgs<TEntity>, object>) instead.")]
public ColumnInfo SetPropertyUsing(Func<object, ICell, object> setProp)
{
SetProp = (o, v, c) => setProp(v, c);
SetProp = (entity, value, cell) => setProp(value, cell);
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>
[Obsolete("This method is obsolete. Use SetPropertyUsing<TEntity>(Func<SetPropertyArgs<TEntity>, object>) instead.")]
public ColumnInfo SetPropertyUsing(Func<object, object, ICell, object> setProp)
{
SetProp = setProp;
Expand All @@ -424,18 +452,20 @@ public ColumnInfo SetPropertyUsing(Func<object, object, ICell, object> setProp)
/// <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>
[Obsolete("This method is obsolete. Use SetPropertyUsing<TEntity>(Func<SetPropertyArgs<TEntity>, object>) instead.")]
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;
}

/// <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>
[Obsolete("This method is obsolete. Use SetPropertyUsing<TEntity>(Func<SetPropertyArgs<TEntity>, object>) instead.")]
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 @@ -1136,7 +1136,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
59 changes: 59 additions & 0 deletions ExcelMapper/SetCellArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using NPOI.SS.UserModel;

namespace Ganss.Excel
{
/// <summary>
/// set excell cell parameter
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class SetCellArgs<TEntity, TValue> : 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 TValue 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, TValue value)
{
Cell = cell;
Data = data;
Value = value;
}
}

/// <summary>
/// set excell cell parameter
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public class SetCellArgs<TEntity> : SetCellArgs<TEntity, object> where TEntity : class
{

/// <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) : base(cell, data, value)
{
}
}
}
40 changes: 40 additions & 0 deletions ExcelMapper/SetPropertyArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
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;
}
}
}