# How to retrieve merged cell ColSpan property from table cell objects in Word? Currently, only Rowspan property is visible

**URL:** <https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006>\
**Category:** Development\
**Created:** [September 24, 2024, 12:16pm UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006 "2024-09-24T12:16:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![gulixiang](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/gulixiang/32/1227_2.png) [@gulixiang](https://forum.collaboraonline.com/u/gulixiang)\
**Post date:** [September 24, 2024, 12:16pm UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/1 "2024-09-24T12:16:49Z")

</div>

I want to retrieve the situation of merging cells, and I feel that this is the most basic attribute

---

<div class="post-metadata">

**Author:** ![darshan](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/darshan/32/2230_2.png) [@darshan](https://forum.collaboraonline.com/u/darshan)\
**Post date:** [September 25, 2024, 6:31am UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/2 "2024-09-25T06:31:28Z")

</div>

To retrieve the `ColSpan` (column span) property of a merged cell in **LibreOffice Calc** , you need to use the UNO API, which allows you to access and manipulate various table (spreadsheet) attributes, including merged cells. The `ColSpan` property isn’t directly available, but you can determine it through properties such as `IsMerged` or `CellRangeAddress`.

Here’s how you can retrieve this information using Python with PyUNO:

### Example Python (PyUNO) Script for LibreOffice Calc:

```python
from com.sun.star.uno import Exception as UnoException

def get_merged_cells(sheet):
    # Loop through rows and columns of a sheet
    for row in range(sheet.Rows.Count):
        for col in range(sheet.Columns.Count):
            try:
                # Get the cell object
                cell = sheet.getCellByPosition(col, row)
                
                # Get the cell range address (includes merged region)
                cell_range_address = cell.getRangeAddress()
                
                # Check if it's part of a merged cell
                if cell_range_address.EndColumn != cell_range_address.StartColumn:
                    col_span = cell_range_address.EndColumn - cell_range_address.StartColumn + 1
                else:
                    col_span = 1 # Not merged
                
                print(f"Cell[{row}, {col}] - ColSpan: {col_span}")
                
            except UnoException as e:
                print(f"Error: {e}")

```

### Explanation:

- The script accesses each cell in the specified sheet.
- `getRangeAddress()` provides the merged cell’s range, which allows you to calculate the `ColSpan` (difference between the `EndColumn` and `StartColumn`).
- If the start and end columns differ, it means the cell spans across multiple columns.

> **[LibreOffice 25.8 SDK - Overview](https://api.libreoffice.org/)**

> **[LibreOffice: XCellRange Interface Reference](https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1table_1_1XCellRange.html)**

> **[LibreOffice: XCell Interface Reference](https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1table_1_1XCell.html)**

---

<div class="post-metadata">

**Author:** ![darshan](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/darshan/32/2230_2.png) [@darshan](https://forum.collaboraonline.com/u/darshan)\
**Post date:** [September 25, 2024, 6:32am UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/3 "2024-09-25T06:32:25Z")

</div>

Some forum ans that may also help

> **[Calc Macro Merge Cells](https://ask.libreoffice.org/t/calc-macro-merge-cells/87391)**
>
> I have a macro that requires user input to: dispatcher.executeDispatch(document, “.uno:ToggleMergeCells”, “”, 0, Array()) The pop up shows 3 options. I would like code to provide an answer to the pop up.

---

<div class="post-metadata">

**Author:** ![gulixiang](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/gulixiang/32/1227_2.png) [@gulixiang](https://forum.collaboraonline.com/u/gulixiang)\
**Post date:** [September 25, 2024, 6:36am UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/4 "2024-09-25T06:36:07Z")

</div>

@darshan Thank you for your reply. I am in the writer, not calc. I have searched for Cell, CellRange, and Table objects, but have not found any properties similar to ColumnSpan

---

<div class="post-metadata">

**Author:** ![darshan](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/darshan/32/2230_2.png) [@darshan](https://forum.collaboraonline.com/u/darshan)\
**Post date:** [September 25, 2024, 6:41am UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/5 "2024-09-25T06:41:55Z")

</div>

> [@gulixiang](#):
>
> I have searched for Cell, CellRange, and Table objects, but have not found any properties similar to ColumnSpan

To retrieve the `ColSpan` (column span) property from a table cell inside **LibreOffice Writer** using Python and UNO, you can access the table’s structure through the API. The table cells don’t expose a direct `ColSpan` property like you might expect, but you can still calculate the column span by checking whether a cell is part of a merged range.

```auto

from com.sun.star.uno import Exception as UnoException

def get_colspan_from_table(writer_table):
    rows = writer_table.Rows.Count
    cols = writer_table.Columns.Count

    for row_idx in range(rows):
        for col_idx in range(cols):
            try:
                # Access the cell at (col_idx, row_idx)
                cell = writer_table.getCellByPosition(col_idx, row_idx)

                # Get the cell's range address
                cell_range = cell.getRangeAddress()

                # Calculate ColSpan based on the range of merged columns
                col_span = cell_range.EndColumn - cell_range.StartColumn + 1

                # Print the ColSpan of the current cell
                print(f"Cell[{row_idx}, {col_idx}] - ColSpan: {col_span}")

            except UnoException as e:
                print(f"Error: {e}")

# Assuming 'doc' is your opened LibreOffice Writer document
# Access the table (replace '0' with the correct table index if needed)
writer_table = doc.TextTables.getByIndex(0)
get_colspan_from_table(writer_table)

```

- **Get Table Object** : `getCellByPosition(col_idx, row_idx)` is used to access each cell in the table.
- **Cell Range** : `getRangeAddress()` retrieves the start and end columns of the cell’s range.
- **Calculate `ColSpan`** : By subtracting the `StartColumn` from `EndColumn`, you can determine how many columns the cell spans.

[https://api.libreoffice.org/docs/idl/ref/XTextTable\_8idl.html](https://api.libreoffice.org/docs/idl/ref/XTextTable_8idl.html)

Thanks,  
Darshan

---

<div class="post-metadata">

**Author:** ![gulixiang](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/gulixiang/32/1227_2.png) [@gulixiang](https://forum.collaboraonline.com/u/gulixiang)\
**Post date:** [September 25, 2024, 7:22am UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/6 "2024-09-25T07:22:38Z")

</div>

@darshan Still not working, it just reported an error. The cell object does not have the getRangeAddress() function

---

<div class="post-metadata">

**Author:** ![darshan](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/darshan/32/2230_2.png) [@darshan](https://forum.collaboraonline.com/u/darshan)\
**Post date:** [September 25, 2024, 7:27am UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/7 "2024-09-25T07:27:45Z")

</div>

> [@darshan](#):
>
> ```auto
> cell_range = cell.getRangeAddress()
> 
> ```

Oh, I see, you can check the cell’s `IsMerged` property and retrieve the range of merged cells using the `getRangeAddresses()` method from the `Table` object.  
Sudo code:

```auto

                # Initialize ColSpan
                col_span = 1

                # Check if the cell is part of a merged cell
                if cell.IsMerged:
                    # If merged, find the range address of the merged cell
                    merged_range = cell.getRangeAddresses()[0]
                    col_span = merged_range.EndColumn - merged_range.StartColumn + 1

```

---

<div class="post-metadata">

**Author:** ![vmiklos](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/vmiklos/32/787_2.png) [@vmiklos](https://forum.collaboraonline.com/u/vmiklos)\
**Post date:** [September 25, 2024, 7:36am UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/8 "2024-09-25T07:36:47Z")

</div>

Perhaps it’s useful to know that in Writer’s table model, there is no such thing as “col span”. The model is just a list of rows, and then each row has a list of cells. Any definition of columns is just UI/UNO layer on top of this model. That may explain why it’s hard to find any explicit info about columns.

---

<div class="post-metadata">

**Author:** ![gulixiang](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/gulixiang/32/1227_2.png) [@gulixiang](https://forum.collaboraonline.com/u/gulixiang)\
**Post date:** [September 25, 2024, 8:01am UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/9 "2024-09-25T08:01:25Z")

</div>

@vmiklos It is very likely that this is a very basic attribute, and it feels like a design flaw. The presentation and Calc do not have this issue.

---

<div class="post-metadata">

**Author:** ![gulixiang](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/gulixiang/32/1227_2.png) [@gulixiang](https://forum.collaboraonline.com/u/gulixiang)\
**Post date:** [September 25, 2024, 8:04am UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/10 "2024-09-25T08:04:27Z")

</div>

@darshan I have tried everything that still doesn’t work because the cell object doesn’t have the IsMerged and getRangeAddresses properties

---

<div class="post-metadata">

**Author:** ![vmiklos](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/vmiklos/32/787_2.png) [@vmiklos](https://forum.collaboraonline.com/u/vmiklos)\
**Post date:** [September 26, 2024, 12:43pm UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/11 "2024-09-26T12:43:27Z")

</div>

@gulixiang nah, it’s simply the Writer table model (table is a list of rows, row is a list of cells) fits the ODT and DOCX table model. I understand it’s not a good fit for the HTML table model, and col span is a concept in the HTML table model only.

Are you interested in contributing so the UNO API would hide this complexity?

Code pointer: you can already get column separators for a table (on a 0…10000 scale) here:

> <https://github.com/libreoffice/core/blob/f709c1e7a8060661d3f32d86ce035b95f3fdfd52/sw/source/core/unocore/unotbl.cxx#L2928-L2933>

You could implement something similar, to determine what are the UI-level columns of a table and then determine how many of those columns cover your table box.

---

<div class="post-metadata">

**Author:** ![gulixiang](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/gulixiang/32/1227_2.png) [@gulixiang](https://forum.collaboraonline.com/u/gulixiang)\
**Post date:** [September 27, 2024, 1:19am UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/12 "2024-09-27T01:19:40Z")

</div>

[table-demo.docx](https://forum.collaboraonline.com/uploads/short-url/eeipYjoW4f53e7cHnyNCCNOtpAw.docx) (4.6 KB)  
Your guess is correct. The table in the document does not have the concept of col span. I guess he wants to achieve more flexible formatting. Look at the document I uploaded, there is a table inside the document, and in this case, col span cannot be calculated.

---

<div class="post-metadata">

**Author:** ![darshan](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.collaboraonline.com/darshan/32/2230_2.png) [@darshan](https://forum.collaboraonline.com/u/darshan)\
**Post date:** [October 8, 2024, 6:48am UTC](https://forum.collaboraonline.com/t/how-to-retrieve-merged-cell-colspan-property-from-table-cell-objects-in-word-currently-only-rowspan-property-is-visible/3006/13 "2024-10-08T06:48:19Z")

</div>


