How to get the gridspan of a cell in table

I have the table structure something like:

<w:tr> <w:tc> <w:p><w:r><w:t>T3 Row3 -1</w:t></w:r></w:p> </w:tc> <w:tc> <w:tcPr> <w:gridSpan w:val="2"/> </w:tcPr> <w:p><w:r><w:t>T3 Row3 -2</w:t></w:r></w:p> </w:tc> <w:tc> <w:p><w:r><w:t>T3 Row3 -4</w:t></w:r></w:p> </w:tc> </w:tr>

Need to get the gridSpan of each cells and print something like below:

private void parseTableWithSpan(Table table) throws Exception {

    int rowIndex = 0;

    for (Row row : table.getRows()) {

        int columnIndex = 0; // logical column index (not cell index)

        for (Cell cell : row.getCells()) {

            String cellText = cell.toString(SaveFormat.TEXT)
                                  .replace("\u0007", "")
                                  .trim();

            int gridSpan = cell.getCellFormat().getColumnSpan();

            log.info(
                "Row: {}, Column: {}, Span: {}, Text: [{}]",
                rowIndex,
                columnIndex,
                gridSpan,
                cellText
            );

            // Move column index by grid span
            columnIndex += gridSpan;
        }

        rowIndex++;
    }
}

How can I achieve this?
Attaching the document for reference
Table Merging.docx (11.1 KB)

I am using aspose 25.10 but not able to resolve:

cell.getCellFormat().getColumnSpan()

Please let me know if there is any other method to get columnsSpan?

@ashu_agrawal_sirionlabs_com You can use CellFormat.VerticalMerge and CellFormat.HorizontalMerge properties to determine whether the cell is merged or not.

You should note, in MS Word table there is no “column” concept, each row in the table is independent and can contain any number of cells. Please see our documentation for more information:
https://docs.aspose.com/words/java/working-with-columns-and-rows/#work-with-columns

Therefore, horizontal merged cells in MS Word tables can be simulated by simply wide cells. You can convert “simply wide” cells to horizontally merged cells using the Table.convertToHorizontallyMergedCells method.

Hi @alexey.noskov

In the attached document, I can see that in row3, second column is having <w:gridSpan w:val=“2”/>.
then why cell.getCellFormat().getHorizontalMerge() is giving 0.

PFB the xml of row3 in attached document:

<w:tr> <w:tc> <w:p><w:r><w:t>T3 Row3 -1</w:t></w:r></w:p> </w:tc> <w:tc> <w:tcPr> <w:gridSpan w:val="2"/> </w:tcPr> <w:p><w:r><w:t>T3 Row3 -2</w:t></w:r></w:p> </w:tc> <w:tc> <w:p><w:r><w:t>T3 Row3 -4</w:t></w:r></w:p> </w:tc> </w:tr>

Table Merging.docx (11.1 KB)

@ashu_agrawal_sirionlabs_com CellFormat.HorizontalMerge does not return number of merged cells. It returns type of horizontal merge of the cell, i.e. First, Previous or None value. You can calculate number of horizontally merged cells using code like the following:

Document doc = new Document("C:\\Temp\\in.docx");

Table t = doc.getFirstSection().getBody().getTables().get(0);
t.convertToHorizontallyMergedCells();
for (int i = 0; i < t.getRows().getCount(); i++)
{
    Row r = t.getRows().get(i);
    for (int j = 0; j < r.getCells().getCount(); j++)
    {
        Cell c = r.getCells().get(j);
        if (c.getCellFormat().getHorizontalMerge() == CellMerge.FIRST)
        {
            int colSpan = 1;
            Cell nextCell = c.getNextCell();
            while (nextCell != null && nextCell.getCellFormat().getHorizontalMerge() == CellMerge.PREVIOUS)
            {
                colSpan++;
                j++;
                nextCell = nextCell.getNextCell();
            }

            System.out.println("Cell (" + (i + 1) + ", " + (r.getCells().indexOf(c) + 1) + ") has colSpan=" + colSpan);
        }
    }
}