From c4349d329aa61fada2b1e630cf6b7ef3bfcf5ef9 Mon Sep 17 00:00:00 2001 From: MarkHung Date: Tue, 26 Dec 2017 11:47:31 +0800 Subject: [PATCH] BugFix: process address with sheet name --- EPPlus/ExcelCellBase.cs | 23 ++++++++++++++++---- EPPlusTest/EPPlusTest.csproj | 1 + EPPlusTest/FormulaParsing/FormulaR1C1Test.cs | 8 +++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/EPPlus/ExcelCellBase.cs b/EPPlus/ExcelCellBase.cs index 5a413036..7ff2c18a 100644 --- a/EPPlus/ExcelCellBase.cs +++ b/EPPlus/ExcelCellBase.cs @@ -139,6 +139,13 @@ private static string Translate(string value, dlgTransl addressTranslator, int r /// private static string ToR1C1(string part, int row, int col, int rowIncr, int colIncr) { + int shInd = part.IndexOf('!'); + string sh = ""; + if (shInd > 0) + { + sh = part.Substring(0, shInd + 1); + part = part.Substring(shInd + 1); + } int delim = part.IndexOf(':'); if (delim > 0) { @@ -146,11 +153,11 @@ private static string ToR1C1(string part, int row, int col, int rowIncr, int col string p2 = ToR1C1_1(part.Substring(delim + 1), row, col, rowIncr, colIncr); if (p1.Equals(p2)) return p1; - return p1 + ":" + p2; + return sh + p1 + ":" + p2; } else - return ToR1C1_1(part, row, col, rowIncr, colIncr); + return sh + ToR1C1_1(part, row, col, rowIncr, colIncr); } private static string ToR1C1_1(string part, int row, int col, int rowIncr, int colIncr) { @@ -190,6 +197,14 @@ private static string ToR1C1_1(string part, int row, int col, int rowIncr, int c /// private static string ToAbs(string part, int row, int col, int rowIncr, int colIncr) { + int shInd = part.IndexOf('!'); + string sh = ""; + if (shInd > 0) + { + sh = part.Substring(0, shInd + 1); + part = part.Substring(shInd + 1); + } + int delim = part.IndexOf(':'); if (delim > 0) { @@ -197,10 +212,10 @@ private static string ToAbs(string part, int row, int col, int rowIncr, int colI string p2 = ToAbs_1(part.Substring(delim + 1), row, col, rowIncr, colIncr); if (p1.Equals(p2)) return p1; - return p1 + ":" + p2; + return sh + p1 + ":" + p2; } else - return ToAbs_1(part, row, col, rowIncr, colIncr); + return sh + ToAbs_1(part, row, col, rowIncr, colIncr); } private static string ToAbs_1(string part, int row, int col, int rowIncr, int colIncr) { diff --git a/EPPlusTest/EPPlusTest.csproj b/EPPlusTest/EPPlusTest.csproj index 95efa9bb..b1fac8e6 100644 --- a/EPPlusTest/EPPlusTest.csproj +++ b/EPPlusTest/EPPlusTest.csproj @@ -114,6 +114,7 @@ + diff --git a/EPPlusTest/FormulaParsing/FormulaR1C1Test.cs b/EPPlusTest/FormulaParsing/FormulaR1C1Test.cs index 1c1aac32..299e59ed 100644 --- a/EPPlusTest/FormulaParsing/FormulaR1C1Test.cs +++ b/EPPlusTest/FormulaParsing/FormulaR1C1Test.cs @@ -69,6 +69,14 @@ public void C2Abs() Assert.AreEqual("SUM($B:$B)", f); } [TestMethod] + public void C2AbsWithSheet() + { + string fR1C1 = "SUM(A!C2)"; + _sheet.Cells[5, 3].FormulaR1C1 = fR1C1; + string f = _sheet.Cells[5, 3].Formula; + Assert.AreEqual("SUM(A!$B:$B)", f); + } + [TestMethod] public void C2() { string fR1C1 = "SUM(C2)";