Move Excel extensions to own project

This commit is contained in:
Sergey Chebotar 2023-04-01 15:58:42 +03:00
parent da29243d1d
commit 448af8ecd7
16 changed files with 102 additions and 76 deletions

View File

@ -36,4 +36,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="RhSolutions.Sku" Version="0.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RhSolutions.ExcelExtensions\RhSolutions.ExcelExtensions.csproj" />
</ItemGroup>
</Project>

View File

@ -1,8 +1,8 @@
using System.Collections;
namespace RhSolutions.ExcelTable;
namespace RhSolutions.ExcelExtensions;
public sealed class ExcelColumn : ExcelTable, IEnumerable<ExcelTableCell>
public sealed class Column : Table, IEnumerable<TableCell>
{
public string Header
{
@ -17,26 +17,26 @@ public sealed class ExcelColumn : ExcelTable, IEnumerable<ExcelTableCell>
get => Range.Rows.Count;
}
public ExcelColumn(Range range, ExcelTable table) : base(range, table)
public Column(Range range, Table table) : base(range, table)
{
Range = range;
ParentTable = table;
}
public ExcelTableCell this[int index]
public TableCell this[int index]
{
get => new(Range.Cells[index + 1, 1], ParentTable);
}
public IEnumerator<ExcelTableCell> GetEnumerator()
public IEnumerator<TableCell> GetEnumerator()
{
return new ExcelColumnEnumerator(Range, ParentTable);
return new ColumnEnumerator(Range, ParentTable);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public ExcelColumn AddLeft()
public Column AddLeft()
{
Range.EntireColumn
.Insert(XlInsertShiftDirection.xlShiftToRight,

View File

@ -1,11 +1,11 @@
using System.Collections;
namespace RhSolutions.ExcelTable;
namespace RhSolutions.ExcelExtensions;
public class ExcelColumnEnumerator : IEnumerator<ExcelTableCell>
public class ColumnEnumerator : IEnumerator<TableCell>
{
public Range Range { get; }
public ExcelTable ParentTable { get; }
public Table ParentTable { get; }
private int position = 0;
object IEnumerator.Current
{
@ -15,13 +15,13 @@ public class ExcelColumnEnumerator : IEnumerator<ExcelTableCell>
}
}
public ExcelTableCell Current
public TableCell Current
{
get
{
try
{
return new ExcelTableCell(Range.Cells[position, 1], ParentTable);
return new TableCell(Range.Cells[position, 1], ParentTable);
}
catch (IndexOutOfRangeException)
{
@ -30,7 +30,7 @@ public class ExcelColumnEnumerator : IEnumerator<ExcelTableCell>
}
}
public ExcelColumnEnumerator(Range range, ExcelTable table)
public ColumnEnumerator(Range range, Table table)
{
Range = range;
ParentTable = table;

View File

@ -1,23 +1,23 @@
using System.Collections;
namespace RhSolutions.ExcelTable;
namespace RhSolutions.ExcelExtensions;
public class ExcelColumns : IEnumerable<ExcelColumn>
public class Columns : IEnumerable<Column>
{
public Range Range { get; }
public ExcelTable ParentTable { get; }
public Table ParentTable { get; }
public int Length
{
get => Range.Columns.Count;
}
public ExcelColumns(Range range, ExcelTable parentTable)
public Columns(Range range, Table parentTable)
{
Range = range;
ParentTable = parentTable;
}
public ExcelColumn this[int index]
public Column this[int index]
{
get
{
@ -26,13 +26,13 @@ public class ExcelColumns : IEnumerable<ExcelColumn>
throw new IndexOutOfRangeException();
}
return new ExcelColumn(Range.Columns[index + 1], ParentTable);
return new Column(Range.Columns[index + 1], ParentTable);
}
}
public IEnumerator<ExcelColumn> GetEnumerator()
public IEnumerator<Column> GetEnumerator()
{
return new ExcelColumnsEnumerator(Range, ParentTable);
return new ColumnsEnumerator(Range, ParentTable);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

View File

@ -1,11 +1,11 @@
using System.Collections;
namespace RhSolutions.ExcelTable;
namespace RhSolutions.ExcelExtensions;
public class ExcelColumnsEnumerator: IEnumerator<ExcelColumn>
public class ColumnsEnumerator: IEnumerator<Column>
{
public Range Range { get; }
public ExcelTable ParentTable { get; }
public Table ParentTable { get; }
private int position = 0;
object IEnumerator.Current
{
@ -15,13 +15,13 @@ public class ExcelColumnsEnumerator: IEnumerator<ExcelColumn>
}
}
public ExcelColumn Current
public Column Current
{
get
{
try
{
return new ExcelColumn(Range.Columns[position], ParentTable);
return new Column(Range.Columns[position], ParentTable);
}
catch (IndexOutOfRangeException)
{
@ -30,7 +30,7 @@ public class ExcelColumnsEnumerator: IEnumerator<ExcelColumn>
}
}
public ExcelColumnsEnumerator(Range range, ExcelTable table)
public ColumnsEnumerator(Range range, Table table)
{
Range = range;
ParentTable = table;

View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net472;net6.0-windows7.0</TargetFrameworks>
<LangVersion>10</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ExcelDna.Interop" Version="15.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
</Project>

View File

@ -1,8 +1,8 @@
using System.Collections;
namespace RhSolutions.ExcelTable;
namespace RhSolutions.ExcelExtensions;
public sealed class ExcelRow : ExcelTable, IEnumerable<ExcelTableCell>
public sealed class Row : Table, IEnumerable<TableCell>
{
public int Index
{
@ -13,20 +13,20 @@ public sealed class ExcelRow : ExcelTable, IEnumerable<ExcelTableCell>
get => Range.Columns.Count;
}
public ExcelRow(Range range, ExcelTable table) : base(range, table)
public Row(Range range, Table table) : base(range, table)
{
Range = range;
ParentTable = table;
}
public ExcelTableCell this[int index]
public TableCell this[int index]
{
get => new(Range.Cells[1, index + 1], ParentTable);
}
public IEnumerator<ExcelTableCell> GetEnumerator()
public IEnumerator<TableCell> GetEnumerator()
{
return new ExcelRowEnumerator(Range, ParentTable);
return new RowEnumerator(Range, ParentTable);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

View File

@ -1,11 +1,11 @@
using System.Collections;
namespace RhSolutions.ExcelTable;
namespace RhSolutions.ExcelExtensions;
public class ExcelRowEnumerator : IEnumerator<ExcelTableCell>
public class RowEnumerator : IEnumerator<TableCell>
{
public Range Range { get; }
public ExcelTable ParentTable { get; }
public Table ParentTable { get; }
private int position = 0;
object IEnumerator.Current
{
@ -15,13 +15,13 @@ public class ExcelRowEnumerator : IEnumerator<ExcelTableCell>
}
}
public ExcelTableCell Current
public TableCell Current
{
get
{
try
{
return new ExcelTableCell(Range.Cells[1, position], ParentTable);
return new TableCell(Range.Cells[1, position], ParentTable);
}
catch (IndexOutOfRangeException)
{
@ -30,7 +30,7 @@ public class ExcelRowEnumerator : IEnumerator<ExcelTableCell>
}
}
public ExcelRowEnumerator(Range range, ExcelTable parentTable)
public RowEnumerator(Range range, Table parentTable)
{
Range = range;
ParentTable = parentTable;

View File

@ -1,23 +1,23 @@
using System.Collections;
namespace RhSolutions.ExcelTable;
namespace RhSolutions.ExcelExtensions;
public class ExcelRows : IEnumerable<ExcelRow>
public class Rows : IEnumerable<Row>
{
public Range Range { get; }
public ExcelTable ParentTable { get; }
public Table ParentTable { get; }
public int Length
{
get => Range.Rows.Count;
}
public ExcelRows(Range range, ExcelTable parentTable)
public Rows(Range range, Table parentTable)
{
Range = range;
ParentTable = parentTable;
}
public ExcelRow this[int index]
public Row this[int index]
{
get
{
@ -26,13 +26,13 @@ public class ExcelRows : IEnumerable<ExcelRow>
throw new IndexOutOfRangeException();
}
return new ExcelRow(Range.Rows[index + 1], ParentTable);
return new Row(Range.Rows[index + 1], ParentTable);
}
}
public IEnumerator<ExcelRow> GetEnumerator()
public IEnumerator<Row> GetEnumerator()
{
return new ExcelRowsEnumerator(Range, ParentTable);
return new RowsEnumerator(Range, ParentTable);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

View File

@ -1,11 +1,11 @@
using System.Collections;
namespace RhSolutions.ExcelTable;
namespace RhSolutions.ExcelExtensions;
public class ExcelRowsEnumerator : IEnumerator<ExcelRow>
public class RowsEnumerator : IEnumerator<Row>
{
public Range Range { get; }
public ExcelTable ParentTable { get; }
public Table ParentTable { get; }
private int position = 0;
object IEnumerator.Current
{
@ -15,13 +15,13 @@ public class ExcelRowsEnumerator : IEnumerator<ExcelRow>
}
}
public ExcelRow Current
public Row Current
{
get
{
try
{
return new ExcelRow(Range.Rows[position], ParentTable);
return new Row(Range.Rows[position], ParentTable);
}
catch (IndexOutOfRangeException)
{
@ -30,7 +30,7 @@ public class ExcelRowsEnumerator : IEnumerator<ExcelRow>
}
}
public ExcelRowsEnumerator(Range range, ExcelTable table)
public RowsEnumerator(Range range, Table table)
{
Range = range;
ParentTable = table;

View File

@ -1,34 +1,34 @@
namespace RhSolutions.ExcelTable;
namespace RhSolutions.ExcelExtensions;
public class ExcelTable
public class Table
{
public Range Range { get; protected set; }
public ExcelTable ParentTable { get; protected set; }
public ExcelRows Rows { get; }
public ExcelColumns Columns { get; }
public Table ParentTable { get; protected set; }
public Rows Rows { get; }
public Columns Columns { get; }
public ExcelTable(Range range)
public Table(Range range)
{
Range = range;
ParentTable = null;
Rows = new ExcelRows(Range, this);
Columns = new ExcelColumns(Range, this);
Rows = new Rows(Range, this);
Columns = new Columns(Range, this);
}
public ExcelTable(Range range, ExcelTable table)
public Table(Range range, Table table)
{
Range = range;
ParentTable = table;
Rows = new ExcelRows(Range, this);
Columns = new ExcelColumns(Range, this);
Rows = new Rows(Range, this);
Columns = new Columns(Range, this);
}
public ExcelTableCell this[int row, int column]
public TableCell this[int row, int column]
{
get => new(Range.Cells[row + 1, column + 1], this);
}
public IEnumerable<ExcelTableCell> Find(object item)
public IEnumerable<TableCell> Find(object item)
{
Range firstFound = Range.Find(item);
if (firstFound == null)
@ -40,7 +40,7 @@ public class ExcelTable
while (true)
{
yield return new ExcelTableCell(nextFound, ParentTable ?? this);
yield return new TableCell(nextFound, ParentTable ?? this);
nextFound = Range.FindNext(nextFound);
if (nextFound.Row == firstFound.Row

View File

@ -1,12 +1,12 @@
namespace RhSolutions.ExcelTable;
namespace RhSolutions.ExcelExtensions;
public sealed class ExcelTableCell : ExcelTable
public sealed class TableCell : Table
{
public ExcelRow ParentRow
public Row ParentRow
{
get => ParentTable.Rows[ParentTable.Range.Row - Range.Row];
}
public ExcelColumn ParentColumn
public Column ParentColumn
{
get => ParentTable.Columns[ParentTable.Range.Column - Range.Column];
}
@ -16,7 +16,7 @@ public sealed class ExcelTableCell : ExcelTable
set => Range.Cells[1, 1].Value2 = value;
}
public ExcelTableCell(Range range, ExcelTable table) : base(range, table)
public TableCell(Range range, Table table) : base(range, table)
{
Range = range;
ParentTable = table;

View File

@ -0,0 +1,3 @@
global using Microsoft.Office.Interop.Excel;
global using System.Collections.Generic;
global using Range = Microsoft.Office.Interop.Excel.Range;

View File

@ -3,7 +3,7 @@
[ExcelTestSettings(OutOfProcess = true, Workbook = @"TestWorkbooks\ExcelTableTest.xlsx")]
public class ExcelTablesTests : IDisposable
{
ExcelTable.ExcelTable table;
ExcelExtensions.Table table;
public ExcelTablesTests()
{

View File

@ -1,5 +1,4 @@
global using Xunit;
global using ExcelDna.Testing;
global using Microsoft.Office.Interop.Excel;
global using ExcelDna.Testing;
global using RhSolutions.ExcelTable;
global using RhSolutions.Services;
global using Xunit;

View File

@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RhSolutions.AddIn", "RhSolu
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RhSolutions.Tests", "RhSolutions.Tests\RhSolutions.Tests.csproj", "{6EECCDDB-741C-404A-874F-BB8656265162}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RhSolutions.ExcelExtensions", "RhSolutions.ExcelExtensions\RhSolutions.ExcelExtensions.csproj", "{ADB862A8-5CC6-4509-A4F7-9907E84F5801}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -21,6 +23,10 @@ Global
{6EECCDDB-741C-404A-874F-BB8656265162}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6EECCDDB-741C-404A-874F-BB8656265162}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6EECCDDB-741C-404A-874F-BB8656265162}.Release|Any CPU.Build.0 = Release|Any CPU
{ADB862A8-5CC6-4509-A4F7-9907E84F5801}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ADB862A8-5CC6-4509-A4F7-9907E84F5801}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ADB862A8-5CC6-4509-A4F7-9907E84F5801}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ADB862A8-5CC6-4509-A4F7-9907E84F5801}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE