How to group colums or rows in LibreOffice Calc using macro

I found only how to show or hide grouping. How can I write macro to group columns or rows ?

Hallo

def example_group_hide_show(*_):
    doc = XSCRIPTCONTEXT.getDocument()
    sel = doc.CurrentSelection
    r_addr = sel.RangeAddress
    sheet = sel.Spreadsheet
    sheet.group(r_addr,1) # 0→group columns, 1→group rows
    sheet.hideDetail(r_addr) #collapse
    sheet.showDetail(r_addr) #un-collapse
1 Like

LibreOffice BASIC, please.
Not Python

@lonk You may not believe it, but the BASIC code will be the same:

Sub example_group_hide_show()
    doc = ThisComponent
    sel = doc.CurrentSelection
    r_addr = sel.RangeAddress
    sheet = sel.Spreadsheet
    sheet.group(r_addr,1) '# 0→group columns, 1→group rows
    sheet.hideDetail(r_addr) '#collapse
    sheet.showDetail(r_addr) '#un-collapse
End Sub

And the answer to your question is this line:

sheet.group(r_addr,1) '# 0→group columns, 1→group rows

2 Likes

@JohnSUN
How can I change from CurrentSelection to getCellRangeByPosition ?

Sub GroupColumns
	Dim doc As Object
	Dim sheet As Object
	Dim r_addr As Object
	doc = ThisComponent
	sheet = doc.CurrentController.ActiveSheet
	r_addr = sheet.getCellRangeByPosition(1, 0, 1, 0)
	sheet.group(r_addr, 0) '# 0→group columns, 1→group rows
End Sub

Add .RangeAddress

r_addr = sheet.getCellRangeByPosition(1, 0, 1, 0).RangeAddress

For .group() you need CellRangeAddress

1 Like

@JohnSUN
Thanks A Ton.

youre a proficient copy&paste-coder…are you??

1 Like

If you think the OP is more BASIC-oriented, then maybe along with a Python solution include a hyperlink to a 101 on adding a Python macro to an ODS file. Win a convert.

1 Like