diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs index 9f5c80a..6cd4edb 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/IXLCell.cs @@ -321,5 +321,7 @@ IXLCell SetActive(Boolean value = true); Boolean HasFormula { get; } + + IXLRangeAddress FormulaReference { get; set; } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs index f5041b6..12fe3c9 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCell.cs @@ -1602,7 +1602,7 @@ { _dataType = XLCellValues.DateTime; - if (style.NumberFormat.Format == String.Empty && style.NumberFormat.NumberFormatId == 0) + if (style.NumberFormat.Format == String.Empty && style.NumberFormat.NumberFormatId == 0) Style.NumberFormat.NumberFormatId = dtTest.Date == dtTest ? 14 : 22; val = dtTest.ToOADate().ToString(); @@ -2528,5 +2528,7 @@ #endregion public Boolean HasFormula { get { return !XLHelper.IsNullOrWhiteSpace(FormulaA1); } } + + public IXLRangeAddress FormulaReference { get; set; } } } \ No newline at end of file diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs index 202117a..914e55b 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Ranges/XLRangeBase.cs @@ -187,12 +187,26 @@ public String FormulaA1 { - set { Cells().ForEach(c => c.FormulaA1 = value); } + set + { + Cells().ForEach(c => + { + c.FormulaA1 = value; + c.FormulaReference = RangeAddress; + }); + } } public String FormulaR1C1 { - set { Cells().ForEach(c => c.FormulaR1C1 = value); } + set + { + Cells().ForEach(c => + { + c.FormulaR1C1 = value; + c.FormulaReference = RangeAddress; + }); + } } public Boolean ShareString diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs index e994f93..939cce3 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs @@ -752,6 +752,9 @@ else formula = cell.CellFormula.Text; + if (cell.CellFormula.Reference != null) + xlCell.FormulaReference = ws.Range(cell.CellFormula.Reference.Value).RangeAddress; + xlCell.FormulaA1 = formula; sharedFormulasR1C1.Add(cell.CellFormula.SharedIndex.Value, xlCell.FormulaR1C1); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs index a812034..be31112 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs @@ -861,14 +861,22 @@ if (c.FormulaA1.StartsWith("{")) { - calculationChain.AppendChild(new CalculationCell - { - CellReference = c.Address.ToString(), - SheetId = worksheet.SheetId, - Array = true - }); - calculationChain.AppendChild(new CalculationCell - {CellReference = c.Address.ToString(), InChildChain = true}); + var cc = new CalculationCell + { + CellReference = c.Address.ToString(), + SheetId = worksheet.SheetId + }; + + if (c.FormulaReference.FirstAddress.Equals(c.Address)) + { + cc.Array = true; + calculationChain.AppendChild(cc); + calculationChain.AppendChild(new CalculationCell { CellReference = c.Address.ToString(), InChildChain = true }); + } + else + { + calculationChain.AppendChild(cc); + } } else { @@ -4082,11 +4090,16 @@ if (formula.StartsWith("{")) { formula = formula.Substring(1, formula.Length - 2); - cell.CellFormula = new CellFormula(formula) - { - FormulaType = CellFormulaValues.Array, - Reference = cellReference - }; + String formulaReference = null; + var f = new CellFormula {FormulaType = CellFormulaValues.Array}; + + if (opCell.FormulaReference.FirstAddress.Equals(opCell.Address)) + { + f.Text = formula; + f.Reference = opCell.FormulaReference.ToStringRelative(); + } + + cell.CellFormula = f; } else cell.CellFormula = new CellFormula(formula);