Free Support Forum - aspose.com

Issue cloning Row containing a merge cells

image.png (11.5 KB)

DIAGNOSE is a cell which is a merged cell across row’s 1,2,3.

Now let’s say I want to add a new row … between row 1 and 2 …
Such that DIAGNOSE is now a merged cell across row’s 1,2,3,4 - How to do it?

IRow middleRow = table.getRows.getItem(2);
table.getRows().insertClone(2, middleRow, false);

this throws :
class com.aspose.slides.PptxEditException: Can’t insert rows: target index breaks merged cells.

@kaushikranjandecisionmapper,

I have observed the image shared by you and request you to please share the working sample code and source presentation reproducing the issue. Please also provide the desired output presentation. We will investigate the issue further on our end on provision of requested information.

tablecell.zip (64.7 KB)

The attached code base is a SpringBoot Application.
You can find the sample pptx file in src/test/resources/Presentation.pptx

SLIDE 1 contains the source table.
SLIDE 2 contains the reference table.

Goal is to transition table in SLIDE 1 into the same layout as present in SLIDE 2

You can run the application, by simply running the MainTest.java in src/test/java/com/test/poc
If everything goes good, it should generate output.pptx where the table in SLIDE 1 should be same as table in SLIDE 2.

Let me know, if you have any other doubts to be answered.

@kaushikranjandecisionmapper,

I have observed your requirements and like to share that when you need to either add or remove a row in table with merged cells, you need to first split them (un-merge them) and then perform the desired operation. After that, you need to merge them again. Please try using following sample code on your end to serve the purpose.

public static void editSlide1()
{
    Presentation pr = getTemplate();
    ISlide slide = pr.getSlides().get_Item(0);

    ITable table = (ITable)slide.getShapes().get_Item(0);
    IRow rowToClone = table.getRows().get_Item(1);

    //insert clone for Data2
    int data2StartingPosition = table.getRows().size();
    table.getRows().addClone(rowToClone, true);
    table.getRows().get_Item(data2StartingPosition).get_Item(0).getTextFrame().setText("DATA 2");
    table.getRows().get_Item(4).get_Item(0).splitByRowSpan(1);
    IRow [] ri= table.getRows().insertClone(5, table.getRows().get_Item(4), false);
    ri[0].get_Item(1).getTextFrame().setText("Cell 1.5");
    ri[0].get_Item(0).getTextFrame().setText("");
    table.mergeCells(table.getRows().get_Item(4).get_Item(0), table.getRows().get_Item(5).get_Item(0),true);
    table.mergeCells(table.getRows().get_Item(4).get_Item(0), table.getRows().get_Item(6).get_Item(0), true);
    table.mergeCells(table.getRows().get_Item(4).get_Item(0), table.getRows().get_Item(7).get_Item(0), true);

  
    int data3StartingPosition = table.getRows().size();
    table.getRows().addClone(rowToClone, true);
    table.getRows().get_Item(data3StartingPosition).get_Item(0).getTextFrame().setText("DATA 3");

    table.getRows().get_Item(8).get_Item(0).splitByRowSpan(1);
    table.getRows().get_Item(9).get_Item(0).splitByRowSpan(1);
    table.getRows().removeAt(9,false);
    table.mergeCells(table.getRows().get_Item(8).get_Item(0), table.getRows().get_Item(9).get_Item(0), true);

    pr.save("C:\\Aspose Data\\output.pptx", com.aspose.slides.SaveFormat.Pptx);
}