Skip to content

Commit

Permalink
Merge pull request #105 from WillemJann/improve-issue-101
Browse files Browse the repository at this point in the history
Improve regex for detecting external file names
  • Loading branch information
roelvandersteen authored Jul 8, 2020
2 parents fac389a + cf95d2d commit 4c706d3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/XLParser.Tests/FormulaAnalysisTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,68 @@ public void ExternalWorkbookDefinedNameGlobalScope()
Assert.AreEqual("Items", result.First().Name);
}

[TestMethod]
public void MultipleExternalWorkbookSingleCell()
{
List<ParserReference> result = new FormulaAnalyzer(@"=SUM('C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!$A$1,'C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!$A$2)").ParserReferences().ToList();
Assert.AreEqual(2, result.Count);

Assert.AreEqual(ReferenceType.Cell, result[0].ReferenceType);
Assert.AreEqual("Data.xlsx", result[0].FileName);
Assert.AreEqual("Sheet1", result[0].Worksheet);

Assert.AreEqual(ReferenceType.Cell, result[1].ReferenceType);
Assert.AreEqual("Data.xlsx", result[1].FileName);
Assert.AreEqual("Sheet1", result[1].Worksheet);
}

[TestMethod]
public void MultipleExternalWorkbookCellRange()
{
List<ParserReference> result = new FormulaAnalyzer(@"=SUM('C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!$A$1:$A$10,'C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!$A$11:$A$20)").ParserReferences().ToList();
Assert.AreEqual(2, result.Count);

Assert.AreEqual(ReferenceType.CellRange, result[0].ReferenceType);
Assert.AreEqual("Data.xlsx", result[0].FileName);
Assert.AreEqual("Sheet1", result[0].Worksheet);

Assert.AreEqual(ReferenceType.CellRange, result[1].ReferenceType);
Assert.AreEqual("Data.xlsx", result[1].FileName);
Assert.AreEqual("Sheet1", result[1].Worksheet);
}

[TestMethod]
public void MultipleExternalWorkbookDefinedNameLocalScope()
{
List<ParserReference> result = new FormulaAnalyzer(@"=SUM('C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!FirstItem,'C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!SecondItem)").ParserReferences().ToList();
Assert.AreEqual(2, result.Count);

Assert.AreEqual(ReferenceType.UserDefinedName, result[0].ReferenceType);
Assert.AreEqual("Data.xlsx", result[0].FileName);
Assert.AreEqual("Sheet1", result[0].Worksheet);
Assert.AreEqual("FirstItem", result[0].Name);

Assert.AreEqual(ReferenceType.UserDefinedName, result[1].ReferenceType);
Assert.AreEqual("Data.xlsx", result[1].FileName);
Assert.AreEqual("Sheet1", result[1].Worksheet);
Assert.AreEqual("SecondItem", result[1].Name);
}

[TestMethod]
public void MultipleExternalWorkbookDefinedNameGlobalScope()
{
List<ParserReference> result = new FormulaAnalyzer(@"=SUM('C:\Users\Willem-Jan\Desktop\Data.xlsx'!Items,'C:\Users\Willem-Jan\Desktop\Data.xlsx'!Items2)").ParserReferences().ToList();
Assert.AreEqual(2, result.Count);

Assert.AreEqual(ReferenceType.UserDefinedName, result[0].ReferenceType);
Assert.AreEqual("Data.xlsx", result[0].FileName);
Assert.AreEqual("Items", result[0].Name);

Assert.AreEqual(ReferenceType.UserDefinedName, result[1].ReferenceType);
Assert.AreEqual("Data.xlsx", result[1].FileName);
Assert.AreEqual("Items2", result[1].Name);
}

[TestMethod]
public void DirectSheetReference()
{
Expand Down
24 changes: 24 additions & 0 deletions src/XLParser.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,30 @@ public void ExternalWorkbookDefinedNameGlobalScope()
Test(@"'C:\Users\Willem-Jan\Desktop\Data.xlsx'!Items");
}

[TestMethod]
public void MultipleExternalWorkbookSingleCell()
{
Test(@"=SUM('C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!$A$1,'C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!$A$2)");
}

[TestMethod]
public void MultipleExternalWorkbookCellRange()
{
Test(@"=SUM('C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!$A$1:$A$10,'C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!$A$11:$A$20)");
}

[TestMethod]
public void MultipleExternalWorkbookDefinedNameLocalScope()
{
Test(@"=SUM('C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!FirstItem,'C:\Users\Willem-Jan\Desktop\[Data.xlsx]Sheet1'!SecondItem)");
}

[TestMethod]
public void MultipleExternalWorkbookDefinedNameGlobalScope()
{
Test(@"=SUM('C:\Users\Willem-Jan\Desktop\Data.xlsx'!Items,'C:\Users\Willem-Jan\Desktop\Data.xlsx'!Items2)");
}

[TestMethod]
public void TestFilePathWithSpace()
{
Expand Down
2 changes: 1 addition & 1 deletion src/XLParser/ExcelFormulaGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public class ExcelFormulaGrammar : Grammar
{ Priority = TerminalPriority.FileName };

// Source: https://stackoverflow.com/a/14632579
private const string fileNameRegex = @"[^\.].*\..{1,4}";
private const string fileNameRegex = @"[^\.]+\..{1,4}";
public Terminal FileNameWindowsToken { get; } = new RegexBasedTerminal(GrammarNames.TokenFileNameWindows, fileNameRegex)
{ Priority = TerminalPriority.FileName };

Expand Down

0 comments on commit 4c706d3

Please sign in to comment.