»range« is a python-builtin, dont use it otherwise!
there is also no need for:
cursor = sheet.createCursorByRange(range)
cursor.DataArray = reversed(cursor.DataArray)
just use:
range.DataArray = reversed( range.DataArray)
I would replace your »creative« solution with:
def flip_Data(*_, header_rows=1):
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.CurrentController.ActiveSheet
cell_range = doc.CurrentSelection
if len(cell_range.DataArray) <= 1:
cursor = sheet.createCursor()
cursor.gotoEndOfUsedArea(True)
cell_range = sheet[ f"A2:{cursor.AbsoluteName.rsplit(':',1)[1]}"]
cell_range.DataArray = reversed(cell_range.DataArray)
or just slice cursor :
def flip_Data(*_, header_rows=1):
doc = XSCRIPTCONTEXT.getDocument()
sheet = doc.CurrentController.ActiveSheet
cell_range = doc.CurrentSelection
if len(cell_range.DataArray) <= 1:
cursor = sheet.createCursor()
cursor.gotoEndOfUsedArea(True)
cursor[1:,:].DataArray = reversed(cursor[1:,:].DataArray)
return
cell_range.DataArray = reversed(cell_range.DataArray)