IRange.Copy throws InvalidOperationException for merged cells

会有一股神秘感。 提交于 2019-12-25 01:52:37

问题


I am using below method to copy value without formula from a range of excel to the same range (basically removing formula). This range has 7 cells but they are merged.

void Copy(IRange destination, PasteType pasteType, PasteOperation pasteOperation, bool skipBlanks, bool transpose);

I am getting InvalidOperationException with message "Operation is not valid for a partial merged cell."

SpreadsheetGear.Core Version - 8.0.63.102

This works well with single cell but not with merged cells.

sourceRange.Copy(targetRange, SpreadsheetGear.PasteType.Values, SpreadsheetGear.PasteOperation.None, false, false);

回答1:


This behavior is by design, as SpreadsheetGear does not allow pasting into a range that includes partial merged cells. Instead, you would need to adapt the paste operation in some way so that the destination range fully includes all cells that are part of a merged range. There are some APIs in the IRange interface that can help you determine if a given IRange is part of a merged range and return adjusted IRanges to account for this:

  • IRange.MergeCells - Gets or sets the property which specifies whether the top left cell of this IRange is merged.
  • IRange.MergeCellsDefined - Returns true if the MergeCells property of all cells represented by this IRange is false or if all cells represented by this IRange are contained by the same merged cell area, otherwise false is returned.
  • IRange.MergeArea - Returns an IRange representing the merged cell area if the cell represented by this single cell IRange is part of a merged cell range, otherwise the original IRange is returned.
  • IRange.EntireMergeArea - Returns an IRange representing the area of the current IRange which is iteratively grown as needed until it includes no partial merged cells.

Below is some sample code that demonstrates how you can use some of the above APIs to deal with your case. Note depending on the ranges involved in the copy operation (single cell vs multi-cell), the approach might vary.

// Setup a workbook with some test data and a merged range.
IWorkbook workbook = Factory.GetWorkbook();
IRange cells = workbook.ActiveWorksheet.Cells;
cells["A1:B2"].Formula = "=ROW()+COLUMN()";
cells["A1:A7"].Merge();

//For a single cell copy operation
{
    IRange copyPasteRange = cells["A1"];

    // If IRange.MergeCells is true, A1 will be part of a larger merged range, so we 
    // should use the IRange.MergeArea property to expand A1 to include the entire 
    // merged range
    if (copyPasteRange.MergeCells)
        copyPasteRange = copyPasteRange.MergeArea;
    copyPasteRange.Copy(copyPasteRange, PasteType.Values, PasteOperation.None, false, false);
}

// For a multi-cell copy operation.  
{
    // Note in this case that A1 is part of merged range A1:A7 but B2 is not merged.  
    // How you deal with this depends on your requirements.  
    IRange copyPasteRange = cells["A1:B2"];

    // One way is to use IRange.EntireMergeArea which will iteratively grow the specified 
    // IRange until it fully includes all merged ranges, so in this case A1:B7.  
    // Obviously, this means B2:B7 will be copied as a side-effect, but this is what 
    // would happen if you tried to copy this range in Excel's UI (i.e., when attempting 
    // to select A1:B1 in Excel, they will always expand the selected range to A1:B7), 
    // so might be acceptable in some cases.
    if (copyPasteRange.Address != copyPasteRange.EntireMergeArea.Address)
        copyPasteRange = copyPasteRange.EntireMergeArea; // A1:B7
    copyPasteRange.Copy(copyPasteRange, PasteType.Values, PasteOperation.None, false, false);

    // Alternatively, you could loop through each cell in the originalCopyPaste 
    // range and copy each cell as needed.  I leave it up to you to flesh this out if 
    // it is necessary for your requirements.
    if (copyPasteRange.Address != copyPasteRange.EntireMergeArea.Address)
    {
        foreach (IRange cell in copyPasteRange)
        {
            // Paste by value for each cell, taking into account merged cells however you 
            // see fit.
        }
    }
}


来源:https://stackoverflow.com/questions/58432353/irange-copy-throws-invalidoperationexception-for-merged-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!