[Solved] Call nested Calc function in Basic

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

[Solved] Call nested Calc function in Basic

Post by luofeiyu »

The formula can get rigth value in openoffice calc.

Code: Select all

=INDEX(B1:B47,MATCH("land",A1:A47,0))
I want to write it in oobasic codes:

Code: Select all

sub callindex()
    dim oFunAccess as object
    oFunAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )
    dim oDoc, oSheet, oRange as object
    
    oDoc = ThisComponent
    oSheet = oDoc.sheets.getByName("Sheet1")
    oRange = oSheet.getCellRangeByName("a1:a47")	
    dim embedded_formula as string    
    embedded_formula = "MATCH('land',A1:A47,0)"
    num = oFunAccess.CallFunction( "index",array("b1:b47", embedded_formula))
    print num
end sub
https://i.sstatic.net/L5ie9wdr.png
Last edited by luofeiyu on Sat Feb 01, 2025 5:22 am, edited 1 time in total.
LibreOffice 7.4.7.2 on Debian 12
User avatar
karolus
Volunteer
Posts: 1226
Joined: Sat Jul 02, 2011 9:47 am

Re: How call worksheet function with embedded worksheet function?

Post by karolus »

Hallo

obviously its:

Code: Select all

sub call_nested_calc()
    oFunAccess = createUnoService( "com.sun.star.sheet.FunctionAccess" )
    
    oDoc = ThisComponent
    oSheet = oDoc.sheets.getByName("Sheet1")
    with oSheet
    	a_Range = .getCellRangeByName("a1:a47")
    	b_Range = .getCellRangeByName("b1:b47")
    end with	
    with oFunAccess
    	num = .CallFunction( "index",array( b_Range, .CallFunction("match", array("land", a_Range, 0))))
    end with
    print num
end sub
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 24.8… flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Locked