From 30f7512ff444aae7a1db1aba25cc033b136bd5c8 Mon Sep 17 00:00:00 2001 From: David Rasch Date: Tue, 17 May 2022 14:19:55 +0200 Subject: [PATCH 1/8] fix regexp for networkpath with space --- src/XLParser.Tests/FormulaAnalysisTest.cs | 12 ++++++++++++ src/XLParser/ExcelFormulaGrammar.cs | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/XLParser.Tests/FormulaAnalysisTest.cs b/src/XLParser.Tests/FormulaAnalysisTest.cs index 2a257c9..4e80cb6 100644 --- a/src/XLParser.Tests/FormulaAnalysisTest.cs +++ b/src/XLParser.Tests/FormulaAnalysisTest.cs @@ -383,6 +383,18 @@ public void ExternalWorkbookUrlPathHttpWithSpaceInDocument() Assert.AreEqual("Book 1.xlsx", references.First().FileName); Assert.AreEqual("Sheet1", references.First().Worksheet); } + [TestMethod] + public void ExternalWorkbookNetworkPathWithSpace() + { + // See [#142](https://github.com/spreadsheetlab/XLParser/issues/142) + + List references = new FormulaAnalyzer(@"='\\networkshare\test folder$\[Book 1.xlsx]Sheet1'!$C$33").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"\\networkshare\test folder$\", references.First().FilePath); + Assert.AreEqual("Book 1.xlsx", references.First().FileName); + Assert.AreEqual("Sheet1", references.First().Worksheet); + } [TestMethod] public void ExternalWorkbookUrlPathHttps() diff --git a/src/XLParser/ExcelFormulaGrammar.cs b/src/XLParser/ExcelFormulaGrammar.cs index e092026..564e4ca 100644 --- a/src/XLParser/ExcelFormulaGrammar.cs +++ b/src/XLParser/ExcelFormulaGrammar.cs @@ -184,7 +184,7 @@ public class ExcelFormulaGrammar : Grammar { Priority = TerminalPriority.FileName }; // Source: http://stackoverflow.com/a/6416209/572635 - private const string windowsFilePathRegex = @"(?:[a-zA-Z]:|\\?\\?[\w\.-]+\\[\w.$]+)\\(([^<>:""/\|?*\\]| )+\\)*"; + private const string windowsFilePathRegex = @"(?:[a-zA-Z]:|\\?\\?[\w\.-]+\\[\w.$ ]+)\\(([^<>:""/\|?*\\]| )+\\)*"; private const string urlPathRegex = @"http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_ ]*)?"; private const string filePathRegex = @"(" + windowsFilePathRegex + @"|" + urlPathRegex + @")"; public Terminal FilePathToken { get; } = new RegexBasedTerminal(GrammarNames.TokenFilePath, filePathRegex); From 46f0d7e92e2431555bb2f5dba3af77370489300d Mon Sep 17 00:00:00 2001 From: David Rasch Date: Tue, 17 May 2022 16:56:12 +0200 Subject: [PATCH 2/8] fix urlpath regex dont match document --- src/XLParser.Tests/FormulaAnalysisTest.cs | 26 +++++++++++++++++++++-- src/XLParser/ExcelFormulaGrammar.cs | 5 +++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/XLParser.Tests/FormulaAnalysisTest.cs b/src/XLParser.Tests/FormulaAnalysisTest.cs index 2a257c9..c4d0735 100644 --- a/src/XLParser.Tests/FormulaAnalysisTest.cs +++ b/src/XLParser.Tests/FormulaAnalysisTest.cs @@ -387,14 +387,36 @@ public void ExternalWorkbookUrlPathHttpWithSpaceInDocument() [TestMethod] public void ExternalWorkbookUrlPathHttps() { - List references = new FormulaAnalyzer(@"='https://d.docs.live.net/3fade139bf25879f/Documents/[Tracer.xlsx]Sheet2'!$C$5+Sheet10!J44").ParserReferences().ToList(); + List references = new FormulaAnalyzer(@"='https://d.docs.live.net/3fade139bf25879f/Documents/[Tracer.xlsx]Sheet2'!$C$5").ParserReferences().ToList(); - Assert.AreEqual(2, references.Count); + Assert.AreEqual(1, references.Count); Assert.AreEqual(@"https://d.docs.live.net/3fade139bf25879f/Documents/", references.First().FilePath); Assert.AreEqual("Tracer.xlsx", references.First().FileName); Assert.AreEqual("Sheet2", references.First().Worksheet); } + [TestMethod] + public void ExternalWorkbookDocumentWithoutBracketsWithSpace() + { + List references = new FormulaAnalyzer(@"='https://preview.perfectxl.com/Financial Sample.xlsx'!financials").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"https://preview.perfectxl.com/", references.First().FilePath); + Assert.AreEqual("Financial Sample.xlsx", references.First().FileName); + Assert.AreEqual("financials", references.First().Name); + } + + [TestMethod] + public void ExternalWorkbookDocumentWithoutBrackets() + { + List references = new FormulaAnalyzer(@"='https://preview.perfectxl.com/FinancialSample.xlsx'!financials").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"https://preview.perfectxl.com/", references.First().FilePath); + Assert.AreEqual("FinancialSample.xlsx", references.First().FileName); + Assert.AreEqual("financials", references.First().Name); + } + [TestMethod] public void ExternalWorkbookRelativePath() { diff --git a/src/XLParser/ExcelFormulaGrammar.cs b/src/XLParser/ExcelFormulaGrammar.cs index e092026..156d6b9 100644 --- a/src/XLParser/ExcelFormulaGrammar.cs +++ b/src/XLParser/ExcelFormulaGrammar.cs @@ -185,7 +185,7 @@ public class ExcelFormulaGrammar : Grammar // Source: http://stackoverflow.com/a/6416209/572635 private const string windowsFilePathRegex = @"(?:[a-zA-Z]:|\\?\\?[\w\.-]+\\[\w.$]+)\\(([^<>:""/\|?*\\]| )+\\)*"; - private const string urlPathRegex = @"http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_ ]*)?"; + private const string urlPathRegex = @"http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*[/]([a-zA-Z0-9\-\.\?\,\'+&%\$#_ ]*[/])*"; private const string filePathRegex = @"(" + windowsFilePathRegex + @"|" + urlPathRegex + @")"; public Terminal FilePathToken { get; } = new RegexBasedTerminal(GrammarNames.TokenFilePath, filePathRegex); #endregion @@ -385,7 +385,7 @@ public ExcelFormulaGrammar() : base(false) | FileNameEnclosedInBracketsToken | FilePathToken + FileNameEnclosedInBracketsToken | FilePathToken + FileName - ; + ; DynamicDataExchange.Rule = File + exclamationMark + SingleQuotedStringToken; @@ -494,6 +494,7 @@ private static class TerminalPriority public const int ReservedName = -700; public const int FileName = -500; + public const int FilePathToken = -501; public const int SingleQuotedString = -100; From ee19d5ef1b026a71c589ba19be97db8e1146673b Mon Sep 17 00:00:00 2001 From: David Rasch Date: Wed, 18 May 2022 16:01:24 +0200 Subject: [PATCH 3/8] fix network path with quotes --- src/XLParser.Tests/FormulaAnalysisTest.cs | 30 +++++++++++++++++++++++ src/XLParser/ExcelFormulaGrammar.cs | 4 +-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/XLParser.Tests/FormulaAnalysisTest.cs b/src/XLParser.Tests/FormulaAnalysisTest.cs index bdb9a05..9fb49da 100644 --- a/src/XLParser.Tests/FormulaAnalysisTest.cs +++ b/src/XLParser.Tests/FormulaAnalysisTest.cs @@ -383,6 +383,7 @@ public void ExternalWorkbookUrlPathHttpWithSpaceInDocument() Assert.AreEqual("Book 1.xlsx", references.First().FileName); Assert.AreEqual("Sheet1", references.First().Worksheet); } + [TestMethod] public void ExternalWorkbookNetworkPathWithSpace() { @@ -396,6 +397,35 @@ public void ExternalWorkbookNetworkPathWithSpace() Assert.AreEqual("Sheet1", references.First().Worksheet); } + + [TestMethod] + public void ExternalWorkbookNetworkPathWithQuotes() + { + // See [#135](https://github.com/spreadsheetlab/XLParser/issues/135) + + List references = new FormulaAnalyzer(@"='\\SRV01\[TestFile.xls]TestSheet'!#REF!").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"\\SRV01\", references.First().FilePath); + Assert.AreEqual("TestFile.xls", references.First().FileName); + Assert.AreEqual("TestSheet", references.First().Worksheet); + } + + [TestMethod] + public void ExternalWorkbookNetworkPathWithQuotesAndFolder() + { + // See [#135](https://github.com/spreadsheetlab/XLParser/issues/135) + + List references = new FormulaAnalyzer(@"='\\SRV01\Test Folder\[Test File.xls]Test Sheet'!#REF! + +").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"\\SRV01\Test Folder\", references.First().FilePath); + Assert.AreEqual("Test File.xls", references.First().FileName); + Assert.AreEqual("Test Sheet", references.First().Worksheet); + } + [TestMethod] public void ExternalWorkbookUrlPathHttps() { diff --git a/src/XLParser/ExcelFormulaGrammar.cs b/src/XLParser/ExcelFormulaGrammar.cs index 156d6b9..87f3a54 100644 --- a/src/XLParser/ExcelFormulaGrammar.cs +++ b/src/XLParser/ExcelFormulaGrammar.cs @@ -179,12 +179,12 @@ 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 FileName { get; } = new RegexBasedTerminal(GrammarNames.TokenFileName, fileNameRegex) { Priority = TerminalPriority.FileName }; // Source: http://stackoverflow.com/a/6416209/572635 - private const string windowsFilePathRegex = @"(?:[a-zA-Z]:|\\?\\?[\w\.-]+\\[\w.$]+)\\(([^<>:""/\|?*\\]| )+\\)*"; + private const string windowsFilePathRegex = @"(?:[a-zA-Z]:|\\?\\?[\w\-.$ ]+)\\(([^<>:\""/\|?*\\]| )+\\)*"; private const string urlPathRegex = @"http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*[/]([a-zA-Z0-9\-\.\?\,\'+&%\$#_ ]*[/])*"; private const string filePathRegex = @"(" + windowsFilePathRegex + @"|" + urlPathRegex + @")"; public Terminal FilePathToken { get; } = new RegexBasedTerminal(GrammarNames.TokenFilePath, filePathRegex); From df704d8bd10e72510671f515ee88a28c5986ba69 Mon Sep 17 00:00:00 2001 From: David Rasch Date: Wed, 18 May 2022 16:06:51 +0200 Subject: [PATCH 4/8] cleanup --- src/XLParser/ExcelFormulaGrammar.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/XLParser/ExcelFormulaGrammar.cs b/src/XLParser/ExcelFormulaGrammar.cs index 87f3a54..8e454c7 100644 --- a/src/XLParser/ExcelFormulaGrammar.cs +++ b/src/XLParser/ExcelFormulaGrammar.cs @@ -385,7 +385,7 @@ public ExcelFormulaGrammar() : base(false) | FileNameEnclosedInBracketsToken | FilePathToken + FileNameEnclosedInBracketsToken | FilePathToken + FileName - ; + ; DynamicDataExchange.Rule = File + exclamationMark + SingleQuotedStringToken; @@ -494,7 +494,6 @@ private static class TerminalPriority public const int ReservedName = -700; public const int FileName = -500; - public const int FilePathToken = -501; public const int SingleQuotedString = -100; From 681a513614151a83a25da456104c55fa76fb46fb Mon Sep 17 00:00:00 2001 From: David Rasch Date: Thu, 19 May 2022 13:50:59 +0200 Subject: [PATCH 5/8] fix defined name als filepath --- src/XLParser.Tests/FormulaAnalysisTest.cs | 10 ++++++++++ src/XLParser/ExcelFormulaGrammar.cs | 10 ++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/XLParser.Tests/FormulaAnalysisTest.cs b/src/XLParser.Tests/FormulaAnalysisTest.cs index 9fb49da..f42cffd 100644 --- a/src/XLParser.Tests/FormulaAnalysisTest.cs +++ b/src/XLParser.Tests/FormulaAnalysisTest.cs @@ -470,6 +470,16 @@ public void ExternalWorkbookRelativePath() Assert.AreEqual("Sheet1", references.First().Worksheet); } + [TestMethod] + public void ExternalWorkbookRelativePathIsDefinedName() + { + List references = new FormulaAnalyzer(@"=Test\Folder").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"Test\Folder", references.First().Name); + Assert.AreEqual(ReferenceType.UserDefinedName, references.First().ReferenceType); + } + [TestMethod] public void ExternalWorkbookSingleCell() { diff --git a/src/XLParser/ExcelFormulaGrammar.cs b/src/XLParser/ExcelFormulaGrammar.cs index 8e454c7..7dde778 100644 --- a/src/XLParser/ExcelFormulaGrammar.cs +++ b/src/XLParser/ExcelFormulaGrammar.cs @@ -187,7 +187,9 @@ public class ExcelFormulaGrammar : Grammar private const string windowsFilePathRegex = @"(?:[a-zA-Z]:|\\?\\?[\w\-.$ ]+)\\(([^<>:\""/\|?*\\]| )+\\)*"; private const string urlPathRegex = @"http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*[/]([a-zA-Z0-9\-\.\?\,\'+&%\$#_ ]*[/])*"; private const string filePathRegex = @"(" + windowsFilePathRegex + @"|" + urlPathRegex + @")"; - public Terminal FilePathToken { get; } = new RegexBasedTerminal(GrammarNames.TokenFilePath, filePathRegex); + public Terminal FilePathToken { get; } = new RegexBasedTerminal(GrammarNames.TokenFilePath, filePathRegex) + { Priority = TerminalPriority.FileNamePath }; + #endregion #endregion @@ -389,7 +391,10 @@ public ExcelFormulaGrammar() : base(false) DynamicDataExchange.Rule = File + exclamationMark + SingleQuotedStringToken; - NamedRange.Rule = NameToken | NamedRangeCombinationToken; + NamedRange.Rule = + NameToken + | NamedRangeCombinationToken + ; Prefix.Rule = SheetToken @@ -494,6 +499,7 @@ private static class TerminalPriority public const int ReservedName = -700; public const int FileName = -500; + public const int FileNamePath = -800; public const int SingleQuotedString = -100; From 358183bb8138697b43154b50af6dfa65d34ed9c5 Mon Sep 17 00:00:00 2001 From: David Rasch Date: Thu, 19 May 2022 14:14:52 +0200 Subject: [PATCH 6/8] add round brackets for url paths and documents --- src/XLParser/ExcelFormulaGrammar.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/XLParser/ExcelFormulaGrammar.cs b/src/XLParser/ExcelFormulaGrammar.cs index 7dde778..b108548 100644 --- a/src/XLParser/ExcelFormulaGrammar.cs +++ b/src/XLParser/ExcelFormulaGrammar.cs @@ -185,7 +185,7 @@ public class ExcelFormulaGrammar : Grammar // Source: http://stackoverflow.com/a/6416209/572635 private const string windowsFilePathRegex = @"(?:[a-zA-Z]:|\\?\\?[\w\-.$ ]+)\\(([^<>:\""/\|?*\\]| )+\\)*"; - private const string urlPathRegex = @"http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*[/]([a-zA-Z0-9\-\.\?\,\'+&%\$#_ ]*[/])*"; + private const string urlPathRegex = @"http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*[/]([a-zA-Z0-9\-\.\?\,\'+&%\$#_ ()]*[/])*"; private const string filePathRegex = @"(" + windowsFilePathRegex + @"|" + urlPathRegex + @")"; public Terminal FilePathToken { get; } = new RegexBasedTerminal(GrammarNames.TokenFilePath, filePathRegex) { Priority = TerminalPriority.FileNamePath }; From 023b5d483b5dedf874ad35c9201be33b0e09e98c Mon Sep 17 00:00:00 2001 From: David Rasch Date: Thu, 19 May 2022 14:15:08 +0200 Subject: [PATCH 7/8] add all test for fixed issues --- src/XLParser.Tests/FormulaAnalysisTest.cs | 98 ++++++++++++++++++++++- 1 file changed, 95 insertions(+), 3 deletions(-) diff --git a/src/XLParser.Tests/FormulaAnalysisTest.cs b/src/XLParser.Tests/FormulaAnalysisTest.cs index f42cffd..e06f413 100644 --- a/src/XLParser.Tests/FormulaAnalysisTest.cs +++ b/src/XLParser.Tests/FormulaAnalysisTest.cs @@ -397,6 +397,18 @@ public void ExternalWorkbookNetworkPathWithSpace() Assert.AreEqual("Sheet1", references.First().Worksheet); } + [TestMethod] + public void ExternalWorkbookNetworkPathWithoutSpaceInFolder() + { + // See [#142](https://github.com/spreadsheetlab/XLParser/issues/142) + + List references = new FormulaAnalyzer(@"='\\networkshare\testfolder$\[Book 1.xlsx]Sheet1'!$C$33").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"\\networkshare\testfolder$\", references.First().FilePath); + Assert.AreEqual("Book 1.xlsx", references.First().FileName); + Assert.AreEqual("Sheet1", references.First().Worksheet); + } [TestMethod] public void ExternalWorkbookNetworkPathWithQuotes() @@ -412,16 +424,42 @@ public void ExternalWorkbookNetworkPathWithQuotes() } [TestMethod] - public void ExternalWorkbookNetworkPathWithQuotesAndFolder() + public void ExternalWorkbookNetworkPathWithQuotesAndSpaces() { // See [#135](https://github.com/spreadsheetlab/XLParser/issues/135) - List references = new FormulaAnalyzer(@"='\\SRV01\Test Folder\[Test File.xls]Test Sheet'!#REF! + List references = new FormulaAnalyzer(@"='\\SRV01\[Test File.xls]Test Sheet'!#REF!").ParserReferences().ToList(); -").ParserReferences().ToList(); + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"\\SRV01\", references.First().FilePath); + Assert.AreEqual("Test File.xls", references.First().FileName); + Assert.AreEqual("Test Sheet", references.First().Worksheet); + } + + [TestMethod] + public void ExternalWorkbookNetworkPathWithQuotesAndSpaceInFolder() + { + // See [#135](https://github.com/spreadsheetlab/XLParser/issues/135) + + List references = new FormulaAnalyzer(@"='\\SRV01\Test Folder\[TestFile.xls]Test Sheet'!#REF!").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); Assert.AreEqual(@"\\SRV01\Test Folder\", references.First().FilePath); + Assert.AreEqual("TestFile.xls", references.First().FileName); + Assert.AreEqual("Test Sheet", references.First().Worksheet); + } + + [TestMethod] + public void ExternalWorkbookNetworkPathWithQuotesAndSpaceInDocument() + { + // See [#135](https://github.com/spreadsheetlab/XLParser/issues/135) + + List references = new FormulaAnalyzer(@"='\\SRV01\TestFolder\[Test File.xls]Test Sheet'!#REF! + +").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"\\SRV01\TestFolder\", references.First().FilePath); Assert.AreEqual("Test File.xls", references.First().FileName); Assert.AreEqual("Test Sheet", references.First().Worksheet); } @@ -440,6 +478,7 @@ public void ExternalWorkbookUrlPathHttps() [TestMethod] public void ExternalWorkbookDocumentWithoutBracketsWithSpace() { + // See [#137](https://github.com/spreadsheetlab/XLParser/issues/137) List references = new FormulaAnalyzer(@"='https://preview.perfectxl.com/Financial Sample.xlsx'!financials").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -451,6 +490,7 @@ public void ExternalWorkbookDocumentWithoutBracketsWithSpace() [TestMethod] public void ExternalWorkbookDocumentWithoutBrackets() { + // See [#137](https://github.com/spreadsheetlab/XLParser/issues/135) List references = new FormulaAnalyzer(@"='https://preview.perfectxl.com/FinancialSample.xlsx'!financials").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -480,6 +520,58 @@ public void ExternalWorkbookRelativePathIsDefinedName() Assert.AreEqual(ReferenceType.UserDefinedName, references.First().ReferenceType); } + [TestMethod] + public void ExternalWorkbookUrlPathHttpWithRoundBracketsInDocument() + { + // See [#140](https://github.com/spreadsheetlab/XLParser/issues/140) + + List references = new FormulaAnalyzer(@"='http://example.com/testfolder(brackets)/[Book 1.xlsx]Sheet1'!$A$1").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"http://example.com/testfolder(brackets)/", references.First().FilePath); + Assert.AreEqual("Book 1.xlsx", references.First().FileName); + Assert.AreEqual("Sheet1", references.First().Worksheet); + } + + [TestMethod] + public void ExternalWorkbookUrlPathHttpWithRoundBrackets() + { + // See [#140](https://github.com/spreadsheetlab/XLParser/issues/140) + + List references = new FormulaAnalyzer(@"='http://example.com/testfolder(brackets)/[Book (1).xlsx]Sheet1'!$A$1").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"http://example.com/testfolder(brackets)/", references.First().FilePath); + Assert.AreEqual("Book (1).xlsx", references.First().FileName); + Assert.AreEqual("Sheet1", references.First().Worksheet); + } + + [TestMethod] + public void ExternalWorkbookPathWithRoundBracketsInDocument() + { + // See [#140](https://github.com/spreadsheetlab/XLParser/issues/140) + + List references = new FormulaAnalyzer(@"='c:\My documents\[Book 1 (copy).xlsx]Sheet1'!$A$1").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"c:\My documents\", references.First().FilePath); + Assert.AreEqual("Book 1 (copy).xlsx", references.First().FileName); + Assert.AreEqual("Sheet1", references.First().Worksheet); + } + + [TestMethod] + public void ExternalWorkbookPathWithRoundBrackets() + { + // See [#140](https://github.com/spreadsheetlab/XLParser/issues/140) + + List references = new FormulaAnalyzer(@"='c:\My documents (copy)\[Book 1 (copy).xlsx]Sheet1'!$A$1").ParserReferences().ToList(); + + Assert.AreEqual(1, references.Count); + Assert.AreEqual(@"c:\My documents (copy)\", references.First().FilePath); + Assert.AreEqual("Book 1 (copy).xlsx", references.First().FileName); + Assert.AreEqual("Sheet1", references.First().Worksheet); + } + [TestMethod] public void ExternalWorkbookSingleCell() { From a5d8703f6b1c535835e45f7cf069d383b65ae1e3 Mon Sep 17 00:00:00 2001 From: David Rasch Date: Thu, 19 May 2022 16:07:53 +0200 Subject: [PATCH 8/8] layout fixes --- src/XLParser.Tests/FormulaAnalysisTest.cs | 16 +--------------- src/XLParser/ExcelFormulaGrammar.cs | 9 +++------ 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/src/XLParser.Tests/FormulaAnalysisTest.cs b/src/XLParser.Tests/FormulaAnalysisTest.cs index e06f413..ab62829 100644 --- a/src/XLParser.Tests/FormulaAnalysisTest.cs +++ b/src/XLParser.Tests/FormulaAnalysisTest.cs @@ -362,7 +362,6 @@ public void ExternalWorkbookUrlPathHttp() public void ExternalWorkbookUrlPathHttpWithSpaceInPath() { // See [#138](https://github.com/spreadsheetlab/XLParser/issues/138) - List references = new FormulaAnalyzer(@"='http://example.com/test folder/[Book1.xlsx]Sheet1'!$A$1").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -375,7 +374,6 @@ public void ExternalWorkbookUrlPathHttpWithSpaceInPath() public void ExternalWorkbookUrlPathHttpWithSpaceInDocument() { // See [#138](https://github.com/spreadsheetlab/XLParser/issues/138) - List references = new FormulaAnalyzer(@"='http://example.com/testfolder/[Book 1.xlsx]Sheet1'!$A$1").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -388,7 +386,6 @@ public void ExternalWorkbookUrlPathHttpWithSpaceInDocument() public void ExternalWorkbookNetworkPathWithSpace() { // See [#142](https://github.com/spreadsheetlab/XLParser/issues/142) - List references = new FormulaAnalyzer(@"='\\networkshare\test folder$\[Book 1.xlsx]Sheet1'!$C$33").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -401,7 +398,6 @@ public void ExternalWorkbookNetworkPathWithSpace() public void ExternalWorkbookNetworkPathWithoutSpaceInFolder() { // See [#142](https://github.com/spreadsheetlab/XLParser/issues/142) - List references = new FormulaAnalyzer(@"='\\networkshare\testfolder$\[Book 1.xlsx]Sheet1'!$C$33").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -414,7 +410,6 @@ public void ExternalWorkbookNetworkPathWithoutSpaceInFolder() public void ExternalWorkbookNetworkPathWithQuotes() { // See [#135](https://github.com/spreadsheetlab/XLParser/issues/135) - List references = new FormulaAnalyzer(@"='\\SRV01\[TestFile.xls]TestSheet'!#REF!").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -427,7 +422,6 @@ public void ExternalWorkbookNetworkPathWithQuotes() public void ExternalWorkbookNetworkPathWithQuotesAndSpaces() { // See [#135](https://github.com/spreadsheetlab/XLParser/issues/135) - List references = new FormulaAnalyzer(@"='\\SRV01\[Test File.xls]Test Sheet'!#REF!").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -440,7 +434,6 @@ public void ExternalWorkbookNetworkPathWithQuotesAndSpaces() public void ExternalWorkbookNetworkPathWithQuotesAndSpaceInFolder() { // See [#135](https://github.com/spreadsheetlab/XLParser/issues/135) - List references = new FormulaAnalyzer(@"='\\SRV01\Test Folder\[TestFile.xls]Test Sheet'!#REF!").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -453,10 +446,7 @@ public void ExternalWorkbookNetworkPathWithQuotesAndSpaceInFolder() public void ExternalWorkbookNetworkPathWithQuotesAndSpaceInDocument() { // See [#135](https://github.com/spreadsheetlab/XLParser/issues/135) - - List references = new FormulaAnalyzer(@"='\\SRV01\TestFolder\[Test File.xls]Test Sheet'!#REF! - -").ParserReferences().ToList(); + List references = new FormulaAnalyzer(@"='\\SRV01\TestFolder\[Test File.xls]Test Sheet'!#REF!").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); Assert.AreEqual(@"\\SRV01\TestFolder\", references.First().FilePath); @@ -524,7 +514,6 @@ public void ExternalWorkbookRelativePathIsDefinedName() public void ExternalWorkbookUrlPathHttpWithRoundBracketsInDocument() { // See [#140](https://github.com/spreadsheetlab/XLParser/issues/140) - List references = new FormulaAnalyzer(@"='http://example.com/testfolder(brackets)/[Book 1.xlsx]Sheet1'!$A$1").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -537,7 +526,6 @@ public void ExternalWorkbookUrlPathHttpWithRoundBracketsInDocument() public void ExternalWorkbookUrlPathHttpWithRoundBrackets() { // See [#140](https://github.com/spreadsheetlab/XLParser/issues/140) - List references = new FormulaAnalyzer(@"='http://example.com/testfolder(brackets)/[Book (1).xlsx]Sheet1'!$A$1").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -550,7 +538,6 @@ public void ExternalWorkbookUrlPathHttpWithRoundBrackets() public void ExternalWorkbookPathWithRoundBracketsInDocument() { // See [#140](https://github.com/spreadsheetlab/XLParser/issues/140) - List references = new FormulaAnalyzer(@"='c:\My documents\[Book 1 (copy).xlsx]Sheet1'!$A$1").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); @@ -563,7 +550,6 @@ public void ExternalWorkbookPathWithRoundBracketsInDocument() public void ExternalWorkbookPathWithRoundBrackets() { // See [#140](https://github.com/spreadsheetlab/XLParser/issues/140) - List references = new FormulaAnalyzer(@"='c:\My documents (copy)\[Book 1 (copy).xlsx]Sheet1'!$A$1").ParserReferences().ToList(); Assert.AreEqual(1, references.Count); diff --git a/src/XLParser/ExcelFormulaGrammar.cs b/src/XLParser/ExcelFormulaGrammar.cs index b108548..4589423 100644 --- a/src/XLParser/ExcelFormulaGrammar.cs +++ b/src/XLParser/ExcelFormulaGrammar.cs @@ -176,12 +176,12 @@ public class ExcelFormulaGrammar : Grammar private const string fileNameInBracketsRegex = @"\[[^\[\]]+\]"; public Terminal FileNameEnclosedInBracketsToken { get; } = new RegexBasedTerminal(GrammarNames.TokenFileNameEnclosedInBrackets, fileNameInBracketsRegex) - { Priority = TerminalPriority.FileName }; + { Priority = TerminalPriority.FileName }; // Source: https://stackoverflow.com/a/14632579 private const string fileNameRegex = @"[^\.\\]+\..{1,4}"; public Terminal FileName { get; } = new RegexBasedTerminal(GrammarNames.TokenFileName, fileNameRegex) - { Priority = TerminalPriority.FileName }; + { Priority = TerminalPriority.FileName }; // Source: http://stackoverflow.com/a/6416209/572635 private const string windowsFilePathRegex = @"(?:[a-zA-Z]:|\\?\\?[\w\-.$ ]+)\\(([^<>:\""/\|?*\\]| )+\\)*"; @@ -391,10 +391,7 @@ public ExcelFormulaGrammar() : base(false) DynamicDataExchange.Rule = File + exclamationMark + SingleQuotedStringToken; - NamedRange.Rule = - NameToken - | NamedRangeCombinationToken - ; + NamedRange.Rule = NameToken | NamedRangeCombinationToken; Prefix.Rule = SheetToken