[Solved] Avoid Calc functions in subroutine

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
luofeiyu
Posts: 55
Joined: Thu Sep 14, 2017 2:11 am

[Solved] Avoid Calc functions in subroutine

Post by luofeiyu »

The target sheet is simple.

https://i.sstatic.net/frSmYl6t.png

I want to find which district's vol is 580,to ype "=INDEX(A2:A3,MATCH(580,B2:B3,0))" in B6 can get it.
And i can get the district name with below code:

Code: Select all

sub call_nested_calc()
    oFunAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )    
    oDoc = ThisComponent
    oSheet = oDoc.sheets.getByName("Sheet15")
    with oSheet
    	a_Range = .getCellRangeByName("a2:a3")
    	b_Range = .getCellRangeByName("b2:b3")
    end with	
    with oFunAccess
    	district = .CallFunction( "index",array( a_Range, .CallFunction("match", array(580, b_Range, 0))))
    end with
    print district
end sub
How can simplify the oobasic codes to get the result?Other way ,not to call worksheet function.
Last edited by luofeiyu on Sat Feb 01, 2025 5:23 am, edited 1 time in total.
LibreOffice 24.8 on Debian 12
User avatar
karolus
Volunteer
Posts: 1236
Joined: Sat Jul 02, 2011 9:47 am

Re: Simplify subroutine to avoid Calc function

Post by karolus »

luofeiyu wrote: Thu Jan 30, 2025 2:56 pm

Other way ,not to call worksheet function.
use python, eg:

Code: Select all

doc = XSCRIPTCONTEXT.getDocument()
data = doc.Sheets["Sheet15"]["A2:B28"].DataArray
countries, values = list(zip(*data))
print( countries[values.index(52)])
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
Locked