CALC: GetElementNames and Filter

From here, I’m running this FUNCTION to get the SheetNames of a CALC-file:

SHEETNAMES = ThisComponent.Sheets.getElementNames()
End Function

How can I reduce it to SHEETNAME_FILTERED(), having only the shees with a certain spelling,
f.e. filter “ABC*” should provide “ABC 01”, ABC 02" but NOT “XYZ 99”.
Thank you!


doc = XSCRIPTCONTEXT.getDocument()
sheet_names = doc.Sheets.ElementNames
abc_sheets = list(filter(lambda x: x.startswith('ABC'), sheet_names))

Thanks @karolus, but

  • Is it a replacment for my FUNCTION or a SUB or …
  • how to use your code, how do I get the filtered result into the spreadsheet?
  • Did you really mean ‘ABC’? Here its marked as comment after first ’

It is a Python macro. Contrary to Basic, Python is a programming language for adults. Your requirement indicates that you have a massive problem with the organization of your data. Splitting equally structured data across sheets has always been a very popular mistake.
In stupid Basic, the following may work (did not test)

Function getABC_Sheets()
Dim a()
sheet_names = ThisComponent.Sheets.ElementNames
for each s in sheet_names
  if left(s, 3)="ABC" then bas_PushArray(a(), s)
getABC_Sheets = a()
End Function

Sub bas_PushArray(xArray(),vNextElement)
REM helper function to help Basic fill up an array
Dim iUB%,iLB%
	iLB = lBound(xArray())
	iUB = uBound(xArray())
	If iLB > iUB then
		iUB = iLB
		redim xArray(iLB To iUB)
		iUB = iUB +1
		redim preserve xArray(iLB To iUB)
	xArray(iUB) = vNextElement
End Sub

Thank you @Villeroy. If I’m not wrong, my source (which I mentioned in my question and which works perfectly) has been your work. The only task I have: To reduce this list to those starting with “ABC”.