diff --git a/src/EPPlus/ExternalReferences/BreakFormulaHandler.cs b/src/EPPlus/ExternalReferences/BreakFormulaHandler.cs index ab0667d92..f309eeaff 100644 --- a/src/EPPlus/ExternalReferences/BreakFormulaHandler.cs +++ b/src/EPPlus/ExternalReferences/BreakFormulaHandler.cs @@ -86,7 +86,7 @@ internal static void BreakFormulaLinks(ExcelWorkbook wb, int ix, bool delete) IEnumerable t = ws._formulaTokens?.GetValue(enumerator.Row, enumerator.Column); if (t == null) { - t = SourceCodeTokenizer.Default.Tokenize(formula, ws.Name); + t = SourceCodeTokenizer.Default_KeepWhiteSpaces.Tokenize(formula, ws.Name); } if (HasFormulaExternalReference(wb, ix, t, out string newFormula, false)) { @@ -136,7 +136,7 @@ private static void HandleNames(ExcelWorkbook wb, string wsName, ExcelNamedRange } else { - var t = SourceCodeTokenizer.Default.Tokenize(n.Formula, wsName); + var t = SourceCodeTokenizer.Default_KeepWhiteSpaces.Tokenize(n.Formula, wsName); if (HasFormulaExternalReference(wb, ix, t, out string newFormula, true)) { if(newFormula!="") @@ -215,7 +215,7 @@ private static bool HasFormulaExternalReference(ExcelWorkbook wb, int ix, IEnume address += t.Value; } } - else if (extRefIx >= 0 && (t.TokenTypeIsAddress || t.TokenType==TokenType.NameValue || t.TokenType == TokenType.TableName)) + else if (extRefIx > 0 && (t.TokenTypeIsAddress || t.TokenType==TokenType.NameValue || t.TokenType == TokenType.TableName)) { if (extRefIx < 0) //Current workbook { @@ -247,7 +247,7 @@ private static bool HasFormulaExternalReference(ExcelWorkbook wb, int ix, IEnume } else { - var v= t.TokenTypeIsSet(TokenType.StringContent) ? "\"" + t.Value.Replace("\"", "\"\"") + "\"" : t.Value; + var v= t.Value; if(string.IsNullOrEmpty(address)) { newFormula += v; @@ -260,13 +260,9 @@ private static bool HasFormulaExternalReference(ExcelWorkbook wb, int ix, IEnume } } + newFormula += address; return false; } - - private static string AddApostrophes(string address, bool needsApostrophes) - { - return needsApostrophes ? "'" + address + "'" : address; - } } } diff --git a/src/EPPlusTest/Issues/ExternalLinksIssues.cs b/src/EPPlusTest/Issues/ExternalLinksIssues.cs index c00d7f13f..5af8e5a33 100644 --- a/src/EPPlusTest/Issues/ExternalLinksIssues.cs +++ b/src/EPPlusTest/Issues/ExternalLinksIssues.cs @@ -1,5 +1,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using OfficeOpenXml; +using System; using System.IO; namespace EPPlusTest.Issues { @@ -33,5 +34,33 @@ public void s786() } } } + [TestMethod] + public void s2592() + { + using var lp = OpenPackage("I2592-linkto.xlsx", true); + lp.Workbook.Worksheets.Add("Prices"); + SaveAndCleanup(lp, false); + + // other.xlsx is any workbook with a sheet called "Prices" + using var package = OpenPackage("I2592.xlsx", true); + var sheet = package.Workbook.Worksheets.Add("Sheet1"); + sheet.Cells["A1"].Value = "Qty"; + sheet.Cells["A2"].Value = 1; + sheet.Cells["A3"].Value = 2; + sheet.Tables.Add(sheet.Cells["A1:A3"], "Table1"); + package.Workbook.ExternalLinks.AddExternalWorkbook(new FileInfo(lp.File.FullName)); + sheet.Cells["C1"].Formula = "IF(A2 = 1, \"a\", 0)"; + sheet.Cells["C2"].Formula = "SUM(Table1[Qty])"; + sheet.Cells["C3"].Formula = "[1]Prices!$A$1+1"; + sheet.Calculate(); + using var reopened = new ExcelPackage(new MemoryStream(package.GetAsByteArray())); + reopened.Workbook.ExternalLinks.RemoveAt(0); + + var ws = reopened.Workbook.Worksheets["Sheet1"]; + Assert.AreEqual(sheet.Cells["C1"].Formula, ws.Cells["C1"].Formula); + Assert.AreEqual(sheet.Cells["C2"].Formula, ws.Cells["C2"].Formula); + Assert.IsTrue(string.IsNullOrEmpty(ws.Cells["C3"].Formula)); + Assert.AreEqual(1D, ws.Cells["C3"].Value); + } } }