Search code examples
asp.netexcelasp.net-coreasposeaspose-cells

With Aspose Cells, how to copy a Cell formula to another cell and update its references in .Net Core without its formating


I'm using Aspose Cells with .net Core.

I would like to copy a cell formula to another cell and update the cells references in the formula with its new "location".

If I use the cell.copy it's working but It's copy also the cell attributes, like 'locked'.

Is there way around it?

Like when using MS-Excel and you do a “Special Paste Formula Only”


Solution

  • I post my question on the Aspose Forum and got a quick answer from @Amjad Sahi

    https://forum.aspose.com/t/copy-formula-from-a-cell-to-another-with-updated-references-without-the-source-cell-attribute-like-locked/239996/2

    It's worked for me.

    I end up with this:

        public void CopyFormulaFrom(ICell cell)
        {
            var cells = cell.Sheet.Name == Sheet.Name
                ? _cell.Worksheet.Cells
                : _cell.Worksheet.Workbook.Worksheets[cell.Sheet.Name]?.Cells;
    
            var cellSource = cells.CreateRange(cell.Row, cell.Column, 1, 1);
            var cellDestination = cells.CreateRange(_cell.Row, _cell.Column, 1, 1);
            
            var options = new PasteOptions() { PasteType = PasteType.Formulas };
    
            cellDestination.Copy(cellSource, options);
        }