diff --git a/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/Excel/Cells/XLCell.cs index d667470..a712995 100644 --- a/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/Excel/Cells/XLCell.cs @@ -59,7 +59,7 @@ , RegexOptions.Compiled); private static readonly Regex R1C1Regex = new Regex( - @"(?<=\W)([Rr]\[?-?\d{0,7}\]?[Cc]\[?-?\d{0,7}\]?)(?=\W)" // R1C1 + @"(?<=\W)([Rr](?:\[-?\d{0,7}\]|\d{0,7})?[Cc](?:\[-?\d{0,7}\]|\d{0,7})?)(?=\W)" // R1C1 + @"|(?<=\W)([Rr]\[?-?\d{0,7}\]?:[Rr]\[?-?\d{0,7}\]?)(?=\W)" // R:R + @"|(?<=\W)([Cc]\[?-?\d{0,5}\]?:[Cc]\[?-?\d{0,5}\]?)(?=\W)", RegexOptions.Compiled); // C:C @@ -1960,13 +1960,13 @@ public IXLCell CopyFrom(IXLCell otherCell, Boolean copyDataValidations) { - var castedOtherCell = otherCell as XLCell; // To expose GetFormulaR1C1, etc - var source = castedOtherCell; - CopyValues(castedOtherCell); + var source = otherCell as XLCell; // To expose GetFormulaR1C1, etc + //var source = castedOtherCell; + CopyValues(source); SetStyle(source._style ?? source.Worksheet.Workbook.GetStyleById(source._styleCacheId)); - var conditionalFormats = castedOtherCell.Worksheet.ConditionalFormats.Where(c => c.Range.Contains(castedOtherCell)).ToList(); + var conditionalFormats = source.Worksheet.ConditionalFormats.Where(c => c.Range.Contains(source)).ToList(); foreach (var cf in conditionalFormats) { var c = new XLConditionalFormat(cf as XLConditionalFormat) {Range = AsRange()}; @@ -1977,7 +1977,7 @@ var f = v.Value; if (v.IsFormula) { - var r1c1 = castedOtherCell.GetFormulaR1C1(f); + var r1c1 = source.GetFormulaR1C1(f); f = GetFormulaA1(r1c1); } @@ -1992,8 +1992,8 @@ { var eventTracking = Worksheet.EventTrackingEnabled; Worksheet.EventTrackingEnabled = false; - if (castedOtherCell.HasDataValidation) - CopyDataValidation(castedOtherCell, castedOtherCell.DataValidation); + if (source.HasDataValidation) + CopyDataValidation(source, source.DataValidation); else if (HasDataValidation) { using (var asRange = AsRange()) diff --git a/ClosedXML_Tests/Excel/Misc/FormulaTests.cs b/ClosedXML_Tests/Excel/Misc/FormulaTests.cs index 9d7cc15..28c1a53 100644 --- a/ClosedXML_Tests/Excel/Misc/FormulaTests.cs +++ b/ClosedXML_Tests/Excel/Misc/FormulaTests.cs @@ -18,5 +18,26 @@ ws.Cell("A1").CopyTo("A2"); Assert.AreEqual("B2", ws.Cell("A2").FormulaA1); } + + [Test] + public void CopyFormula2() + { + using (var wb = new XLWorkbook()) + { + IXLWorksheet ws = wb.Worksheets.Add("Sheet1"); + + ws.Cell("A1").FormulaA1 = "A2-1"; + ws.Cell("A1").CopyTo("B1"); + Assert.AreEqual("R[1]C-1", ws.Cell("A1").FormulaR1C1); + Assert.AreEqual("R[1]C-1", ws.Cell("B1").FormulaR1C1); + Assert.AreEqual("B2-1", ws.Cell("B1").FormulaA1); + + ws.Cell("A1").FormulaA1 = "B1+1"; + ws.Cell("A1").CopyTo("A2"); + Assert.AreEqual("RC[1]+1", ws.Cell("A1").FormulaR1C1); + Assert.AreEqual("RC[1]+1", ws.Cell("A2").FormulaR1C1); + Assert.AreEqual("B2+1", ws.Cell("A2").FormulaA1); + } + } } -} \ No newline at end of file +}