Skip to content

Commit

Permalink
Merge pull request #172 from spreadsheetlab/improve-parser-reference
Browse files Browse the repository at this point in the history
Improve ParserReference
  • Loading branch information
joerivv authored Jan 19, 2023
2 parents 8a4ab70 + 9468c07 commit 52b87ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 39 deletions.
3 changes: 3 additions & 0 deletions src/XLParser/ExcelFormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ public static IEnumerable<ParserReference> GetParserReferences(this ParseTreeNod
ParserReference range = rangeStart.First();
range.MaxLocation = rangeEnd.First().MinLocation;
range.ReferenceType = ReferenceType.CellRange;
range.ReferenceNode = node;
range.LocationString = node.Print();
list.Add(range);
}
Expand All @@ -440,6 +441,8 @@ public static IEnumerable<ParserReference> GetParserReferences(this ParseTreeNod
ParserReference range = rangeStart.First();
range.TableColumns = rangeStart.First().TableColumns.Concat(rangeEnd.First().TableColumns).ToArray();
range.TableSpecifiers = rangeStart.First().TableSpecifiers.SequenceEqual(rangeEnd.First().TableSpecifiers) ? range.TableSpecifiers : new string[0];
range.ReferenceNode = node;
range.LocationString = node.Print();
list.Add(range);
}
else
Expand Down
45 changes: 6 additions & 39 deletions src/XLParser/ParserReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,19 @@ public enum ReferenceType

public class ParserReference
{
public const int MaxRangeHeight = 1048576;
public const int MaxRangeWidth = 16384;

public ReferenceType ReferenceType { get; set; }
public ParseTreeNode ReferenceNode { get; set; }
public string LocationString { get; set; }
public string Worksheet { get; set; }
public string LastWorksheet { get; set; }
public string FilePath { get; set; }
public string FileName { get; set; }
public string Name { get; private set; }
public string MinLocation { get; set; } //Location as appearing in the formula, eg $A$1
public string Name { get; set; }
public string MinLocation { get; set; }
public string MaxLocation { get; set; }
public string[] TableSpecifiers { get; set; }
public string[] TableColumns { get; set; }

public ParserReference(ReferenceType referenceType, string locationString = null, string worksheet = null, string lastWorksheet = null,
string filePath = null, string fileName = null, string name = null, string minLocation = null, string maxLocation = null,
string[] tableSpecifiers = null, string[] tableColumns = null)
{
ReferenceType = referenceType;
LocationString = locationString;
Worksheet = worksheet;
LastWorksheet = lastWorksheet;
FilePath = filePath;
FileName = fileName;
Name = name;
MinLocation = minLocation;
MaxLocation = maxLocation != null ? maxLocation : minLocation;
TableColumns = tableColumns;
TableSpecifiers = tableSpecifiers;
}


public ParserReference(ParseTreeNode node)
{
InitializeReference(node);
Expand Down Expand Up @@ -123,6 +104,7 @@ public void InitializeReference(ParseTreeNode node)
break;
}

ReferenceNode = node;
LocationString = node.Print();
}

Expand All @@ -131,24 +113,9 @@ private string UnEscape(string value, string escapeCharacter)
return System.Text.RegularExpressions.Regex.Replace(value, $"{escapeCharacter}(?!{escapeCharacter})", "");
}

/// <summary>
/// Converts the column number to an Excel column string representation.
/// </summary>
/// <param name="columnNumber">The zero-based column number.</param>
private string ConvertColumnToStr(int columnNumber)
{
var sb = new System.Text.StringBuilder();
while (columnNumber >= 0)
{
sb.Insert(0, (char)(65 + columnNumber % 26));
columnNumber = columnNumber / 26 - 1;
}
return sb.ToString();
}

public override string ToString()
{
return ReferenceType == ReferenceType.Cell ? MinLocation.ToString() : string.Format("{0}:{1}", MinLocation, MaxLocation);
return LocationString;
}
}
}

0 comments on commit 52b87ec

Please sign in to comment.