Python macro check if groups exists in sheets

Dear all,
I have a LO *.ods file with several sheets, some of them contain column groups, some don’t.
With a LO macro in python I need to programmatically identify the sheets that contain groups of columns.
The XSheetOutline Interface does not providing this info. Is there another way?
With openpyxl there seems a way, but it does not read ods files and would be unnecessary overhead for a simple boolean check. I can’t imagine this property is not accessible.

@karolus you seem to have good knowledge, any ideas?

Thanks!

While @karolus is on his way to the forum… :slightly_smiling_face:
Unfortunately, I don’t know the direct way.

We can try a workaround:

  1. Select sheet columns from used range.
  2. Use the function GetFeatureState with parameter .uno:Ungroup. If there are grouped columns among the selected columns, then the IsEnabled property of the FeatureStateEvent structure will be True.

@sokol92 yes, thank you, that worked. The code by @torrua from here worked.
I added

def find_grouped_sheets(doc, sheet_groups):
    '''
    Finds sheets with outline groups.

    Parameters:
        doc             (obj):XSCRIPTCONTEXT.getDocument() from LO.
        sheet_groups    (dict):Empty dict for sheets with groups.

    Returns:
        sheet_groups    (dict):dict with sheets with groups. key = sheet id; val = boolean True/False.   
    '''
    
    uno_cmd = "Ungroup"
    # omit first sheet
    for i in range(1,len(doc.Sheets)):
        sheet = doc.Sheets.getByIndex(i)
        view = doc.getCurrentController()
        cursor = sheet.createCursor()
        cursor.gotoEndOfUsedArea(False)
        cursor.gotoStartOfUsedArea(True)
        view.select(cursor)
        event = get_feature_state(doc, uno_cmd)
        sheet_groups[i] = event.IsEnabled
    return sheet_groups

def deselect(doc):
    view = doc.getCurrentController()
    # source: https://wiki.documentfoundation.org/Macros/Python_Guide/Useful_functions#Create_instances
    desktop = create_instance("com.sun.star.frame.Desktop", True)
    document = desktop.getCurrentComponent()
    emptyRanges = document.createInstance("com.sun.star.sheet.SheetCellRanges")
    view.select(emptyRanges)

deselect() function is called elsewhere in the script to undo the selection of the find_grouped_sheets() function.
An alternative I was thinking about was to parse the content.xml file from within an odf file and search for table-column-group tag as child from table tag.

1 Like