Skip to content

ReferenceArea To A1 logic #20

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

Merged
merged 3 commits into from
Oct 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/ClosedXML.Parser/ClosedXML.Parser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageProjectUrl>https://github.com/ClosedXML/ClosedXML.Parser</PackageProjectUrl>
<RepositoryUrl>https://github.com/ClosedXML/ClosedXML.Parser</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageVersion>1.2.0</PackageVersion>
<PackageVersion>1.3.0</PackageVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
43 changes: 20 additions & 23 deletions src/ClosedXML.Parser/ReferenceArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ public readonly struct ReferenceArea
/// </summary>
public ReferenceStyle Style => First.Style;

/// <summary>
/// Is area a row span (e.g. <c>$5:7</c> in A1 or <c>R[7]</c>, <c>R7:R[9]</c>)?
/// </summary>
internal bool IsRowSpan => First.IsRow && Second.IsRow;

/// <summary>
/// Is area a col span (e.g. <c>$C:Z</c> in A1 or <c>C[7]</c>, <c>C7:C[9]</c>)?
/// </summary>
internal bool IsColSpan => First.IsColumn && Second.IsColumn;

/// <summary>
/// Create a reference symbol using the two <see cref="RowCol"/> (e.g.
/// <c>A1:B2</c>) or two columns (e.g. <c>A:D</c>) or two rows (e.g.
Expand Down Expand Up @@ -85,14 +95,9 @@ internal ReferenceArea(int rowPosition, int columnPosition, ReferenceStyle style
/// </summary>
public string GetDisplayStringA1()
{
if (First == Second && !First.IsColumn && !First.IsRow)
return First.GetDisplayStringA1();

return new StringBuilder()
.Append(First.GetDisplayStringA1())
.Append(':')
.Append(Second.GetDisplayStringA1())
.ToString();
var sb = new StringBuilder();
AppendA1(sb);
return sb.ToString();
}

/// <summary>
Expand All @@ -102,14 +107,7 @@ public string GetDisplayStringA1()
/// </summary>
public string GetDisplayStringR1C1()
{
if (First == Second)
return First.GetDisplayStringR1C1();

return new StringBuilder()
.Append(First.GetDisplayStringR1C1())
.Append(':')
.Append(Second.GetDisplayStringR1C1())
.ToString();
return AppendR1C1(new StringBuilder()).ToString();
}

/// <summary>
Expand Down Expand Up @@ -156,14 +154,13 @@ internal StringBuilder Append(StringBuilder sb)
/// <remarks>Assumes reference is in A1.</remarks>
internal StringBuilder AppendA1(StringBuilder sb)
{
First.AppendA1(sb);

if (First != Second)
{
sb.Append(':');
Second.AppendA1(sb);
}
// In A1, column and row span must have both parts specified.
if (First == Second && !IsColSpan && !IsRowSpan)
return First.AppendA1(sb);

First.AppendA1(sb);
sb.Append(':');
Second.AppendA1(sb);
return sb;
}

Expand Down
4 changes: 3 additions & 1 deletion src/ClosedXML.Parser/RowCol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ internal void Append(StringBuilder sb)
/// <inheritdoc cref="GetDisplayStringA1()"/>
/// <param name="sb">String buffer where to write the output.</param>
/// <exception cref="InvalidOperationException">When <c>RowCol</c> is not in <em>A1</em> notation.</exception>
internal void AppendA1(StringBuilder sb)
internal StringBuilder AppendA1(StringBuilder sb)
{
if (!IsA1)
throw new InvalidOperationException("RowCol doesn't use A1 semantic.");
Expand Down Expand Up @@ -334,6 +334,8 @@ internal void AppendA1(StringBuilder sb)
default:
throw new NotSupportedException();
}

return sb;
}

/// <inheritdoc cref="GetDisplayStringR1C1()"/>
Expand Down
Loading