【优化】调整excel多行记录的判定方式。对于除了多行字段以外的字段,新行全空或者与第一行相应单元格值完全相同,则判定该行属于当前记录

main
walon 2021-08-13 15:35:45 +08:00
parent de250768bc
commit ec3dfe53ae
1 changed files with 42 additions and 3 deletions

View File

@ -129,12 +129,16 @@ namespace Luban.Job.Cfg.DataSources.Excel
{ {
continue; continue;
} }
// 如果非多行数据全空,说明该行属于多行数据 // 如果非多行数据全空,或者跟记录第一行完全相同说明该行属于多行数据
if (notMultiRowFields.All(f => if (notMultiRowFields.All(f =>
{ {
var fieldTitle = title.SubTitles[f.Name]; var fieldTitle = title.SubTitles[f.Name];
return Sheet.IsBlankRow(row, fieldTitle.FromIndex, fieldTitle.ToIndex); return Sheet.IsBlankRow(row, fieldTitle.FromIndex, fieldTitle.ToIndex);
})) }) || (recordRows != null && notMultiRowFields.All(f =>
{
var fieldTitle = title.SubTitles[f.Name];
return Sheet.IsSameRow(row, recordRows[0], fieldTitle.FromIndex, fieldTitle.ToIndex);
})))
{ {
if (recordRows == null) if (recordRows == null)
{ {
@ -202,7 +206,7 @@ namespace Luban.Job.Cfg.DataSources.Excel
{ {
if (Titles.TryGetValue(name, out var title)) if (Titles.TryGetValue(name, out var title))
{ {
CheckEmptySinceSecondRow(name, title.FromIndex, title.ToIndex); // CheckEmptySinceSecondRow(name, title.FromIndex, title.ToIndex);
var es = new ExcelStream(Rows[0], title.FromIndex, title.ToIndex, sep, namedMode); var es = new ExcelStream(Rows[0], title.FromIndex, title.ToIndex, sep, namedMode);
return es; return es;
} }
@ -607,6 +611,41 @@ namespace Luban.Job.Cfg.DataSources.Excel
return true; return true;
} }
private static bool IsSameRow(List<Cell> row1, List<Cell> row2, int fromIndex, int toIndex)
{
if (row2.Count < toIndex - 1)
{
return false;
}
for (int i = Math.Max(1, fromIndex), n = Math.Min(toIndex, row1.Count - 1); i <= n; i++)
{
var v1 = row1[i].Value;
var v2 = row2[i].Value;
if (v1 != v2)
{
if (v1 == null)
{
if (!(v2 is string s && string.IsNullOrWhiteSpace(s)))
{
return false;
}
}
else if (v2 == null)
{
if (!(v1 is string s && string.IsNullOrWhiteSpace(s)))
{
return false;
}
}
else
{
return v1.ToString() == v2.ToString();
}
}
}
return true;
}
public IEnumerable<Record> ReadMulti(TBean type) public IEnumerable<Record> ReadMulti(TBean type)
{ {
foreach (var recordNamedRow in NamedRow.CreateMultiRowNamedRow(this._rowColumns, this._rootTitle, type)) foreach (var recordNamedRow in NamedRow.CreateMultiRowNamedRow(this._rowColumns, this._rootTitle, type))