【问题标题】:Excel Open xml sdk - Copy formula between cells with range modificationExcel Open xml sdk - 在具有范围修改的单元格之间复制公式
【发布时间】:2015-02-26 09:24:18
【问题描述】:

我有一个带有公式 (CellFormula) 的单元格。我不知道公式内容(可能是 Sum、ifs 或任何其他计算)。
该公式可能包括范围。
当您将单元格拖动到其他单元格(手动在 excel 应用程序本身)时,公式范围会更新。
我希望公式编程副本的行为方式相同。
如果公式包含范围说明符,我想将它们更新到当前行。
是否有一些内置方法可以在没有正则表达式操作的情况下这样做?

【问题讨论】:

    标签: c# excel openxml openxml-sdk


    【解决方案1】:

    我不知道这是否是一个涵盖所有可能性的好解决方案,但是...

    //Formula update
    if (cloneFromCell.CellFormula != null && (cloneFromCell.CellFormula.FormulaType == null || !cloneFromCell.CellFormula.FormulaType.HasValue || cloneFromCell.CellFormula.FormulaType.Value == CellFormulaValues.Normal))
    {
        uint cloneRowIndex = OXMLTools.GetRowIndex(cloneFromCell.CellReference);
        uint offset = rowIndex - cloneRowIndex;
        exCell.CellFormula.Text = OXMLTools.GetUpdatedFormulaToNewRow(cloneFromCell.CellFormula.Text, offset);
    }
    
    public static string GetUpdatedFormulaToNewRow(string formula, uint offset)
    {
        return Regex.Replace(formula, @"[A-Za-z]+\d+", delegate(Match match)
        {
          //Calculate the new row for this cell in the formula by the given offset
          uint oldRow = GetRowIndex(match.Value);
          string col = GetColumnName(match.Value);
          uint newRow = oldRow + offset;
    
          //Create the new reference for this cell
          string newRef = col + newRow;
          return newRef;
        });
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多